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<实体>,但是有一个缺陷,这个类只能接受一种数据类型,所以项目中很少用。