Struts2 action的参数传递方式

2016年6月19日,今天学习Struts2 action的参数传递

主要有3种传输方式:
1、用action属性接收
2、用domain model或者dto(data transcation object)
3、实现modelDriven

第一种传输方式:直接在action里set参数变量


index页面:
<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//在head中<base href>指定basePath
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href=<%= basePath %> />
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
使用Domain Model接收参数
<a href="user/User_add?name=a&age=8">添加用户</a>
</body>
</html>

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>
	 -->
	 <!--打开dmi  -->
	 <constant name="struts.enable.DynamicMethodInvocation" value="true" /> 
	 <constant name="struts.devMode" value="true" />
	<package name="user" extends="struts-default" namespace="/user">
		<action name="*_*" class="com.struts.action.{1}Action" method="{2}" >
			<result>/{1}_{2}_success.jsp</result>
		</action>
	</package>
</struts>
action类:
package com.struts.action;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UserAction extends ActionSupport{
	private String name;
	private int age;
	
	public String add(){
		System.out.println("name="+name);
		System.out.println("age="+age);
		return SUCCESS;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}

第二种传输方式:通过Domain Model(域模型)接收参数或者dto



index写法:

action写法:

model层:

可以通过vo(value object)或do或dto(data transcation object)

这种传输方式的流程是这样的:



这种方式目前还不太理解,等在后面学习到的时候再补充吧。他的大概意思是增加dto层:



通过dto层来接收参数,然后再将使用到的参数通过dto传到domain model层。

第三种传输方式是action实现ModelDriven接口

package com.bjsxt.struts2.user.action;

import com.bjsxt.struts2.user.model.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven<User>{
	
	private User user = new User();
	
	public String add() {
		System.out.println("name=" + user.getName());
		System.out.println("age=" + user.getAge());
		return SUCCESS;
	}

	@Override
	public User getModel() {
		return user;
	}
	
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值