Struts2 属性驱动

需求:模拟用户注册,接收注册表单参数

1 属性驱动-普通属性驱动

1.1 设计一个用户注册表单

<h3>属性驱动-普通属性驱动</h3>
<form action="demo1.action" method="post">
  用户名:<input type="text" name="userName"/><br/>
  密码:<input type="password" name="userPwd"/><br/>
  邮箱:<input type="text" name="email"/><br/>
  <input type="submit" value="注册"/>
</form>

1.2 编写Action,接收表单参数

package com.yiidian.action;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 普通属性驱动
 * @author 一点教程(yiidian.com)
 */
public class Demo1Action extends ActionSupport{
	//提供setter方法
	private String userName;
	private String userPwd;
	private String email;
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
	@Override
	public String execute() throws Exception {
		System.out.println(userName);
		System.out.println(userPwd);
		System.out.println(email);
		return SUCCESS;
	}
	
}

注意:Action的属性名称必须和表单的name名称一致!

1.3 struts.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">	
<struts>
	<package name="base" extends="struts-default" namespace="/">
		<action name="demo1" class="com.yiidian.action.Demo1Action">
			<result>/succ.jsp</result>
		</action>		
	</package>
</struts>

1.4 访问测试

http://localhost:8080/ch02_04_struts2_parameter/index.jsp

2 属性驱动-JavaBean属性驱动

2.1 设计注册表单

<h3>JavaBean属性驱动</h3>
<form action="demo2.action" method="post">
  用户名:<input type="text" name="user.userName"/><br/>
  密码:<input type="password" name="user.userPwd"/><br/>
  邮箱:<input type="text" name="user.email"/><br/>
  <input type="submit" value="注册"/>
</form>

2.2 定义JavaBean,接收参数

package com.yiidian.domain;
import java.io.Serializable;
/**
 * @author 一点教程(yiidian.com)
 */
public class User implements Serializable{
	private String userName;
	private String userPwd;
	private String email;
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserPwd() {
		return userPwd;
	}
	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	@Override
	public String toString() {
		return "User [userName=" + userName + ", userPwd=" + userPwd
				+ ", email=" + email + "]";
	}
}

2.3 编写Action,接收参数

package com.yiidian.action;
import com.opensymphony.xwork2.ActionSupport;
import com.yiidian.domain.User;
/**
 * JavaBean属性驱动
 * @author 一点教程(yiidian.com)
 */
public class Demo2Action extends ActionSupport{
	private User user; //该对象用于接收表单的参数
	//必须提供getter和setter方法
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}	
	@Override
	public String execute() throws Exception {
		System.out.println(user);
		return SUCCESS;
	}
}

2.4 struts.xml配置

<action name="demo2" class="com.yiidian.action.Demo2Action">
	<result>/succ.jsp</result>
</action>

2.5 访问测试

 

源码下载:https://pan.baidu.com/s/1ylz6DAAeiG-FBA2BJTxuWQ

热门文章

优秀文章