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

2021-06-04 11:04

阅读:460

YPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">

标签:char   instance   filter   string   xmlns   4.0   tty   turn   返回结果   

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>struts2filter-name>
        filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilterfilter-class>
    filter>
    filter-mapping>
        filter-name>struts2filter-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>
    

    package name="test" extends="struts-default">
    
        global-allowed-methods>userglobal-allowed-methods>

        action name="user" class="test.TestAction">
            
            result name="success">/success.jspresult>
            result name="user">/user.jspresult>
        action>
    package>

    
    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.
--%>$Title$
      user
      select
  

  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.
--%>


用户页面
    测试内置对象:
    ${requestScope.name}
    ${sessionScope.name}
    ${applicationScope.name}
 

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

二、通过特定接口访问

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

  1)ServletRequestAware

  2)ServletResponseAware

  3)ServletContextAware

  ·······

  1.准备web.xml


         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">
    struts2class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilterclass>
    struts2/*

  2.配置struts,xml

DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

package name="test" extends="struts-default">
 
        
        class="test.TestServletAction">
            /servlet.jsp
        
   

  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查看测试结果

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

标签:char   instance   filter   string   xmlns   4.0   tty   turn   返回结果   

原文地址:https://www.cnblogs.com/Tiandaochouqin1/p/10851871.html


评论


亲,登录后才可以留言!