1. 需求
2. JSP
chapter02/part02/login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Chapter02 | Part02</title>
</head>
<body>
<form id="loginForm" action="login.action" method="post">
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Login"></td>
</tr>
</table>
</form>
</body>
</html>
chapter02/part02/success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Chapter02 | Part02</title>
</head>
<body>
<h3>SUCCESS!</h3>
<hr>
<table>
<tr>
<td>Username:</td>
<td>${requestScope.username}</td>
</tr>
<tr>
<td>Password:</td>
<td>${requestScope.password}</td>
</tr>
</table>
</body>
</html>
chapter02/part02/error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Chapter02 | Part02</title>
</head>
<body>
<h3>ERROR!</h3>
<hr>
${requestScope.message}
</body>
</html>
3. Action
package chapter02.part02.action;
import com.opensymphony.xwork2.Action;
public class LoginAction implements Action {
private String username;
private String password;
private String message;
@Override
public String execute() throws Exception {
if (null == username || "".equals(username)) {
message = "Username is required ...";
return ERROR;
}
if (null == password || "".equals(password)) {
message = "Password is required ...";
return ERROR;
}
return SUCCESS;
}
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 getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
4. 注册Action
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> <include file="config/chapter02.xml"/> </struts>
config/chapter02.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="chapter02.part02" namespace="/chapter02/part02" extends="struts-default"> <action name="login" class="chapter02.part02.action.LoginAction"> <result name="success">success.jsp</result> <!-- 默认值为 success --> <result name="error">error.jsp</result> </action> </package> </struts>
5. 项目结构
6. 发布运行