struts2业务控制类(Action)获取页面请求参数的几种方式
来源:互联网 发布:开淘宝店到底怎么样 编辑:程序博客网 时间:2024/05/18 09:39
- 通过模型驱动的方式
- 在Action里面设置参数
- 在Action里面设置具体的对象属性
1.通过模型驱动的方式
这种方式是webwork最初的几个版本所使用的,通过模型驱动的方式来收集请求中的参数,利用ModelDrivenInterceptor拦截器进行参数注入,有点类似struts1通过ActionForm表单来收集参数的方式。
实现方式:实现Action和ModelDriven接口重写getModel方法,在Action业务控制类里面设置相应的对象属性即可,前台form的参数name值设置为对象的属性即可。
action业务控制类:
package com.lyu.struts.sysmanage.action;import com.lyu.struts.sysmanage.entity.User;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ModelDriven;public class LoginAction implements Action, ModelDriven<User> { // 这里一定要手动实例化user对象 private User user = new User(); @Override public User getModel() { return user; } public String execute() { System.out.println(user.getUserName()); System.out.println(user.getPassword()); if (user.getUserName().equals("admin")) { return "success"; } else { return "fail"; } }}
struts.xml文件配置:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <package name="loginPackage" namespace="/" extends="struts-default"> <action name="login" class="com.lyu.struts.sysmanage.action.LoginAction" method="execute"> <result name="success" type="dispatcher">/main.jsp</result> <result name="fail" type="dispatcher">/error.jsp</result> </action> </package></struts>
前台页面:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><%String path = request.getContextPath();%><!doctype html><html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> <meta name="Keywords" content=""> <meta name="Description" content=""></head><body> <h2>登录页面</h2> <form action="<%=path%>/login.action" method="post"> 用户名:<input type="text" name="userName" /><br/> 密码:<input type="password" name="password"><br/> <input type="submit" value="登录" /> </form></body></html>
这种模型驱动的方式用的比较少,因为有侵入性(本身项目代码需要依赖别的框架的代码),一般用第二种方式。
2.在Action里面设置参数
直接在action业务控制类里面设置与前台的参数名相匹配的属性,以及setter,getter方法,前台的参数名和后台的属性名一致,这种方式是利用ParameteresInterceptor拦截器实现参数注入的。
action业务控制类:
package com.lyu.struts.sysmanage.action;public class LoginSecondAction { 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; } public String execute() { System.out.println(userName); System.out.println(password); if (userName.equals("admin")) { return "success"; } else { return "fail"; } }}
前台页面与上一种模型驱动的方式一样:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><%String path = request.getContextPath();%><!doctype html><html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> <meta name="Keywords" content=""> <meta name="Description" content=""></head><body> <h2>登录页面</h2> <form action="<%=path%>/login.action" method="post"> 用户名:<input type="text" name="userName" /><br/> 密码:<input type="password" name="password"><br/> <input type="submit" value="登录" /> </form></body></html>
这种方式比较常用
3.在Action里面设置具体的对象属性
和模型驱动类似,不用实现任何接口,继承任何类,在action里面仍然设置对象属性,不过前台的参数名要加上action里面的对象属性,例如:user.userName才能匹配action里面的user对象里面的userName值。
action业务控制类配置:
package com.lyu.struts.sysmanage.action;import com.lyu.struts.sysmanage.entity.User;public class LoginThirdAction { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String execute() { System.out.println(user.getUserName()); System.out.println(user.getPassword()); if (user.getUserName().equals("admin")) { return "success"; } else { return "fail"; } }}
前台页面编写:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><%String path = request.getContextPath();%><!doctype html><html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> <meta name="Keywords" content=""> <meta name="Description" content=""></head><body> <h2>登录页面</h2> <form action="<%=path%>/login.action" method="post"> 用户名:<input type="text" name="user.userName" /><br/> 密码:<input type="password" name="user.password"><br/> <input type="submit" value="登录" /> </form></body></html>
总结:第一种从webwork继承而来,第二种比较常见,第三种也可以。
阅读全文
0 0
- struts2业务控制类(Action)获取页面请求参数的几种方式
- struts2中action业务控制类的几种配置方式
- struts2中 Action获取表单参数的几种方式
- struts2中 Action获取表单参数的几种方式
- struts2中 Action获取表单参数的几种方式
- struts2中 Action获取表单参数的几种方式
- struts2的action从request获取参数值的几种方式
- struts2的action从request获取参数值的几种方式【转】
- struts2 的Action获取表单的几种方式
- struts2 action获取jsp参数的三种方式
- struts2——文件的下载以及在Action中获取请求参数的方式
- spring mvc获取请求的参数的几种方式
- springmvc 获取请求参数的几种方式
- springMVC 获取请求参数的几种方式
- Struts2配置action的几种方式
- 【问题】struts2中Action的请求参数获取
- 在Struts2的Action中取得请求参数值的几种方法
- 在Struts2的Action中取得请求参数值的几种方法
- React创建组件的三种方式及其区别
- UncaughtExceptionHandler线程异常终止处理
- linux中正则表达式、find、xargs、grep以及sed等命令的用法
- spring Security4 和 oauth2整合 注解+xml混合使用(替换用户名密码认证)
- 一文读懂Go的net/http标准库
- struts2业务控制类(Action)获取页面请求参数的几种方式
- Android资源文件分析
- Android自定义星星评分控件代替RatingBar的使用
- 7款经典游戏 诠释高级算法精髓
- GAN学习笔记:MMGAN:Manifold-Matching Generative Adversarial Network
- 平常心
- 【java基础】19.网络编程
- js写排序的方法。
- [Office] 常用的一些快捷键 [持续更新]