Struts2中Action接收参数的方法

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 ;
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:
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:
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:
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:
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:
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:
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:
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值