struts 练习

本文介绍了将Java Struts项目部署到Tomcat服务器的过程,包括将JavaStrutsDemo部署到tomcat webapps目录。详细阐述了登录流程,从index.jsp页面提交用户名和密码,经struts.xml配置,由LoginAction执行验证,根据结果跳转到相应页面,还给出了相关配置文件和代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

启动服务器:

tomcat的webapps下可以部署创建的web project:

创建好的JavaStrutsDemo部署到tomcat webapps目录下

1、http://127.0.0.1:8080/JavaStrutsDemo/index.jsp

2、index.jsp - post username password action login

3、struts.xml - struts action login

4、cgh.StrutsAction.LoginAction 执行 execute

5、根据执行返回值,success或者login 比较struts.xml中的配置跳转到index.jsp或者welcom.jsp

 

 

目录

  1. project
  2. wel.xml
  3. struts.xml
  4. index.jsp
  5. welcom.jsp
  6. LoginAction,java

 

project

 

JavaStrutsDemo

    src

        cgh.StrutsAction.LoginAction.java

        struts.xml

JRE jdk7u45.jar

Java EE 5.jar

Web App Libraries.jar

WebRoot

    META-INF

    WEB_INF

        lib

 

    web.xml

    index.jsp

    welcom.jsp

 

 

 

wel.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5"

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<display-name></display-name>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<filter>

<!--指定Struts2的核心Filter-->

<filter-name>struts2</filter-name>

<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

</filter-class>

</filter>

 

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

 

</web-app>

 

struts.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<package name="default" namespace="/" extends="struts-default">

<action name="login" class="cgh.StrutsAction.LoginAction" method="execute" >

<result name="success">welcome.jsp</result>

<result name="login">login.jsp</result>

</action>

</package>

</struts>

 

 

index.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>登录</title>

</head>

<body>

<s:form action="login" method="post">

<s:label value="系统登录"></s:label>

<s:textfield name="username" label="账号"></s:textfield>

<s:password name="password" label="密码"></s:password>

<s:submit value="登录"></s:submit>

</s:form>

</body>

</html>

 

welcom.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>欢迎</title>

</head>

<body>

欢迎${username}

</body>

</html>

 

LoginAction,java

 

package cgh.StrutsAction;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class LoginAction extends ActionSupport{

private static final long serialVersionUID = 1L;

//该类继承了ActionSupport,就可以直接使用SUCCESS LOGIN等变量和 重写execute方法

private String username;

private String password;

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

@Override

public String execute() throws Exception {

if("cgh".equals(username) && "123".equals(password))

return SUCCESS;

return LOGIN;

}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值