Struts学习之路-第二天(Action与Servlet API)

Struts作为一款Web框架自然少不了与页面的交互,开发过程中我们最常用的request、application、session等struts都为我们进行了一定的封装与处理

一、通过ActionContext获取

方法 说明
void put(String key,Object value) 模拟HttpServletRequest中的setAttribute()
Object get(Object key) 通过参数key查找当前ActionContext中的值
Map getApplication() 返回一个Application级的Map对象
static ActionContext getContext() 获得当前线程的ActionContext对象
Map getParameters() 返回一个Map类型HttpSession对象
void put(Object key,Object vlue) 向前ActionContext对象中存入名值对信息
void setApplication(Map application) 设置Application上下文
void setSession(Map session) 设置一个Map类型的Session对象
Map setSession()

返回一个Map类型的HttpSession对象

  1.配置web.xml

  

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

  2.配置struts.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>
    <!--struts中对应mvc的三层是,filterdispatcher->控制层、Action->模型层、Result->视图层-->

    <package name="test" extends="struts-default">
    <!--设置允许动态调用的方法-->
        <global-allowed-methods>user</global-allowed-methods>

        <action name="user" class="test.TestAction">
            <!--result顾名思义返回结果,name属性与Action中返回的字符串一样,作用就是返回视图-->
            <result name="success">/success.jsp</result>
            <result name="user">/user.jsp</result>
        </action>
    </package>

    <!--设置允许动态调用,除了execute()方法之外的其他自定义方法-->
    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
    <constant name="struts.i18n.encoding" value="UTF-8"></constant>
</struts>

  3.在test包下写TestAction类

package test;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport{
    private static final long serialVersionUID = 1L;
    //我们也可以通过实现特定接口来获取对应的对象比如ServletRequestAware可以获取到HttpServletRequest
    //的实例,但是我们需要手动定义该对象,通过接口中的重写的方法来初始化
    public String execute(){
        return "success";
    }
    public String user(){
//        struts通过Action访问servlet中的request、session、application等
        ActionContext context = ActionContext.getContext();
        context.put("name","request");//默认是request
        context.getSession().put("name","session");//session
        context.getApplication().put("name","application");//application
        return "user";
    }
}

4.准备页面index.jsp、user.jsp

  1)index.jsp

<%--
  Created by IntelliJ IDEA.
  po: zgye
  Date: 2019/5/11
  Time: 12:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
      <a href="user">user</a>
      <a href="user!user.action">select</a>
  </body>
</html>

  2)user.jsp

<%--
  Created by IntelliJ IDEA.
  po: zgye
  Date: 2019/5/11
  Time: 13:18
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户页面</title>
</head>
<body>
    测试内置对象:
    ${requestScope.name}
    ${sessionScope.name}
    ${applicationScope.name}
 </body>
</html>

5.启动服务点击select超链接测试

二、通过特定接口访问

ActionContext虽然可以间接的访问Servlet API,同时我们有时候也需要直接去访问Servlet API,Struts2为我们提供了对应的接口

  1)ServletRequestAware

  2)ServletResponseAware

  3)ServletContextAware

  ·······

  1.准备web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

  2.配置struts,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>
    <!--struts中对应mvc的三层是,filterdispatcher->控制层、Action->模型层、Result->视图层-->

    <package name="test" extends="struts-default">
 
        <!--接口方式测试Servlet API-->
        <action name="servlet" class="test.TestServletAction">
            <result name="servlet">/servlet.jsp</result>
        </action>
   </package>
</struts>

  3.准备TestServletAction.ava

package test;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServletAction extends ActionSupport implements ServletRequestAware, ServletResponseAware, ServletContextAware {
    private HttpServletRequest request;
    private HttpServletResponse response;
    private ServletContext context;
    @Override
    public void setServletRequest(javax.servlet.http.HttpServletRequest httpServletRequest) {
        request=httpServletRequest;
    }
    @Override
    public void setServletResponse(HttpServletResponse httpServletResponse) {
        response=httpServletResponse;
    }
    @Override
    public void setServletContext(ServletContext servletContext) {
        context=servletContext;
    }
    @Override
    public String execute() throws Exception {
        request.setAttribute("name","request");
        request.setAttribute("response",response);
        context.setAttribute("name","context");
        return "servlet";
    }



}

  4.准备servlet.jsp页面

<%--
  Created by IntelliJ IDEA.
  User: zgye
  Date: 2019/5/12
  Time: 12:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>servlet测试页面</title>
</head>
<body>
    request:${requestScope.name}<br>
    response:${requestScope.response}<br>
    context:${applicationScope.name}
</body>
</html>

  5.启动服务请求http://localhost:8080/servlet查看测试结果

上一篇:三个数由小到大排序


下一篇:Asp.Net Web Api 与 Andriod 接口对接开发经验,给小伙伴分享一下!