首页 > 代码库 > Struts2表单数据接收方式

Struts2表单数据接收方式

1.将Action类作为一个POJO,直接进行接收
Action类内部定义字段名称,并为之声明get/set方法。页面表单name属性与定义字段名称相同。

package com.common.login;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class LoginAction extends ActionSupport{

	private String name;
	private String password;
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	public void showInfo()
	{
		System.out.println("name:"+this.name+"  password"+this.password);
	}
	
}

jsp页面代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Page</title>
</head>
<body>
	<form action="/struts_web/common/login!login" method="post">
		<s:textfield name="name" label="name"></s:textfield>
		<s:password name="password" label="password"></s:password>
		<s:submit label="submit"></s:submit>
	</form>
</body>
</html>

2.定义一个Bean对象,通过Bean进行传递接收

上述方法是最常用的一种方式,但是这个方法有个缺点:当属性较多时,Action类显得比较臃肿。

第二种方法需要我们定义一个DTO对象(普通的JavaBean

package com.common.login;

public class UserBean {
	
	private String name;
	private String password;
	
	public void setName(String name)
	{
		this.name = name;
	}
	
	public String getName()
	{
		return name;
	}
	
	public void setPassword(String password)
	{
		this.password = password;
	}
	
	public String getPassword()
	{
		return password;
	}
	
}

然后将该DTO对象组合到Action中,声明相应的get/set方法

package com.common.login;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class LoginAction extends ActionSupport{

	private UserBean bean;
	
	public UserBean getBean() {
		return bean;
	}

	public void setBean(UserBean bean) {
		this.bean = bean;
	}
	
	public void showInfo()
	{
		System.out.println("name:"+bean.getName()+"  password"+bean.getPassword());
	}
	
}

此时页面应该修改为

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Page</title>
</head>
<body>
	<form action="/struts_web/common/login!login" method="post">
		<s:textfield name="bean.name" label="name"></s:textfield>
		<s:password name="bean.password" label="password"></s:password>
		<s:submit label="submit"></s:submit>
	</form>
</body>
</html>

3.使用Struts2ModelDriven模式进行传递

第二种方式提供了一个比较简洁的表单数据接收方式,但是Action类是变得简洁了,但是页面变得相对复杂了许多。

下面提供一种ModelDriven模式的解决方案

使用ModelDriven方式,首先要实现ModelDriven接口和getModel方法

package com.common.login;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings("serial")
public class LoginAction extends ActionSupport implements ModelDriven<UserBean>{

	private UserBean bean;
	
	@Override
	public UserBean getModel() {
		if(bean==null)
		{
			bean = new UserBean();
		}
		return bean;
	}
	public void showInfo()
	{
		System.out.println("name:"+bean.getName()+"  password"+bean.getPassword());
	}
}<span style="font-size:18px;">
</span>

页面就可以改回一开始的处理方式

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Page</title>
</head>
<body>
	<form action="/struts_web/common/login!login" method="post">
		<s:textfield name="name" label="name"></s:textfield>
		<s:password name="password" label="password"></s:password>
		<s:submit label="submit"></s:submit>
	</form>
</body>
</html><span style="font-size:18px;">
</span>

注意:当返回result前对业务对象修改了,但是值栈中对应的对象依然是之前的就值,若想在返回result之前刷新值栈中业务对象各个属性的根对象,需在该action中配置如下参数:

<action name="login" class="com.common.login.LoginAction">
			<interceptor-ref name="defaultStack">
				<param name="modelDriven.refreshModelBeforeResult">true</param>
			</interceptor-ref>
			<result name="login">/pages/common/login/login.jsp</result>
			<result name="success">/pages/common/login/success.jsp</result>
			<result name="error">/pages/common/login/error.jsp</result>
		</action>