• JavaWeb学习:Struts2中Servlet的API的访问(传值)


    传值方式有以下三种:

    • 完全解耦
    • 使用Servlet原生API
    • 使用接口注入

    一、完全解耦(ActionContext)

      ①、index.jsp

    <h3>方式一:完全解耦</h3>
        <form action="${pageContext.request.contextPath}/request1.action" method="post">
            name:<input type="text" name="name" /><br /> 
            password:<input type="password" name="password" /><br /> 
            <input type="submit" value="submit" />
        </form>

      ②、success.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    ${reqName}
    ${sessName}
    ${appName}
    </body>
    </html>

      ③、struts.xml

    <struts>
        <include file="com/xxx/web/action/struts_request.xml"></include>
    </struts>

      ④、Request1Action

    public class Request1Action extends ActionSupport {
        @Override
        public String execute() throws Exception {
        ActionContext context=ActionContext.getContext();
        
        //一、获取表单提交数据
        Map<String, Parameter> map=context.getParameters();
        
        for (Parameter value : map.values()) {
            System.out.println(value.getName()+"     "+value.getValue());
        }
        
        //二、向域对象中存入数据
        context.put("reqName", "reqValue");//相当于request.setAttribute();
        context.getSession().put("sessName", "sessValue");//相当于session.setAttribute();
        context.getApplication().put("appName", "appValue");//相当于application.setAttribute();
        return super.execute();
        }
    }

      ⑤、struts_request.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
    
    <struts>
        <package name="strutsdemo" extends="struts-default" namespace="/">
            <action name="request1" class="com.xxx.web.action.Request1Action">
                <result>success.jsp</result>
            </action>
        </package>
    </struts>

    注意:这种方式只能获得代表request、session、application的数据的Map集合,不能操作这些对象的本身的方法。

    二、使用Servlet的原生API(通过ServletActionContext对象获取域对象(request对象、session对象、application对象))

       ①、index.jsp

        <h3>方式二:使用Servlet的原生API</h3>
        <form action="${pageContext.request.contextPath}/request2.action" method="post">
            name:<input type="text" name="name" /><br /> 
            password:<input type="password" name="password" /><br /> 
            <input type="submit" value="submit" />
        </form>

      ②、Request2Action

    public class Request2Action extends ActionSupport {
        @Override
        public String execute() throws Exception {
        HttpServletRequest request= ServletActionContext.getRequest();
        
        //一、获取表单提交数据
        Map<String, String[]> map=request.getParameterMap();
        
        for (String key : map.keySet()) {
            System.out.println(key+"     "+Arrays.toString(map.get(key)));
        }
        
        //二、向域对象中存入数据
        request.setAttribute("reqName", "reqValue");
        request.getSession().setAttribute("sessName", "sessValue");
        ServletActionContext.getServletContext().setAttribute("appName", "appValue");
        return super.execute();
        }
    }

      ③、struts_request.xml

         <action name="request2" class="com.xxx.web.action.Request2Action">
                <result>success.jsp</result>
            </action>

    注意:这种方式可以操作域对象的数据,同时也可以获得对象的方法。

    三、接口注入

    通过实现接口的方式实现域对象的实例化

      ①、index.jsp

        <h3>方式三:接口注入</h3>
        <form action="${pageContext.request.contextPath}/request3.action" method="post">
            name:<input type="text" name="name" /><br /> 
            password:<input type="password" name="password" /><br /> 
            <input type="submit" value="submit" />
        </form>

      ②、Request3Action

    public class Request3Action extends ActionSupport implements ServletRequestAware, ServletContextAware {
    
        private HttpServletRequest request;
        private ServletContext context;
    
        @Override
        public void setServletContext(ServletContext context) {
        this.context = context;
        }
    
        @Override
        public void setServletRequest(HttpServletRequest request) {
        this.request = request;
        }
        
        @Override
        public String execute() throws Exception {
        HttpServletRequest request= ServletActionContext.getRequest();
        
        //一、获取表单提交数据
        Map<String, String[]> map=request.getParameterMap();
        
        for (String key : map.keySet()) {
            System.out.println(key+"     "+Arrays.toString(map.get(key)));
        }
        
        //二、向域对象中存入数据
        request.setAttribute("reqName", "reqValue");
        request.getSession().setAttribute("sessName", "sessValue");
        context.setAttribute("appName", "appValue");
        return super.execute();
        }
    }

      ③、struts_request.xml

            <action name="request3" class="com.xxx.web.action.Request3Action">
                <result>success.jsp</result>
            </action>

    四、结果页面配置

      ①、全局结果页面

        在包中配置一次的值,包中所有action(没有配置局部结果页面)返回值与配置值一致,都跳转同一页面

    <struts>
        <package name="strutsdemo" extends="struts-default" namespace="/">
            <!-- 全局结果页面配置 -->
            <global-results>
                <!-- <result name="默认值success"> -->
                <result>success.jsp</result>
            </global-results>
            <action name="request1" class="com.xxx.web.action.Request1Action">
            </action>
            
            <action name="request2" class="com.xxx.web.action.Request2Action">
            </action>
            
            <action name="request3" class="com.xxx.web.action.Request3Action">
            </action>
        </package>
    </struts>

      ②、局部结果页面

        只对当前action有效,配置在action标准中  

    <struts>
        <package name="strutsdemo" extends="struts-default" namespace="/">
            <global-results>
                <result>success.jsp</result>
            </global-results>
            <action name="request1" class="com.xxx.web.action.Request1Action">
                <result>success.jsp</result>
            </action>
            
            <action name="request2" class="com.xxx.web.action.Request2Action">
                <result>success.jsp</result>
            </action>
            
            <action name="request3" class="com.xxx.web.action.Request3Action">
                <result>success.jsp</result>
            </action>
        </package>
    </struts>
    • result标签用于配置页面的跳转。在result标签上有两个属性:
      • name属性                :逻辑视图的名称。默认值:success
      • type属性                   :页面跳转的类型。
        • dispatcher    :默认值,请求转发。(Action转发JSP
        • redirect         :重定向。(Action重定向JSP
        • chain              :转发。(Action转发Action)
        • redirectAction :重定向。(Action重定向Action)
        • stream            :Struts2中提供文件下载的功能。
  • 相关阅读:
    去掉CodeIgniter URL中的index.php
    php分页方法
    CI框架下 ajax分页
    控制DIV中的文字绝对居中
    JS n秒后自动跳转实例
    IE下页面左偏移并页头空出一行解决方法
    iis服务器php环境 failed to open stream: No such file or directory解决办法
    php导出word格式数据的代码(转)
    linux mysql命令行查看显示中文
    Apache Rewrite url重定向功能的简单配置
  • 原文地址:https://www.cnblogs.com/WarBlog/p/14036857.html
Copyright © 2020-2023  润新知