Access and add request, session, and application attributes for Struts2
Access and add request, session, and application attributes in Struts2
Public String execute () {// If only attributes are put into these three ranges, we recommend that you use this method ActionContext actionContext = ActionContext. getContext (); // put applicationactionContext in ServletContext. getApplication (). put ("application", "application Scope"); actionContext. getSession (). put ("session", "session Application Scope"); actionContext. put ("request", "request application scope"); actionContext. put ("hobbies", Arrays. asList ("badminton", "basketball", "soccer", ""); return "success ";}
Print Output
${applicationScope.application } ${sessionScope.session } ${requestScope.request }
What if the corresponding property parameter is an object?
There are two methods to obtain the HttpServletRequest/HttpSession/ServletContext/HttpServletResponse object:
Method 1: Get it directly through the ServletActionContext class:
public String rsa() throws Exception{ HttpServletRequest request=ServletActionContext.getRequest(); ServletContext servletContext=ServletActionContext.getServletContext(); request.getSession(); HttpServletResponse response=ServletActionContext.getResponse(); return "scope";}
Method 2: implement the specified interface, which is injected during struts framework runtime:
Public class HelloWorldAction implements attributes, attributes, ServletContextAware {private HttpServletRequest request; private ServletContext servletContext; private HttpServletResponse response; public void setServletRequest (HttpServletRequest req) {this. request = req; // injection by the Framework at runtime, not set by yourself} public void setServletResponse (HttpServletResponse res) {this. response = res;} public void setServletContext (ServletContext ser) {this. servletContext = ser ;}}
The first method is recommended, which is relatively simple.
Struts2 executes the execute method by default. to dynamically execute other methods, add "Exclamation point + corresponding method name" after the action name"
Complete code
/WEB-INF/page/showInfo. jsp
Package struts2.example. action; import java. util. arrays; import javax. servlet. servletContext; import javax. servlet. http. httpServletRequest; import org. apache. struts2.ServletActionContext; import com. opensymphony. xwork2.ActionContext; public class HelloWorldAction {public String execute () {// If you only add attributes to these three ranges, we recommend that you use ActionContext actionContext = ActionContext. getContext (); // put applicationactionContext in ServletContext. getApplication (). put ("application", "application Scope"); actionContext. getSession (). put ("session", "session Application Scope"); actionContext. put ("request", "request application scope"); actionContext. put ("hobbies", Arrays. asList ("badminton", "basketball", "soccer", ""); return "success";} public String rsa () throws Exception {HttpServletRequest request = ServletActionContext. getRequest (); // get the original request object // request. getRealPath (path); obtain the absolute path of a file in the site directory, ServletContext servletContext = ServletActionContext. getServletContext (); request. setAttribute ("request", "********** request application scope ******"); request. getSession (). setAttribute ("session", "********* session Application Scope ********"); // HttpServletResponse response = ServletActionContext. getResponse (); servletContext. setAttribute ("application", "********* application Scope ********"); return "success ";}}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
$ {ApplicationScope. application}
$ {SessionScope. session}
$ {RequestScope. request}
==================================
$ {Holobby}
My Eclipse will integrate jstl after Java EE5 without importing the corresponding jar package of jstl.