需求:
1、用戶注冊(user_register.jsp)--》注冊成功(UserRegister.action)--》顯示注冊信息(register_success.jsp)
2、用戶登錄(user_login.jsp)--》登錄成功(UserLogin.action)--》顯示用戶名(login_success.jsp)
分析:
這里主要涉及struts2中對於多個類似的業務操作方法的封裝。
效果:
針對需求1,用戶注冊:
針對需求2,用戶登錄:
實現:
user_register.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>Insert title here</title> </head> <body> <form name="register" action="/struts2/UserRegister" method="post"> <table border="2" align="center"> <caption>新用戶注冊</caption> <tr> <th>用戶名:</th> <td><input name="username" id="username" type="text" /></td> </tr> <tr> <th>密碼:</th> <td><input name="password" id="password" type="password" /></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="提交" width="120ppx" /></td> </tr> </table> </form> </body> </html>
user_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>Insert title here</title> </head> <body> <form name="register" action="/struts2/UserLogin" method="post"> <table border="2" align="center"> <caption>用戶登錄</caption> <tr> <th>用戶名:</th> <td><input name="username" id="username" type="text" /></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="提交" width="120ppx" /></td> </tr> </table> </form> </body> </html>
UserAction.java

package com.amos.web.action; import com.opensymphony.xwork2.ActionSupport; /** * @ClassName: UserAction * @Description: 用戶管理,將相關的action封裝到一個類中 * @author: amosli * @email:amosli@infomorrow.com * @date Jan 8, 2014 1:06:00 AM */ public class UserAction extends ActionSupport { private static final long serialVersionUID = -6275534406709255984L; 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 register() throws Exception { return "toRegisterJsp"; } // 用戶登錄 public String login() throws Exception { return "toLoginJsp"; } }
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/user_struts.xml"></include>
</struts>
user_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="user" namespace="/" extends="struts-default"> <action name="UserRegister" class="com.amos.web.action.UserAction" method="register"> <result name="toRegisterJsp" type="dispatcher"> /register_success.jsp </result> </action> <action name="UserLogin" class="com.amos.web.action.UserAction" method="login"> <result name="toLoginJsp" type="dispatcher"> /login_success.jsp </result> </action> </package> </struts>
login_success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <!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>Insert title here</title> </head> <body> 登錄成功! <hr> 用戶名:<s:property value="username"/> </body> </html>
register_success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <!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>Insert title here</title> </head> <body> 注冊成功<br/> <hr> 用戶名:<s:property value="username" /> 密碼:<s:property value="password"/> </body> </html>
代碼分析:
代碼的入口為兩個jsp程序,user_register.jsp和user_login.jsp,因為原理大體相同,這里主要講一下登錄,注冊上一篇文章已經講過了。
首先,在地址欄里輸入http://localhost:8080/struts2/user_login.jsp,表單的action動作為POST方式提交,對應的動作時是UserLogin.action;UserLogin在struts啟動時struts.xml加載到內存中時已經將user_struts.xml 反射出來了。user_struts.xml中已經配置了,調用com.amos.web.action.UserAction中的login方法,然后將值再轉發到login_success.jsp中,login_success.jsp,通過s標簽進行取值並最終顯示到瀏覽器中。
這里最主要的思想在於將登錄和注冊這兩種相類似的業務,相關聯的業務整合到一個action中。
用
戶注冊(user_register.jsp)--》注冊成功(UserRegister.action)--》顯示注冊信息(register_success.jsp)
用戶登錄(user_login.jsp)--》登錄成功(UserLogin.action)--》顯示用戶名(login_success.jsp)