• struts2-第一章-基础用法3


    一,结果类型配置

      在之前servlet学习中,知道网页页面路径跳转有两种方式,内部跳转(请求转发)和外部跳转(重定向),两者的区别,内部跳转浏览器地址不会变化

    可以保存上一次请求的数据

      外部跳转浏览器地址会发生变化,但是不能保存上一次请求数据

    struts2中也有上述两种跳转方式,但是需要我们进行指定,在struts.xml中action节点的result节点的type属性进行配置

    type属性的取值:

      (1)dispatcher :内部跳转,默认值,通常跳转jsp

      (2)redirect:外部跳转,通常跳转jsp

      (3)chain:内部跳转,通常跳转到action

      (4)redirectAction:外部跳转,通常跳转到action

    案例

    (1)action1类

    @Getter
    @Setter
    public class Demo1Action extends ActionSupport {
        
        private String name = "zhangsan";
        
        //内部跳转jsp
        public String dispatcher(){
            System.out.println("Demo1Action.dispatcher...");
            return "a";
        }
        //外部跳转jsp
        public String redirect(){
            System.out.println("Demo1Action.redirect...");
            return "b";
        }
        //内部跳转action
        public String chain(){
            System.out.println("Demo1Action.chain...");
            return "c";
        }
        //外部跳转action
        public String redirectAction(){
            System.out.println("Demo1Action.redirectAction...");
            return "d";
        }
        
    }

    (2)struts.xml配置

    <package name="default" namespace="/" extends="struts-default">
            <action name="demo1Action" class="com.yujun.maven.action.Demo1Action">
                <!-- 跳转到jsp,内部和外部 -->
                <result name="a" type="dispatcher">/demo1.jsp</result>
                <result name="b" type="redirect">/demo1.jsp</result>
                
                <!-- 跳转到action,内部和外部 -->
                <result name="c" type="chain">
                    <!-- 指定跳转的action的名称 -->
                    <param name="actionName">demo2Action</param>
                    <!-- 指定action所在命名空间 -->
                    <param name="namespace">/</param>
                    <!-- 指定执行action的哪个方法 -->
                    <param name="method">m1</param>
                </result>
                <result name="d" type="redirectAction">
                    <!-- 指定跳转的action的名称 -->
                    <param name="actionName">demo2Action!m2.action</param>
                    <!-- 指定action所在命名空间 -->
                    <param name="namespace">/</param>
                </result>
                
            </action>
            
            <action name="demo2Action" class="com.yujun.maven.action.Demo2Action">
                <result>/demo2.jsp</result>
            </action>
            
        </package>

    (3)action2类

    public class Demo2Action extends ActionSupport {
    
        public String m1(){
            System.out.println("Demo2Action.m1...");
            return SUCCESS;
        }
        
        public String m2(){
            System.out.println("Demo2Action.m2...");
            return SUCCESS;
        }
    }

    (4)demo1.jsp

    <h3>
            姓名:${name }
        </h3>

    (5)demo2.jsp

    <h3>
            this is demo2.jsp
        </h3>

    (6)index.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <base href="${pageContext.request.contextPath}/ "/>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <h4>
            <a href="demo3Action!dispatcher.action">访问demo1Action的dispatcher方法</a>
        </h4>
        <h4>
            <a href="demo3Action!redirect.action">访问demo1Action的redirect方法</a>
        </h4>
        <h4>
            <a href="demo3Action!chain.action">访问demo1Action的chain方法</a>
        </h4>
        <h4>
            <a href="demo3Action!redirectAction.action">访问demo1Action的redirectAction方法</a>
        </h4>
    
    </body>
    </html>

    二,action中访问request,session,application

    在struts2中的action有时需要访问servlet资源,比如会话,记录登录人信息,通常有三种

    (1)使用ActionContext

    (2)使用ServletActionContext

    (3)action实现ServletRequestAware/ServletContextAware接口

    1.ActionContext

    表示action上下文,上下文看做一个容器(本质就是HashMap),存放的是action在执行时需要用到的对象,是一个线程安全类

    (1)action类

    public class Demo3Action extends ActionSupport {
    
        public String m1(){
            //都可以给当前请求中存放数据
            //等效于request对象,但并不是request
            ActionContext.getContext().put("name", "小明");
            //真正的request对象
            ((Map)ActionContext.getContext().get("request")).put("name2", "小红");
            
            //session对象
            ActionContext.getContext().getSession().put("username", "admin");
            
            //application对象
            ActionContext.getContext().getApplication().put("count", 10);
            return SUCCESS;
        }
    }

    (2)struts.xml配置

    <action name="demo3Action" class="com.yujun.maven.action.Demo3Action">
                <result>/demo3.jsp</result>
    </action>

    (3)页面

    <h3>
            ${name },${name2 },${username },${count }
    </h3>

    若想查看值栈的信息,可以使用struts2提供的debug标签

    (4)访问

    http://127.0.0.1:8080/struts2-chapter1-3/demo3Action!m1.action

    2.servletActionContext

      public class ServletActionContext extend ActionContext implements StrutsStatics{

    ServletActionContext类继承了ActionContext,因此此类也是一个线程安全类;该类提供了直接访问servlet容器对象(request、session、application)的功能

    (1)action类

    public class Demo4Action extends ActionSupport {
    
        
        public String m1(){
            //request对象
            ServletActionContext.getRequest().setAttribute("abc", "abc123");
            //session对象
            ServletActionContext.getRequest().getSession().setAttribute("xyz", "xyz123");
            //application对象
            ServletActionContext.getServletContext().setAttribute("opq", "opq123");
            return SUCCESS;
        }
    }

    (2)struts.xml配置

    <action name="demo4Action" class="com.yujun.maven.action.Demo4Action">
                <result>/demo4.jsp</result>
    </action>

    (3)页面

    (4)       访问

    http://127.0.0.1:8080/struts2-chapter1-3/demo4Action!m1.action

    3.实现接口的方式

    (1)action类

    public class Demo5Action extends ActionSupport implements ServletRequestAware,ServletContextAware{
    
        //成员变量保存servlet容器对象
        private HttpServletRequest request;
        private ServletContext application;
        private HttpSession session;
        //参数的request是struts2注入给我们的,我们可以直接保存起来
        @Override
        public void setServletRequest(HttpServletRequest request) {
            this.request = request;
            this.session = request.getSession();
        }
        @Override
        public void setServletContext(ServletContext context) {
            this.application = context;
        }
        
        public String m1(){
            request.setAttribute("a", "a123");
            session.setAttribute("b", "b123");
            application.setAttribute("c", "c123");
            return SUCCESS;
        }
    
    }

    (2)struts.xml配置

    <action name="demo5Action" class="com.yujun.maven.action.Demo5Action">
                <result>/demo5.jsp</result>
    </action>

    (3)页面

    (4)访问

    http://127.0.0.1:8080/struts2-chapter1-3/demo5Action!m1.action

  • 相关阅读:
    剑指offer字符串列表
    剑指offer数组3
    剑指offer数组2
    剑指offer数组1
    剑指offer数组列表
    tensorflow+ssd_mobilenet实现目标检测的训练
    Win7+keras+tensorflow使用YOLO-v3训练自己的数据集
    Java是如何实现跨平台的
    Xshell 、PuTTY 复制文件到Linux
    Asp.Net Core2.0在linux下发布
  • 原文地址:https://www.cnblogs.com/faded8679/p/10752841.html
Copyright © 2020-2023  润新知