Struts2 and Servlet

Struts2 access mode decoupled from Servlet API

1. Use the ActionContext class to obtain the Map object corresponding to the Servlet API object

1 Get the Map object corresponding to HttpServletRequest
2 Get the Map object corresponding to HttpSessiont
3 Get the Map object corresponding to ServletContext

2.Struts2 injects the Map object corresponding to the Servlet API object into the Action

Struts and Servlet API coupling access mode

3 Access methods coupled with the Servlet API


后台绑定数据
前台获取数据

Request.setAttribute("The name placed on the server", "object collection");
ServletActionContext.getRequest();//Get the request (response)


1. Bind the value to the server and find the value[LoginAction] through the key value

package com.lanou.entity;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

//包名小写 类名大写
public class LoginAction {
public String execute() {
    HttpServletRequest request =ServletActionContext.getRequest();
    //绑定值request
    request.setAttribute("reqkey","reqvalue");
    //绑定值session
    request.getSession().setAttribute("seskey", "sesvalue");
    //绑定值到Context[后台只要上下文]
    ServletActionContext.getServletContext().setAttribute("appkey", "appvalue");
    return "success";

}
}

2. Get [success.jsp] from the front desk

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
  <head>

  </head>

  <body>
request对象:${request.reqkey}
session对象:${session.seskey}
<!--可以直接写application 因为是就打内置对象之一  -->
application对象:${application.appkey}

  </body>
</html>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325904129&siteId=291194637