struts2的Action从页面获取传递的参数的三种方法

1.直接在Action中获取页面传递的参数

UserAction.java

import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	
	private String username;
	private String password;
	private int age;
	private Date birthday;
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getAge() {
		return age;
	}

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

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String register(){
		System.out.println("UserAction register......");
		System.out.println("username "+username);
		System.out.println("password "+password);
		System.out.println("age "+age);
		System.out.println("birthday "+birthday);
		return SUCCESS;
	}
}


struts.xml






	
    
    
 	
 	
 	 	
 	 		index.jsp
 	 	
 	 	
    


index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>




获取表单



	

获取页面参数

帐号:
密码:
年龄:
生日:


2.使用javaEeen进行封装之后获取页面传递的参数

User.java

import java.util.Date;

public class User {

	private String username;
	private String password;
	private int age;
	private Date birthday;
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getAge() {
		return age;
	}

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

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

}


Action.java
import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	
	private User user;
	
	public User getUser() {
		return user;
	}

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

	public String register(){
		System.out.println("username "+user.getUsername());
		System.out.println("password "+user.getPassword());
		System.out.println("age "+user.getAge());
		System.out.println("birthday "+user.getBirthday());
		return SUCCESS;
	}
}
注意:不管有没有初始化user,都必须要getUser方法。因为初始化user后会创建一个User的实例,会把页面传递的参数通过getUser方法放到个实例中;如果没有初始化user,并没有getUser方法方法,则每传递一个参数都会通过
setUser创建一个User的实例,会导致参数丢失;如果没有初始化user,并且有getUser方法方法,
则传递第一个参数时会通过setUser创建一个User实例,之后传递的参数时getUser会获取传递第一个参数时创建的User实例。


有初始化,有getUser方法


无初始化,有getUser方法


无初始化,无getUser方法

struts.xml






	
    
    
 	
 	
 	 	
 	 		index.jsp
 	 	
 	 	
    

index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>




获取表单



	

获取页面参数

帐号:
密码:
年龄:
生日:

注意:input的name的属性名一定要是"在Action中User的变量名.User中相应的变量名"

3.在第二种方法的基础上使用ModelDriven接口

User.java和struts.xml与第二种方法的一样


Action.java

import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven{
	
	private User user;
	
	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		this.user = new User();
		return this.user;
	}

	public String register(){
		System.out.println("username "+user.getUsername());
		System.out.println("password "+user.getPassword());
		System.out.println("age "+user.getAge());
		System.out.println("birthday "+user.getBirthday());
		return SUCCESS;
	}
}


index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>




获取表单



	

获取页面参数

帐号:
密码:
年龄:
生日:


注意:input的name的属性名直接是"User中相应的变量名"就行,因为ModelDriven接口会通过getModel()方法把页面中传递的参数赋给user中,但是必须在getModel()方法中实例化user

你可能感兴趣的:(struts2)