1,Struts2中get/set自动获取/设置数据
比如先在Action类里面添加一个name属性并加上get/set方法;
package com.java.action;
import com.opensymphony.xwork2.Action;
public class HelloWorld implements Action {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String execute() throws Exception {
System.out.println("执行了action的默认方法");
return SUCCESS;
}
}
然后我们在页面上再加上获取属性的代码;
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
${name} Struts2
</body>
</html>
我们来测试一下看看后台能否获取到属性:
运行项目之后:
2,ActionSupport类引入
我们可以继承自ActionSupport类,修改如下:
package com.java.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String execute() throws Exception {
System.out.println("执行了ActionSupport的默认方法");
return SUCCESS;
}
}
运行出来是同样的结果;
3,Action设置数据
属性驱动
先建立一个user实体类;
package com.java.model;
public class User {
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;
}
}
写一个简单的登录页面;
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="user" method="post">
用户名:<input type="text" name="userName"/>
密码:<input type="text" name="passWord"/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
数据访问层service;
package com.java.service;
import com.java.model.User;
public class UserService {
public boolean login(User user){
if("java".equals(user.getUserName()) && "123".equals(user.getPassWord())){
return true;
} else{
return false;
}
}
}
处理类Action;
package com.java.action;
import com.java.model.User;
import com.java.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private UserService userService=new 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 {
System.out.println("默认执行了User方法");
User user=new User();
user.setUserName(userName);
user.setPassWord(passWord);
if(userService.login(user)){
return SUCCESS;
} else{
return ERROR;
}
}
}
struts.xml配置;
<action name="user" class="com.java.action.UserAction">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>
运行之后就可以成功!
JavaBean属性
UserAction类;
package com.java.action;
import com.java.model.User;
import com.java.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction2 extends ActionSupport {
private UserService userService=new UserService();
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String execute() throws Exception {
System.out.println("默认执行了User方法");
if(userService.login(user)){
return SUCCESS;
} else{
return ERROR;
}
}
}
登录界面修改一下;
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="user2" method="post">
用户名:<input type="text" name="user.userName"/>
密码:<input type="text" name="user.passWord"/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
就可以了!
欢迎大家关注我的微信公众号《程序员的时光》,非常感谢!
网友评论