2018.11.21 struts2获得servletAPI方式及如何获得参数
访问servletAPI方式
第一种:通过ActionContext (重点及常用 都是获得原生对象)
原理
Action配置
被引入的配置文件
在页面调用取值
第二种:通过ServletActionContext
第三种:通过实现接口
也可以注入其他接口
在这里找
看源码
struts-default.xml文件
封装配置了20个拦截器,刚才配置的是servletConfig 拦截器
注册拦截器
进入源码的类ServletConfigInterceptor
核心方法
数据中心ActionContext
debug启动测试
如何获得参数
Action设置
servlet是线程不安全的,在运行期间只创建一个实例,
输入地址栏 http://localhost:8080/Struts2Day02/form1.jsp
获取结果
验证Action中的生命周期
结论
1.每次请求到来时,都会创建一个心的Action实例;
2.在struts2中的Action是线程安全的,能在方法之前声明接收的变量;可以使用成成员变量接受参数
第一种获取参数实例--- 属性驱动获得实例 (struts2官方推荐使用,但是现实开发不用)
记得提供属性的get/set方法
输入参数
控制台输出
第二种获取参数实例----对象驱动
声明对象
页面设置属性
控制台输出页面参数
第三种获取参数实例---模型驱动
Action配置
struts.xml 配置
页面输入
控制台输出
封装集合类型
List集合
也可以写集合下标
Map集合
点击提交会发现出现404错误
在map集合中,存储数据是以键值对的形式存在的,接下来修改参数页面
struts2中的mvc
附录
获取参数====对象驱动
package com.legend.c_param;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
/**
* 如何获得参数---方式一 属性驱动
* @author qichunlin
* action的生命周期
* 每次请求Action时都会穿件新的Action实例对象
*/
public class Demo8Action extends ActionSupport{
private static final long serialVersionUID = 1L;
//线程安全的
public Demo8Action() {
super();
System.out.println("Demo8Action被创建了");
}
//准备与参数键名称相同的属性
private String name;
//自动类型转换 只能转换8大基本数据类型以及对应包装类
private Integer age;
private Date birthday;
//支持特定类型字符串转换为Date 例如 yyyy-MM-dd
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute() throws Exception {
System.out.println("name参数值"+name+"年龄"+age+"生日"+birthday);
return SUCCESS;
}
}
获取参数====对象驱动
package com.legend.c_param;
import com.legend.domain.User;
import com.opensymphony.xwork2.ActionSupport;
/**
* 如何获得参数---方式二 对象驱动
* @author qichunlin
* action的生命周期
* 每次请求Action时都会穿件新的Action实例对象
*/
public class Demo9Action extends ActionSupport{
private static final long serialVersionUID = 1L;
//准备user对象
private User user;
//线程安全的
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Demo9Action() {
super();
System.out.println("Demo8Action被创建了");
}
//执行方法
public String execute() throws Exception {
System.out.println(user);
return SUCCESS;
}
}
获取参数===模型驱动
package com.legend.c_param;
import com.legend.domain.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
* 如何获得参数---方式三 模型驱动
* @author qichunlin
* action的生命周期
* 每次请求Action时都会穿件新的Action实例对象
*/
public class Demo10Action extends ActionSupport implements ModelDriven<User>{
private static final long serialVersionUID = 1L;
//准备user 成员变量
private User user = new User();
//执行方法
public String execute() throws Exception {
System.out.println(user);
return SUCCESS;
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
}
封装集合类型参数
package com.legend.c_param;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
/**
* 如何获得参数---封装集合类型参数
* @author qichunlin
* action的生命周期
* 每次请求Action时都会穿件新的Action实例对象
*/
public class Demo11Action extends ActionSupport {
private static final long serialVersionUID = 1L;
//list集合
private List<String> list;
//map集合
private Map<String,String> map;
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
//执行方法
public String execute() throws Exception {
System.out.println("list"+list);
System.out.println("map"+map);
return SUCCESS;
}
}
被引入的配置文件
<?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>
<!-- 是否开启开发模式
struts.enable.DynamicMethodInvocation = false
-->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<package name="param" namespace="/" extends="struts-default">
<action name="Demo8Action" class="com.legend.c_param.Demo8Action" method="execute">
<result name="success" type="dispatcher">/form1.jsp</result>
</action>
<action name="Demo9Action" class="com.legend.c_param.Demo9Action" method="execute">
<result name="success" type="dispatcher">/form2.jsp</result>
</action>
<action name="Demo10Action" class="com.legend.c_param.Demo10Action" method="execute">
<result name="success" type="dispatcher">/form3.jsp</result>
</action>
<action name="Demo11Action" class="com.legend.c_param.Demo11Action" method="execute">
<result name="success" type="dispatcher">/form4.jsp</result>
</action>
</package>
</struts>
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>
<!-- 是否开启开发模式
struts.enable.DynamicMethodInvocation = false
-->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<package name="result" namespace="/" extends="struts-default">
<!-- 转发 -->
<action name="Demo1Action" class="com.legend.action.Demo1Action" method="execute">
<result name="success" type="dispatcher">/hello.jsp</result>
</action>
<!-- 重定向 -->
<action name="Demo2Action" class="com.legend.action.Demo2Action" method="execute">
<result name="success" type="redirect">/hello.jsp</result>
</action>
<!-- 转发到action -->
<action name="Demo3Action" class="com.legend.action.Demo3Action" method="execute">
<result type="chain">
<!-- action的名字 -->
<param name="actionName">Demo1Action</param>
<!-- action所在的命名空间 -->
<param name="namespace">/</param>
</result>
</action>
<!-- 转发到action -->
<action name="Demo4Action" class="com.legend.action.Demo4Action" method="execute">
<result type="redirectAction">
<!-- action的名字 -->
<param name="actionName">Demo1Action</param>
<!-- action所在的命名空间 -->
<param name="namespace">/</param>
</result>
</action>
</package>
<!-- 引入xml文件 -->
<include file="com/legend/b_api/struts.xml"></include>
<include file="com/legend/c_param/struts.xml"></include>
</struts>