struts2属性驱动与模型驱动
程序员文章站
2022-07-12 16:14:00
...
1. struts2属性驱动与模型驱动
1) 属性驱动(FieldDriven)
1.1) 基本数据类型属性
1.2) JavaBean类型属性
2) 模型驱动(ModelDriven)
user.java package com.andrew.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; } } UserService.java package com.andrew.service; import com.andrew.model.User; public class UserService { public boolean login(User user) { if ("andrew".equals(user.getUserName()) && "123456".equals(user.getPassword())) { return true; } else { return false; } } } success.jsp 登录成功! error.jsp 登录失败!
1) 属性驱动(FieldDriven)
1.1) 基本数据类型属性
UserAction.java package com.andrew.action; import com.andrew.model.User; import com.andrew.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("执行了UserAction的默认方法"); 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.andrew.action.UserAction"> <result name="success">success.jsp</result> <result name="error">error.jsp</result> </action> login.jsp <form action="user" method="post"> 用户名:<input type="text" name="userName"/> 密码:<input type="text" name="password"/> <input type="submit" value="登录"/> </form> http://localhost:8080/HeadFirstStruts2Chap02/login.jsp abc 123 submit 运行结果: 登录失败! 控制台: 执行了UserAction的默认方法 andrew 123456 submit 运行结果: 登录成功! 控制台: 执行了UserAction的默认方法
1.2) JavaBean类型属性
UserAction2.java package com.andrew.action; import com.andrew.model.User; import com.andrew.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("执行了UserAction2的默认方法"); if (userService.login(user)) { return SUCCESS; } else { return ERROR; } } } struts.xml <action name="user2" class="com.andrew.action.UserAction2"> <result name="success">success.jsp</result> <result name="error">error.jsp</result> </action> login2.jsp <form action="user2" method="post"> 用户名:<input type="text" name="user.userName"/> 密码:<input type="text" name="user.password"/> <input type="submit" value="登录"/> </form> http://localhost:8080/HeadFirstStruts2Chap02/login2.jsp abc 123 submit 运行结果: 登录失败! 控制台: 执行了UserAction2的默认方法 andrew 123456 submit 运行结果: 登录成功! 控制台: 执行了UserAction2的默认方法
2) 模型驱动(ModelDriven)
现实接口 implements ModelDriven<xxx> UserAction3.java package com.andrew.action; import com.andrew.model.User; import com.andrew.service.UserService; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class UserAction3 extends ActionSupport implements ModelDriven<User> { private UserService userService = new UserService(); private User user = new User(); @Override public String execute() throws Exception { System.out.println("执行了UserAction3的默认方法"); if (userService.login(user)) { return SUCCESS; } else { return ERROR; } } public User getModel() { return user; } } struts.xml <action name="user3" class="com.andrew.action.UserAction3"> <result name="success">success.jsp</result> <result name="error">error.jsp</result> </action> login3.jsp <form action="user3" method="post"> 用户名:<input type="text" name="userName"/> 密码:<input type="text" name="password"/> <input type="submit" value="登录"/> </form> http://localhost:8080/HeadFirstStruts2Chap02/login3.jsp abc 123 submit 运行结果: 登录失败! 控制台: 执行了UserAction3的默认方法 andrew 123456 submit 运行结果: 登录成功! 控制台: 执行了UserAction3的默认方法
下一篇: struts2简介
推荐阅读
-
浅浅的叙WPF之数据驱动与命令
-
Struts2_属性驱动
-
Struts2中接收表单数据的三种驱动方式
-
Struts2(二)— Result结果配置、Servlet的API的访问、模型驱动、属性驱动
-
asp.net core系列 23 EF模型配置(概述, 类型和属性的包含与排除)
-
RX480与GTX1060哪个值得买?GTX 1060和RX 480显卡驱动优化对比评测
-
MongoDB的PHP驱动方法与技巧
-
架构视角 - DDD、TDD、MDD领域驱动、测试驱动还是模型驱动?
-
Redis中事件驱动模型示例详解
-
ThinkPad 安装 Ubuntu 18.10 系统 -- 高分屏各项配置与Nvdia独显驱动