struts2的静态参数封装及3种动态参数封装方法
2018-04-10 11:27
337 查看
1.静态参数封装用到了相应的拦截器,在struts-default.xml中查找<interceptor-ref name="staticParams"/>对应的jsp[html] view plain copy<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配置[html] view plain copy<action name="action1" class="com.itheima.web.action.Demo1Action" method="addUser">
<!-- 当我们不写任何拦截器时,默认的拦截器栈defaultStack它来为我们工作。
但是,只要写了任何一个拦截器,默认的就全都不起作用了 -->
<!-- 使用注入的方式,给动作类的中的参数赋值 -->
<param name="username">张三</param>
<param name="age">18</param>
</action>
对应的动作类[java] view plain copyimport 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,与静态的一致[html] view plain copy<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配置[html] view plain copy<!-- 动态参数封装的第一种形式的配置 -->
<action name="action2" class="com.itheima.web.action.Demo2Action" method="addUser"></action>
对应的动作类[java] view plain copyimport 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;
}
}
<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配置[html] view plain copy<action name="action1" class="com.itheima.web.action.Demo1Action" method="addUser">
<!-- 当我们不写任何拦截器时,默认的拦截器栈defaultStack它来为我们工作。
但是,只要写了任何一个拦截器,默认的就全都不起作用了 -->
<!-- 使用注入的方式,给动作类的中的参数赋值 -->
<param name="username">张三</param>
<param name="age">18</param>
</action>
对应的动作类[java] view plain copyimport 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,与静态的一致[html] view plain copy<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配置[html] view plain copy<!-- 动态参数封装的第一种形式的配置 -->
<action name="action2" class="com.itheima.web.action.Demo2Action" method="addUser"></action>
对应的动作类[java] view plain copyimport 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;
}
}
相关文章推荐
- struts2的静态参数封装及3种动态参数封装方法
- 【JavaWeb-16】静态参数封装、3种动态参数封装、数据类型转换、struts标签、验证器
- struts2中的Action接收表单传递过来的参数有3种方法
- Struts2学习---基本配置,action,动态方法调用,action接收参数
- struts2 模型驱动封装动态参数
- Struts2学习---基本配置,action,动态方法调用,action接收参数
- struts2接收前台传来的参数有3种方法
- Struts2动态方法调用的3种方式
- struts2的常量、动态方法调用、接受请求参数处理、类型转换
- 使用echarts类库,在后台封装option,提供两种使用struts2传递参数到页面的方法
- Struts2动态参数封装
- struts2中action接收参数的3种方法
- struts2笔记之共享数据的3种方式和请求参数封装
- asp.net 自己封装数据库操作一个类中一个自定义方法Execute(),非常实用,省去了麻烦的中间过程,动态参数
- struts2的常量、动态方法调用、接受请求参数处理、类型转换 .
- struts2-动态调用方法&接受请求参数
- Struts2动态方法调用的3种方式
- struts2中action接收参数的3种方法
- struts2中的Action接收表单传递过来参数的3种方法
- struts2中的Action接收表单传递过来的参数有3种方法