Action设置数据

本文介绍了Struts2中Action设置数据的两种主要方式:属性驱动(FieldDriven)和模型驱动(ModelDriven)。属性驱动包括基本数据类型属性和JavaBean类型属性的设置;模型驱动则通过实现ModelDriven接口,以公共User和UserService来举例说明,包括基本数据类型和JavaBean类型的使用。尽管模型驱动具有灵活性限制,但在特定场景下仍有其价值。
摘要由CSDN通过智能技术生成

Action设置数据的两种方式;

第一种:属性驱动(FieldDriven)  1、基本数据类型属性  2、JavaBean类型属性

第二种:模型驱动(ModelDriven)

公共User和UserService

package model;

public class User {

	private String userName;
	private String password;
	
	public User() {
		super();
	}
	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;
	}

}
package service;

import model.User;

/**
 * 业务层
 * @author My World
 *
 */
public class UserService {

	public boolean Login(User user) {
		if ("zhangsan".equals(user.getUserName())&&"000000".equals(user.getPassword())) {
			return true;
		}else{
			return false;
		}	
	}
}

1、基本数据类型属性

前端页面;

<%@ 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>
	<h1>Login</h1>
	<form action="login" method="post">
		UserName:<input type="text" name="userName"/><br/>
		Password:<input type="password" name="password"/><br/>
		<input type="submit" value="登录"/>
	</form>
</body>
</html>

srtuts.xml的配置 

 <action name="login" class="action.UserAction">
           <result name="success">hello.jsp</result>
           <result name="error">login.jsp</result>
       </action>

 UserAction.java

package action;

import com.opensymphony.xwork2.ActionSupport;

import model.User;
import service.UserService;

public class UserAction extends ActionSupport {

	private UserService userService;
	private String userName;
	private String password;
	

	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;
	}
	@Override
	public String execute() throws Exception {
		userService = new UserService();
		User user = new User();
		user.setUserName(userName);
		user.setPassword(password);
		return userService.Login(user)?SUCCESS:ERROR;
	}
}

 2、javaBean类型属性

<action name="login2" class="action.UserAction">
           <result name="success">hello.jsp</result>
           <result name="error">login2.jsp</result>
       </action>
<%@ 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>
	<h1>Login</h1>
	<form action="login2" method="post">
		UserName:<input type="text" name="user.userName"/><br/>
		Password:<input type="password" name="user.password"/><br/>
		<input type="submit" value="登录"/>
	</form>
</body>
</html>

 

package action;

import com.opensymphony.xwork2.ActionSupport;

import model.User;
import service.UserService;

public class UserAction extends ActionSupport {

	private UserService userService;
	private User user;

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

	@Override
	public String execute() throws Exception {
		userService = new UserService();
		return userService.Login(user)?SUCCESS:ERROR;
	}

}

第二种:模型驱动

<%@ 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>
	<h1>Login</h1>
	<form action="login3" method="post">
		UserName:<input type="text" name="userName"/><br/>
		Password:<input type="password" name="password"/><br/>
		<input type="submit" value="登录"/>
	</form>
</body>
</html>
<action name="login3" class="action.UserAction3">
           <result name="success">hello.jsp</result>
           <result name="error">login3.jsp</result>
       </action>
package action;

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

import model.User;
import service.UserService;

public class UserAction3 extends ActionSupport implements ModelDriven<User>{

	private UserService userService;
	private User user = new User();

	@Override
	public String execute() throws Exception {
		userService = new UserService();
		return userService.Login(user)?SUCCESS:ERROR;
	}

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

使用第二种需要implements ModelDriven<实体>,但是有一个缺陷,这个类只能接受一种数据类型,所以项目中很少用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值