`
hwy1782
  • 浏览: 150462 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Struts2中Action接收参数的方法

    博客分类:
  • web
阅读更多

 

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:

<?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>
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics