Action中接收参数的方式

在Action接收参数的方式有3种:

  1. 直接使用属性对应
  2. 使用对像接收
  3. 实现ModelDriven接口

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
	<h3>1.使用属性接收</h3>
	<form action="user/common!login" method="post">
		name:<input type="text" name="name"/>
		password:<input type="password" name="password"/>
		
		<input type="submit" value="login" />
	</form>
	
	<h3>2.使用对象接收</h3>
	<form action="user/common!login2" method="post">
		name:<input type="text" name="user.name"/>
		password:<input type="password" name="user.password"/>
		
		<input type="submit" value="login" />
	</form>
	
	<h3>2.实现ModelDriven接口</h3>
	<form action="user/common!login3" method="post">
		name:<input type="text" name="name"/>
		password:<input type="password" name="password"/>
		
		<input type="submit" value="login" />
	</form>
</body>
</html>

UserAction

package com.deppon.demo.struts;

import com.deppon.demo.entity.UserEntity;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven<UserEntity>{
	private static final long serialVersionUID = 7146977447328113550L;
	
	private String name;
	private String password;
	private UserEntity user;
	private UserEntity userModel = new UserEntity();
	
	public UserEntity getModel() {
		return userModel;
	}
	
	public UserEntity getUser() {
		return user;
	}

	public void setUser(UserEntity user) {
		this.user = user;
	}

	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;
	}

	/**
	 * 使用属性的方式,直接接收参数
	 * 当参数较多时,不方便
	 * @return
	 */
	public String login() {
		System.out.println("name->" + name + ",password->" + password);
		
		return SUCCESS;
	}
	
	/**
	 * 使用对象接收
	 * 需要加前缀,和变量名一致
	 * @return
	 */
	public String login2() {
		System.out.println("user->" + user);
		return SUCCESS;
	}
	
	/**
	 * 是想ModelDriven接口
	 * @return
	 */
	public String login3() {
		System.out.println("userModel->" + userModel);
		return SUCCESS;
	}

}

UserEntity

package com.deppon.demo.entity;

import java.io.Serializable;

public class UserEntity implements Serializable {
	private static final long serialVersionUID = -1467621300783425180L;
	
	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;
	}

	@Override
	public String toString() {
		return "UserEntity [name=" + name + ", password=" + password + "]";
	}
	
}

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.i18n.encoding" value="utf-8"></constant>
	<constant name="struts.multipart.maxSize" value="20971520"/>
    <constant name="struts.devMode" value="true" />
    
    <package name="p_user" namespace="/user" extends="struts-default">
    	<action name="common" class="com.deppon.demo.struts.UserAction">
    		<result type="redirect">/index.jsp</result>
    	</action>
    	
    </package>
</struts>

你可能感兴趣的:(struts,action,接受参数)