Struts2中Action接收参数的方法

引用:http://hwy1782.javaeye.com/blog/701904

 

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>  
<?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:

 

Java代码 复制代码
  1. package cn.edu.ahau.mgc.struts2.mode;   
  2.   
  3. public class User {   
  4.   
  5.     private String userName;   
  6.     private String password;   
  7.        
  8.     public String getUserName() {   
  9.         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. }  
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:

 

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.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
  8. <html>   
  9.   <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、付费专栏及课程。

余额充值