1、程序结构
2、各种文件
LoginAction.java
package com.tfj.action;
public class LoginAction {
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;
}
public String execute() {
if (username.equals("tufujie")&&password.equals("123")) {
return "success";
} else
return "error";
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="login" namespace="/" extends="struts-default">
<action name="login" class="com.tfj.action.LoginAction" method="execute">
<result name="success">/WEB-INF/pages/success.jsp</result>
<result name="error">/WEB-INF/pages/error.jsp</result>
</action>
</package>
</struts>
error.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> 账号或者密码错误,1秒后返回登录界面 <br> <meta http-equiv="Refresh" content="1;url=http://localhost:8888/strutslogin/index.jsp" /> </body> </html>
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>第一个struts2应用</title> </head> <body> 欢迎<%=request.getParameter("username")%>登录 </body> </html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Login</display-name>
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
index.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> <form action="login" method="post"> 账号:<input type="text" name="username"><br /> 密码:<input type="password" name="password"><br /> <input type="submit" value="登录"> <input type="reset" value="重置" /> </form> </body> </html>