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="struts" extends="struts-default"> 8 <action name="loginvalidate" class="com.sunflower.action.LoginAction"> 9 <result name="success">/welcome.jsp</result> 10 <!-- 如果輸入信息的校驗出錯,則轉回index.jsp --> 11 <result name="input">/index.jsp</result> 12 </action> 13 14 <action name="action1" class="com.sunflower.action.Action1"> 15 <result name="success" type="redirectAction"> 16 <param name="actionName">action2</param> 17 <param name="username">${username}</param> 18 <param name="password">${password}</param> 19 </result> 20 </action> 21 22 <action name="action2" class="com.sunflower.action.Action2"> 23 <result name="success">action2.jsp</result> 24 </action> 25 </package> 26 </struts>
action1.jsp:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>信息輸入</title> 8 </head> 9 <body> 10 <form action="action1" method="post"> 11 姓名:<input type="text" name="username"><br> 12 密碼:<input type="password" name="password"><br> 13 <input type="submit" value="提交"> 14 </form> 15 </body> 16 </html>
action2.jsp:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib prefix="s" uri="/struts-tags"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>顯示信息</title> 9 </head> 10 <body> 11 姓名:<s:property value="username"/> 12 密碼:<s:property value="password"/> 13 </body> 14 </html>
Action1.java:
1 package com.sunflower.action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class Action1 extends ActionSupport { 6 private String username; 7 private String password; 8 9 public String getUsername() { 10 return username; 11 } 12 13 public void setUsername(String username) { 14 this.username = username; 15 } 16 17 public String getPassword() { 18 return password; 19 } 20 21 public void setPassword(String password) { 22 this.password = password; 23 } 24 25 public String execute() throws Exception { 26 return SUCCESS; 27 } 28 }
Action2.java:
1 package com.sunflower.action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class Action2 extends ActionSupport { 6 private String username; 7 private String password; 8 9 public String getUsername() { 10 return username; 11 } 12 13 public void setUsername(String username) { 14 this.username = username; 15 } 16 17 public String getPassword() { 18 return password; 19 } 20 21 public void setPassword(String password) { 22 this.password = password; 23 } 24 25 public String execute() throws Exception { 26 return SUCCESS; 27 } 28 }
關鍵是在sruts.xml中配置一下,如下:
result-type屬性可以在struts-default.xml中找到: