原帖:
http://mgc.ahau.edu.cn/article.asp?id=779
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:
view sourceprint?01.<?xml version="1.0" encoding="UTF-8"?>
02.<web-app version="2.5"
03.
xmlns="http://java.sun.com/xml/ns/javaee"
04.
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
05.
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee ;
06.
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
07.
<welcome-file-list>
08.
<welcome-file>hello.jsp</welcome-file>
09.
</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:
view sourceprint?01.<?xml version="1.0" encoding="UTF-8" ?>
02.<!DOCTYPE struts PUBLIC
03.
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
04.
"http://struts.apache.org/dtds/struts-2.0.dtd">
05.
06.<struts>
07.
<!--
08.
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
09.
<constant name="struts.devMode" value="false" />
10.
11.
<include file="example.xml"/>
12.
13.
14.
15.
<package name="default" namespace="/" extends="struts-default">
16.
<default-action-ref name="index" />
17.
<action name="index">
18.
<result type="redirectAction">
19.
<param name="actionName">HelloWorld</param>
20.
<param name="namespace">/example</param>
21.
</result>
22.
</action>
23.
</package>
24.
-->
25.
26.
<!-- Add packages here -->
27.
<constant name="struts.devMode" value="true" />
28.
<package name="user" namespace="/" extends="struts-default">
29.
<action name="user*" class="cn.edu.ahau.mgc.struts2.action.UserAction{1}">
30.
<result>/addSuccess.jsp</result>
31.
</action>
32.
</package>
33.</struts>
User.java:
view sourceprint?01.package cn.edu.ahau.mgc.struts2.mode;
02.
03.public class User {
04.
05.
private String userName;
06.
private String password;
07.
08.
public String getUserName() {
09.
return this.userName;
10.
}
11.
12.
public void setUserName(String userName) {
13.
this.userName = userName;
14.
}
15.
16.
public String getPassword() {
17.
return this.password;
18.
}
19.
20.
public void setPassword(String password) {
21.
this.password = password;
22.
}
23.}
UserAction1.java:
view sourceprint?01.package cn.edu.ahau.mgc.struts2.action;
02.
03.import com.opensymphony.xwork2.ActionSupport;
04.
05.public class UserAction1 extends ActionSupport {
06.
07.
private String userName;
08.
private String password;
09.
10.
public String add() {
11.
System.out.println("userName: " + userName);
12.
System.out.println("password: " + password);
13.
return SUCCESS;
14.
}
15.
16.
public String getUserName() {
17.
return this.userName;
18.
}
19.
20.
public void setUserName(String userName) {
21.
this.userName = userName;
22.
}
23.
24.
public String getPassword() {
25.
return this.password;
26.
}
27.
28.
public void setPassword(String password) {
29.
this.password = password;
30.
}
31.}
UserAction2.java:
view sourceprint?01.package cn.edu.ahau.mgc.struts2.action;
02.
03.import com.opensymphony.xwork2.ActionSupport;
04.
05.import cn.edu.ahau.mgc.struts2.mode.User;
06.
07.public class UserAction2 extends ActionSupport {
08.
09.
private User user;
10.
11.
public String add() {
12.
System.out.println("userName: " + user.getUserName());
13.
System.out.println("password: " + user.getPassword());
14.
return SUCCESS;
15.
}
16.
17.
public User getUser() {
18.
return this.user;
19.
}
20.
21.
public void setUser(User user) {
22.
this.user = user;
23.
}
24.
25.}
UserAction3.java:
view sourceprint?01.package cn.edu.ahau.mgc.struts2.action;
02.
03.import cn.edu.ahau.mgc.struts2.mode.User;
04.
05.import com.opensymphony.xwork2.ActionSupport;
06.import com.opensymphony.xwork2.ModelDriven;
07.
08.public class UserAction3 extends ActionSupport implements ModelDriven<User> {
09.
10.
private User user = new User();
11.
12.
public String add() {
13.
System.out.println("userName: " + user.getUserName());
14.
System.out.print("password: " + user.getPassword());
15.
return SUCCESS;
16.
}
17.
18.
public User getModel() {
19.
return this.user;
20.
}
21.
22.}
index.jsp:
view sourceprint?01.<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
02.<%
03.String path = request.getContextPath();
04.String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
05.%>
06.
07.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
08.<html>
09.
<head>
10.
<base href="<%=basePath%>">
11.
12.
<title>Param</title>
13.
<meta http-equiv="pragma" content="no-cache">
14.
<meta http-equiv="cache-control" content="no-cache">
15.
<meta http-equiv="expires" content="0">
16.
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17.
<meta http-equiv="description" content="This is my page">
18.
<!--
19.
<link rel="stylesheet" type="text/css" href="styles.css">
20.
-->
21.
</head>
22.
23.
<body>
24.
<a href="user1!add?userName=Magci&password=123456">user1!add?userName=Magci&password=123456</a>
25.
<br />
26.
<br />
27.
28.
<a href="user2!add?user.userName=MGC&user.password=abc">user2!add?user.userName=MGC&user.password=abc</a>
29.
<br />
30.
<br />
31.
32.
<a href="user3!add?userName=MaGC&password=000000">user3!add?userName=MaGC&password=000000</a>
33.
</body>
34.</html>
addSuccess.jsp:
view sourceprint?01.<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
02.<%
03.String path = request.getContextPath();
04.String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
05.%>
06.
07.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
08.<html>
09.
<head>
10.
<base href="<%=basePath%>">
11.
12.
<title>AddSuccess</title>
13.
<meta http-equiv="pragma" content="no-cache">
14.
<meta http-equiv="cache-control" content="no-cache">
15.
<meta http-equiv="expires" content="0">
16.
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17.
<meta http-equiv="description" content="This is my page">
18.
<!--
19.
<link rel="stylesheet" type="text/css" href="styles.css">
20.
-->
21.
</head>
22.
23.
<body>
24.
User Add Success! <br>
25.
</body>
26.</html>
分享到:
相关推荐
Struts2中Action接收参数的方法
NULL 博文链接:https://cdxs2.iteye.com/blog/1925245
action跳转后的页面接收由上一个ACTION传过来的参数
androind http 请求 struts2 action 返回 json 数据 例子
程序参考源代码 博文链接:https://honly-java.iteye.com/blog/1605764
利用AJAX传递JSON数组,后台struts2的action接收。 详细代码简单易懂。
NULL 博文链接:https://lipeixiaoyu.iteye.com/blog/626185
Struts2_ModelDriven接收参数(项目案例及视频) 初学者必备.
首先,Struts的ActionServlet接收到一个请求,然后根据struts-config.xml的配置定位到相应的mapping (映射);接下来如果form的范围是request或者在定义的范围中找不到这个form,创建一个新的form实例;取得form...
建立一个Struts2 工程,用Action的属性接收参数,使用Domain Model (实体模型) 接收参数,Struts2_2.1.6版本的中文问题,Struts模块包含,Struts简单数据验证 ,Struts ValueStack(值栈) Debug,Value Stack ...
主要介绍了Struts2之Action接收请求参数和拦截器详解,非常具有实用价值,需要的朋友可以参考下
主要介绍了 Struts2获取参数的三种方法总结的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
九、 Action的属性接收参数 10 1、 在Action添加成员属性接受参数 10 2、 域模型(Domain Model) 10 3、 ModelDriven接收参数 11 十、 Action属性接收参数中文问题 12 十一、 简单数据验证 12 十二、 访问Web元素 14 ...
同样适合学习者的小代码,大神勿扰,也请别喷,我个人认为我上传的东西虽然简单,但对于初学者,和刚刚接触的人还是有点用处的。
九、 Action的属性接收参数 10 1、 在Action添加成员属性接受参数 10 2、 域模型(Domain Model) 10 3、 ModelDriven接收参数 11 十、 Action属性接收参数中文问题 12 十一、 简单数据验证 12 十二、 访问Web元素 14 ...
请求参数接收 自定义类型转换器 全局类型转换器 访问或添加几个属性 文件上传 多文件上传 自定义拦截器 对Action中所有方法进行输入校验 对Action指定方法进行校验 输入校验的流程 基于XML配置方式实现对...
本文以登录验证为例,进行代码展示,下面给大家详细介绍Struts2中Action三种接收参数形式与简单的表单验证功能,需要的朋友参考下
java发送url请求进行文件的提交以及后台struts2的action接收处理 1、java模拟表单方式发送url请求进行文件的提交 /** * * @作者 王建明 * @创建日期 2013-06-27 * @创建时间 19:28:18 * @...
Struts2框架中Action搜索顺序、动态方法调用、指定多个配置文件、Struts2后缀、接收参数、处理结果类型.
1, 思路 使用一个页面控制类,它记录页面信息,如上页,下页,当前页等。在查询的Action中,将这个控制类和查询条件一起传递给...在分页控制Action中,利用接收到的分页参数调用数据库访问的bean. 2,实现 ......