接上例
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.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="login" class="com.xh.struts2.struts2InAction.LoginAction" method="login"> <result name="success">/WEB-INF/view/success.jsp</result> <result name="fail">/WEB-INF/view/fail.jsp</result> </action> <action name="rigest" class="com.xh.struts2.struts2InAction.RigestAction" method="rigest"> <result name="rigest">/WEB-INF/view/rige_success.jsp</result> </action> <action name="toLogin" > <result >/WEB-INF/view/login.jsp</result> </action> <action name="toRigest" > <result >/WEB-INF/view/rigest.jsp</result> </action> </package> </struts>
user.java
package com.xh.struts2.bean; public class User { private String userName; private String password; private int age; 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } }
rigest.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!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>注册</title> </head> <body> <form action="rigest" method="post"> 姓名:<s:textfield name="user.userName"></s:textfield> 密码:<s:textfield name="user.password"></s:textfield> 年龄:<s:textfield name="user.age"></s:textfield> <button type="submit">注 册</button> </form> </body> </html>
rigestAction.java
package com.xh.struts2.struts2InAction; import com.opensymphony.xwork2.ActionSupport; import com.xh.struts2.bean.User; public class RigestAction extends ActionSupport{ /** * */ private static final long serialVersionUID = 1L; private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String rigest() { //setUser(user); return "rigest"; } }
rige_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"> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>欢迎</title> </head> <body> <h2>欢迎</h2> <h3><s:property value="user.userName"/></h3> <h3><s:property value="user.password"/></h3> <h3><s:property value="user.age"/></h3> </body> </html>