先看GetRequestParameterAction类代码:
public class GetRequestParameterAction extends ActionSupport {
private String bookName;
private String bookPrice;
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookPrice() {
return bookPrice;
}
public void setBookPrice(String bookPrice) {
this.bookPrice = bookPrice;
}
public String execute() throws Exception{
//方式一: 将参数作为Action的类属性,让OGNL自动填充
System.out.println("方法一,把参数作为Action的类属性,让OGNL自动填充:");
System.out.println("bookName: "+this.bookName);
System.out.println("bookPrice: " +this.bookPrice);
//方法二:在Action中使用ActionContext得到parameterMap获取参数:
ActionContext context=ActionContext.getContext();
Map parameterMap=context.getParameters();
String bookName2[]=(String[])parameterMap.get("bookName");
String bookPrice2[]=(String[])parameterMap.get("bookPrice");
System.out.println("方法二,在Action中使用ActionContext得到parameterMap获取参数:");
System.out.println("bookName: " +bookName2[0]);
System.out.println("bookPrice: " +bookPrice2[0]);
//方法三:在Action中取得HttpServletRequest对象,使用request.getParameter获取参数
HttpServletRequest request = (HttpServletRequest)context.get(ServletActionContext.HTTP_REQUEST);
String bookName=request.getParameter("bookName");
String bookPrice=request.getParameter("bookPrice");
System.out.println("方法三,在Action中取得HttpServletRequest对象,使用request.getParameter获取参数:");
System.out.println("bookName: " +bookName);
System.out.println("bookPrice: " +bookPrice);
return SUCCESS;
}
}
总结:
Struts2中Action接收参数的方法主要有以下三种:方法一:当把参数作为Action的类属性,且提供属性的getter/setter方法时,xwork的OGNL会自动把request参数的值设置到类属性中,此时访问请求参数只需要访问类属性即可。
方法二:可以通过ActionContext对象Map parameterMap=context.getParameters();方法,得到请求参数Map,然后通过parameterMap来获取请求参数。需要注意的是:当通过parameterMap的键取得参数值时,取得是一个数组对象,即同名参数的值的集合。
方法三:通过ActionContext取得HttpServletRequest对象,然后使用request.getParameter("参数名")得到参数值。
1.使用Action的属性接收参数:
a.定义:在Action类中定义属性,创建get和set方法;2.使用DomainModel接收参数:
b.接收:通过属性接收参数,如:userName;
c.发送:使用属性名传递参数,如:user1!add?userName=Magci;
a.定义:定义Model类,在Action中定义Model类的对象(不需要new),创建该对象的get和set方法;3.使用ModelDriven接收参数:
b.接收:通过对象的属性接收参数,如:user.getUserName();
c.发送:使用对象的属性传递参数,如:user2!add?user.userName=MGC;
a.定义:Action实现ModelDriven泛型接口,定义Model类的对象(必须new),通过getModel方法返回该对象;实例:
b.接收:通过对象的属性接收参数,如:user.getUserName();
c.发送:直接使用属性名传递参数,如:user2!add?userName=MGC;
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>hello.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</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:
package cn.edu.ahau.mgc.struts2.mode;
public class User {
private String userName;
private String password;
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;
}
}
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:
<%@ 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>AddSuccess</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>
User Add Success! <br>
</body>
</html>
参考链接: