struts2的静态参数封装及3种动态参数封装方法
来源:互联网 发布:阿里云邀请码获取 编辑:程序博客网 时间:2024/05/22 05:27
1.静态参数封装
用到了相应的拦截器,在struts-default.xml中查找<interceptor-ref name="staticParams"/>
对应的jsp
<body> <form action="${pageContext.request.contextPath}/action1.action" method="post"> <%--表单中的name属性取值必须和动作类中数据模型的set方法后面的名称一致。 --%> 用户名:<input type="text" name="username" /><br/> 年龄:<input type="text" name="age"/><br/> <input type="submit" value="提交" /> </form>
对应的struts配置
<action name="action1" class="com.itheima.web.action.Demo1Action" method="addUser"><!-- 当我们不写任何拦截器时,默认的拦截器栈defaultStack它来为我们工作。 但是,只要写了任何一个拦截器,默认的就全都不起作用了 --><!-- 使用注入的方式,给动作类的中的参数赋值 --><param name="username">张三</param><param name="age">18</param></action>
对应的动作类
import com.opensymphony.xwork2.ActionSupport;/** * 静态参数封装 * @author zhy * */public class Demo1Action extends ActionSupport {private String username;private int age;public String addUser(){System.out.println(username+","+age);return null;//不返回任何结果视图 NONE常量}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
2.动态参数封装
1)用到了相应的拦截器,在struts-default.xml中查找<interceptor-ref name="params">
对应的jsp,与静态的一致
<body> <form action="${pageContext.request.contextPath}/action2.action" method="post"> <%--表单中的name属性取值必须和动作类中数据模型的set方法后面的名称一致。 --%> 用户名:<input type="text" name="username" /><br/> 年龄:<input type="text" name="age"/><br/> <input type="submit" value="提交" /> </form> </body>
对应的struts配置
<!-- 动态参数封装的第一种形式的配置 --><action name="action2" class="com.itheima.web.action.Demo2Action" method="addUser"></action>
对应的动作类
import com.opensymphony.xwork2.ActionSupport;/** * 动态参数封装,第一种情况: * 数据模型与动作类写在一起 * @author zhy * */public class Demo2Action extends ActionSupport {private String username;private int age;public String addUser(){System.out.println(username+","+age);return null;//不返回任何结果视图 NONE常量}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
发现,第一类动态参数封装与静态参数封装只与静态参数封装在struts.xml文件配置有差异,其他都一致。
2)用到了相应的拦截器,在struts-default.xml中查找<interceptor-ref name="staticParams"/>
对应的jsp
<body> <form action="${pageContext.request.contextPath}/action3.action" method="post"> <%--表单中的name属性取值必须和动作类中数据模型的set方法后面的名称一致。 --%> 用户名:<input type="text" name="user.username" /><br/> 年龄:<input type="text" name="user.age"/><br/> <input type="submit" value="提交" /> </form> </body>
对应的struts配置
<!-- 动态参数封装的第二种形式的配置 --><action name="action3" class="com.itheima.web.action.Demo3Action" method="addUser"></action>
对应的动作类
import com.itheima.domain.User;import com.opensymphony.xwork2.ActionSupport;/** * 动态参数封装,第二种情况: * 数据模型与动作类分开写 * @author zhy * */public class Demo3Action extends ActionSupport {//定义数据模型对象private User user = new User();public String addUser(){System.out.println(user.getUsername()+","+user.getAge());return null;//不返回任何结果视图 NONE常量}public User getUser() {System.out.println("getUser");return user;}public void setUser(User user) {System.out.println("setUser");this.user = user;}}
此时创建了一个User类
import java.io.Serializable;public class User implements Serializable {private String username;private int age;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
从上面可以看出,本次的jsp文件发生了变化,主要是采用了User类,拦截器将不再识别username和age,需要通过user.username和user.age来进行辨别。
3)模型驱动封装动态参数(用的最为广泛的一种)用到了相应的拦截器,在struts-default.xml中查找<interceptor-ref name="modelDriven"/>
相应的jsp
<body> <form action="${pageContext.request.contextPath}/action4.action" method="post"> <%--需要注意name属性的取值,已经不是一个普通的字符串了 --%> 用户名:<input type="text" name="username" /><br/> 年龄:<input type="text" name="age"/><br/> <input type="submit" value="提交" /> </form> </body>
相应的struts配置
<!-- 动态参数封装的第三种形式的配置 --><action name="action4" class="com.itheima.web.action.Demo4Action" method="addUser"></action>
相应的动作类
import com.itheima.domain.User;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;/** * 动态参数封装,第三种情况:模型驱动 * 要想使用模型驱动,必须数据模型与动作类分开写 * 实现模型驱动的步骤: * 1.实现一个ModelDriven的接口 * 2.实现接口中的抽象方法getModel() * 3.在使用模型驱动的时候,数据模型必须由我们自己来实例化 * * 是由一个ModelDriven的拦截器为我们做的 * * 实际开发中采用的方式 * @author zhy * */public class Demo4Action extends ActionSupport implements ModelDriven<User>{//定义数据模型对象private User user = new User() ;//使用模型驱动,必须自己实例化模型对象//该方法返回的是数据模型public User getModel() {return user;}public String addUser(){System.out.println(user.getUsername()+","+user.getAge());return null;//不返回任何结果视图 NONE常量}public User getUser() {return user;}public void setUser(User user) {this.user = user;}}
这时候对比jsp,发现又跟最初一样了~
阅读全文
0 0
- struts2的静态参数封装及3种动态参数封装方法
- Struts2动态参数封装
- Struts2的参数封装
- 【JavaWeb-16】静态参数封装、3种动态参数封装、数据类型转换、struts标签、验证器
- struts2的请求参数封装
- struts2 模型驱动封装动态参数
- Struts2参数封装
- Struts2 封装请求参数
- struts2参数封装
- Struts2-参数封装
- Struts2架构中参数的封装
- Struts2如何封装请求参数
- Struts2如何封装请求参数
- Struts2如何封装请求参数
- struts2获取参数/封装对象
- struts2中自动封装参数及类型转换
- 封装多次调用的方法,参数不同
- struts2笔记之共享数据的3种方式和请求参数封装
- Python matplotlib画图表
- Google VR开发-Cardboard VR SDK头部追踪实现(隐马尔可夫模型)
- Android程序员学WEB前端(6)-CSS(1)-选择器-Sublime
- LintCode 题目之Aplusb2
- Activiti工作流框架学习(二)——使用Activiti提供的API完成流程操作
- struts2的静态参数封装及3种动态参数封装方法
- 10. Regular Expression Matching
- npm 配置淘宝镜像
- unity音频管理类
- java防止请求重复提交
- jsp九大内置对象的使用详解
- python库matplotlib中线条的颜色及样式
- JAVA的网络编程
- Vert.x 之Web Client