zhuce.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>
denglu.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"; } }
user_struts.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <package name="user" namespace="/" extends="struts-default"> 8 <action name="UserRegister" class="com.amos.web.action.UserAction" 9 method="register"> 10 <result name="toRegisterJsp" type="dispatcher"> 11 /register_success.jsp 12 </result> 13 </action> 14 <action name="UserLogin" class="com.amos.web.action.UserAction" 15 method="login"> 16 <result name="toLoginJsp" type="dispatcher"> 17 /login_success.jsp 18 </result> 19 </action> 20 </package> 21 </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>