先看GetRequestParameterAction类代码:
- publicclassGetRequestParameterActionextendsActionSupport{
- privateStringbookName;
- privateStringbookPrice;
- publicStringgetBookName(){
- returnbookName;
- }
- publicvoidsetBookName(StringbookName){
- this.bookName=bookName;
- }
- publicStringgetBookPrice(){
- returnbookPrice;
- }
- publicvoidsetBookPrice(StringbookPrice){
- this.bookPrice=bookPrice;
- }
- publicStringexecute()throwsException{
- //方式一:将参数作为Action的类属性,让OGNL自动填充
- System.out.println("方法一,把参数作为Action的类属性,让OGNL自动填充:");
- System.out.println("bookName:"+this.bookName);
- System.out.println("bookPrice:"+this.bookPrice);
- //方法二:在Action中使用ActionContext得到parameterMap获取参数:
- ActionContextcontext=ActionContext.getContext();
- MapparameterMap=context.getParameters();
- StringbookName2[]=(String[])parameterMap.get("bookName");
- StringbookPrice2[]=(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获取参数
- HttpServletRequestrequest=(HttpServletRequest)context.get(ServletActionContext.HTTP_REQUEST);
- StringbookName=request.getParameter("bookName");
- StringbookPrice=request.getParameter("bookPrice");
- System.out.println("方法三,在Action中取得HttpServletRequest对象,使用request.getParameter获取参数:");
- System.out.println("bookName:"+bookName);
- System.out.println("bookPrice:"+bookPrice);
- returnSUCCESS;
- }
- }
总结:
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:
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;
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:
01.
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02.
<!DOCTYPEstrutsPUBLIC
03.
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
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.
<!--Addpackageshere-->
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:
01.
package
cn.edu.ahau.mgc.struts2.mode;
02.
03.
public
class
User{
04.
05.
private
StringuserName;
06.
private
Stringpassword;
07.
08.
public
StringgetUserName(){
09.
return
this
.userName;
10.
}
11.
12.
public
void
setUserName(StringuserName){
13.
this
.userName=userName;
14.
}
15.
16.
public
StringgetPassword(){
17.
return
this
.password;
18.
}
19.
20.
public
void
setPassword(Stringpassword){
21.
this
.password=password;
22.
}
23.
}
UserAction1.java:
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
StringuserName;
08.
private
Stringpassword;
09.
10.
public
Stringadd(){
11.
System.out.println(
"userName:"
+userName);
12.
System.out.println(
"password:"
+password);
13.
return
SUCCESS;
14.
}
15.
16.
public
StringgetUserName(){
17.
return
this
.userName;
18.
}
19.
20.
public
void
setUserName(StringuserName){
21.
this
.userName=userName;
22.
}
23.
24.
public
StringgetPassword(){
25.
return
this
.password;
26.
}
27.
28.
public
void
setPassword(Stringpassword){
29.
this
.password=password;
30.
}
31.
}
UserAction2.java:
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
Useruser;
10.
11.
public
Stringadd(){
12.
System.out.println(
"userName:"
+user.getUserName());
13.
System.out.println(
"password:"
+user.getPassword());
14.
return
SUCCESS;
15.
}
16.
17.
public
UsergetUser(){
18.
return
this
.user;
19.
}
20.
21.
public
void
setUser(Useruser){
22.
this
.user=user;
23.
}
24.
25.
}
UserAction3.java:
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
Useruser=
new
User();
11.
12.
public
Stringadd(){
13.
System.out.println(
"userName:"
+user.getUserName());
14.
System.out.print(
"password:"
+user.getPassword());
15.
return
SUCCESS;
16.
}
17.
18.
public
UsergetModel(){
19.
return
this
.user;
20.
}
21.
22.
}
index.jsp:
01.
<%@pagelanguage="java"import="java.util.*"pageEncoding="GB18030"%>
02.
<%
03.
Stringpath=request.getContextPath();
04.
StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
05.
%>
06.
07.
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//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
=
"Thisismypage"
>
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:
01.
<%@pagelanguage="java"import="java.util.*"pageEncoding="GB18030"%>
02.
<%
03.
Stringpath=request.getContextPath();
04.
StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
05.
%>
06.
07.
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//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
=
"Thisismypage"
>
18.
<!--
19.
<
link
rel
=
"stylesheet"
type
=
"text/css"
href
=
"styles.css"
>
20.
-->
21.
</
head
>
22.
23.
<
body
>
24.
UserAddSuccess!<
br
>
25.
</
body
>
26.
</
html
>