struts2 基础

Action 获取 域 的三种方式

通过 ActionContext 获取

public class Demo05Action extends ActionSupport{
    @Override
    public String execute() throws Exception {
        // 使用ActionContext方式一 获取session域
        Map<String, Object> session = ActionContext.getContext().getSession();
        session.put("name", "james");
        // 获取aoolication域
        Map<String, Object> application = ActionContext.getContext().getApplication();
        application.put("name", "miku");
        // 获取 request 域 (不建议你 直接使用request域)
        // 如果想使用request域 系统推荐直接使用ActionContext这个map
        // 因为生命周期一样 一个请求 就创建一个 ActionContext 对象(推荐)
        ActionContext.getContext().put("name", "Tom");
        // 这种方式也可以直接从 ActionContext 对象中获取request对象(不推荐)
//       Map<String, Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
//      request.put("name", "Tom");
        System.out.println("Demo05Action");
        return SUCCESS;
    }
}

第二种 ServletActionContext 获得

public class Demo06Action extends ActionSupport{

    @Override
    public String execute() throws Exception {
        // 获取原生servlet 对象
        HttpServletRequest request = ServletActionContext.getRequest(); 
        HttpServletResponse response = ServletActionContext.getResponse();
        ServletContext servletContext = ServletActionContext.getServletContext();
        HttpSession session = request.getSession();
        return SUCCESS;
    }
}

第三种 通过实现 ServletRequestAware 接口 获得

public class Demo07Action extends ActionSupport implements ServletRequestAware{
    private HttpServletRequest request;

    public Demo07Action() {
        System.out.println("Demo07Action 启动!");
    }
    @Override
    public String execute() throws Exception {
        this.request.setAttribute("name", "james");
        return SUCCESS;
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        // 使用成员变量 赋值的方式得到原生的servlet对象
        this.request = request;
    }
}

Action 获取表单属性的三种方式

第一种 属性驱动

public class Demo08Action extends ActionSupport{
    // 利用属性驱动 获取提交的参数   
    // 注意:要提供 set/get 方法
    // 要使用表单中的name 作为属性名
    // struts 2 提供了 类型转换 支持基本数据类型的包装类
    private String username;
    private Integer age;
    // 只支持 2018-04-13 这个格式的类型转换
    private Date birthday;
    /*
     * 获取表单提交数据方式1
     * 创建form1.jsp 
     * 提交姓名 年龄 生日 date 
     */

    @Override
    public String execute() throws Exception {

        System.out.println(username);
        System.out.println(age);
        System.out.println(birthday);
        return SUCCESS;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }   
}

第二种 对象驱动

public class Demo09Action extends ActionSupport{
    // 声明一个 User 对象属性
    private User user;
    @Override
    public String execute() throws Exception {

        System.out.println(user);
        return SUCCESS;
    }
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }   
}
// 这里要注意一点
// 使用对象驱动 在jsp表单中 每个input标签中name  要更改成 user.名字 这种格式

第三种 模型驱动 (常用)

public class Demo10Action extends ActionSupport implements ModelDriven<User>{
    // 声明一个对象
    // 模型驱动必须要对 接收参数的对象 进行初始化
    // 注意 对象驱动 需要修改 form表单的name属性 
    // 模型驱动 不需要 原来怎么整现在就怎么整
    private User user = new User();


    @Override
    public String execute() throws Exception {
        System.out.println(user);   
        return SUCCESS;
    }

    // 实现接口中的方法
    @Override
    public User getModel() {
        // 直接返回user对象
        return user;
    }
}

struts2 自定拦截器创建三种方法

第一种 实现 Interceptor 接口

public class MyInterception1 implements Interceptor{

    // 拦截器的初始化方法
    @Override
    public void init() {
        // TODO Auto-generated method stub

    }

    // 拦截方法
    @Override
    public String intercept(ActionInvocation arg0) throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

    // 销毁方法
    @Override
    public void destroy() {
        // TODO Auto-generated method stub
    }
}

第二种 继承AbstractInterceptor

public class MyInterception2 extends AbstractInterceptor{

    @Override
    public String intercept(ActionInvocation arg0) throws Exception {
        // TODO Auto-generated method stub
        return null;
    }
}

第三种 继承 MethodFilterInterceptor 常用!

public class MyInterception3 extends MethodFilterInterceptor{

    // 拦截方法
    @Override
    protected String doIntercept(ActionInvocation invocation) throws Exception {
        // 前处理
        System.out.println("前处理");
        // 放行方法
        // 所有拦截器都执行完毕后会拿到一个字符串
        // 这个字符串就是 action类 执行完毕后返回的字符串
        String result = invocation.invoke();
        // 后处理
        System.out.println("后处理");
        return result;
    }

}

拦截器配置

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>
<constant name="struts.devMode" value="true"></constant>
<package name="inter" namespace="/" extends="struts-default">
    <!-- 1.注册拦截器 -->
    <interceptors>
        <!-- 注册自己定义的拦截器 -->
        <interceptor name="myinterception3" class="com.lanou3g.interception.MyInterception3"></interceptor>
        <!-- 注册拦截器栈 -->
        <interceptor-stack name="mystack">
            <!-- 除了应用自己的拦截器 还要应用系统自己的拦截器 -->
            <!-- 建议先放自己的拦截器 在放系统的拦截器
                 自己写的拦截器 可能会用到系统拦截器封装好的功能 -->
            <!-- 引入拦截器 -->
            <interceptor-ref name="myinterception3">
                <!-- 设置不想拦截的方法 -->
                <param name="excludeMethods">add,update</param>
            </interceptor-ref>
            <!-- 引入系统默认的拦截器栈 -->
            <interceptor-ref name="defaultStack"></interceptor-ref>
        </interceptor-stack>
    </interceptors>
    <!-- 设置默认的拦截器栈是哪个 -->
    <default-interceptor-ref name="mystack"></default-interceptor-ref>
    <global-results>
            <result name="toLogin" type="redirect">/login.jsp</result>
        </global-results>

        <!-- 配置全局异常类型 映射 -->
        <global-exception-mappings>
            <!-- exception 异常错误类型(全类名)  -->
            <!-- result 出现 异常 返回的结果字符串 -->
            <!-- action 可以根据这个 异常的结果 跳转一个页面 -->
            <exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping>
        </global-exception-mappings>
    <action name="Demo01Action_*" class="com.lanou3g.interception.Demo01Action" method="{1}">
        <result name="add">/hello.jsp</result>
    </action>   
</package>
</struts>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值