Struts2中Action接收参数的方法主要有以下三种:
1.使用Action的属性接收参数:
   a.定义:在Action类中定义属性,创建get和set方法;
   b.接收:通过属性接收参数,如:userName;
   c.发送:使用属性名传递参数,如:user1!add?userName=Magci;
2.使用DomainModel接收参数:
   a.定义:定义Model类,在Action中定义Model类的对象(不需要new),创建该对象的get和set方法;
   b.接收:通过对象的属性接收参数,如:user.getUserName();
   c.发送:使用对象的属性传递参数,如:user2!add?user.userName=MGC;
3.使用ModelDriven接收参数:
   a.定义:Action实现ModelDriven泛型接口,定义Model类的对象(必须new),通过getModel方法返回该对象;
   b.接收:通过对象的属性接收参数,如:user.getUserName();
   c.发送:直接使用属性名传递参数,如:user2!add?userName=MGC;

实例:

web.xml:


Java代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <web-app version="2.5"

  3.    xmlns="http://java.sun.com/xml/ns/javaee"

  4.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  5.    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  

  6.    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  7.  <welcome-file-list>  

  8.    <welcome-file>hello.jsp</welcome-file>  

  9.  </welcome-file-list>  

  10.  <filter>  

  11.    <filter-name>struts2</filter-name>  

  12.    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  

  13.  </filter>  

  14.  <filter-mapping>  

  15.    <filter-name>struts2</filter-name>  

  16.    <url-pattern>/*</url-pattern>  

  17.  </filter-mapping>  

  18. </web-app>  

[java]view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <web-app version="2.5"

  3.    xmlns="http://java.sun.com/xml/ns/javaee"

  4.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  5.    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

  6.    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  7.  <welcome-file-list>  

  8.    <welcome-file>hello.jsp</welcome-file>  

  9.  </welcome-file-list>  

  10.  <filter>  

  11.    <filter-name>struts2</filter-name>  

  12.    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  

  13.  </filter>  

  14.  <filter-mapping>  

  15.    <filter-name>struts2</filter-name>  

  16.    <url-pattern>/*</url-pattern>  

  17.  </filter-mapping>  

  18. </web-app>  


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.0.dtd">

<struts>
<!--
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />

<include file="example.xml"/>



<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package>
-->

<!-- Add packages here -->
<constant name="struts.devMode" value="true" />
<package name="user" namespace="/" extends="struts-default">
<action name="user*" class="cn.edu.ahau.mgc.struts2.action.UserAction{1}">
<result>/addSuccess.jsp</result>
</action>
</package>
</struts>


User.java:


Java代码 复制代码
  1. package cn.edu.ahau.mgc.struts2.mode;  

  2. publicclass User {  

  3. private String userName;  

  4. private String password;  

  5. public String getUserName() {  

  6. returnthis.userName;  

  7.    }  

  8. publicvoid setUserName(String userName) {  

  9. this.userName = userName;  

  10.    }  

  11. public String getPassword() {  

  12. returnthis.password;  

  13.    }  

  14. publicvoid setPassword(String password) {  

  15. this.password = password;  

  16.    }  

  17. }  

[java]view plaincopy
  1. package cn.edu.ahau.mgc.struts2.mode;  

  2. publicclass User {  

  3. private String userName;  

  4. private String password;  

  5. public String getUserName() {  

  6. returnthis.userName;  

  7.    }  

  8. publicvoid setUserName(String userName) {  

  9. this.userName = userName;  

  10.    }  

  11. public String getPassword() {  

  12. returnthis.password;  

  13.    }  

  14. publicvoid setPassword(String password) {  

  15. this.password = password;  

  16.    }  

  17. }  


UserAction1.java:


写道
package cn.edu.ahau.mgc.struts2.action;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction1 extends ActionSupport {

private String userName;
private String password;

public String add() {
System.out.println("userName: " + userName);
System.out.println("password: " + password);
return SUCCESS;
}

public String getUserName() {
return this.userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPassword() {
return this.password;
}

public void setPassword(String password) {
this.password = password;
}
}


UserAction2.java:



写道
package cn.edu.ahau.mgc.struts2.action;

import com.opensymphony.xwork2.ActionSupport;

import cn.edu.ahau.mgc.struts2.mode.User;

public class UserAction2 extends ActionSupport {

private User user;

public String add() {
System.out.println("userName: " + user.getUserName());
System.out.println("password: " + user.getPassword());
return SUCCESS;
}

public User getUser() {
return this.user;
}

public void setUser(User user) {
this.user = user;
}

}


UserAction3.java:



写道
package cn.edu.ahau.mgc.struts2.action;

import cn.edu.ahau.mgc.struts2.mode.User;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction3 extends ActionSupport implements ModelDriven<User> {

private User user = new User();

public String add() {
System.out.println("userName: " + user.getUserName());
System.out.print("password: " + user.getPassword());
return SUCCESS;
}

public User getModel() {
return this.user;
}

}


index.jsp:


写道
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>Param</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
<a href="user1!add?userName=Magci&password=123456">user1!add?userName=Magci&password=123456</a>
<br />
<br />

<a href="user2!add?user.userName=MGC&user.password=abc">user2!add?user.userName=MGC&user.password=abc</a>
<br />
<br />

<a href="user3!add?userName=MaGC&password=000000">user3!add?userName=MaGC&password=000000</a>
</body>
</html>

addSuccess.jsp:


Java代码 复制代码
  1. <%@ page language="java"import="java.util.*" pageEncoding="GB18030"%>  

  2. <%  

  3. String path = request.getContextPath();  

  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  

  5. %>  

  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  

  7. <html>  

  8.  <head>  

  9.    <base href="<%=basePath%>">  

  10.    <title>AddSuccess</title>  

  11.    <meta http-equiv="pragma" content="no-cache">  

  12.    <meta http-equiv="cache-control" content="no-cache">  

  13.    <meta http-equiv="expires" content="0">      

  14.    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  

  15.    <meta http-equiv="description" content="This is my page">  

  16.    <!--  

  17.    <link rel="stylesheet" type="text/css" href="styles.css">  

  18.    -->  

  19.  </head>  

  20.  <body>  

  21.    User Add Success! <br>  

  22.  </body>  

  23. </html>