Access and add request, session, and application attributes for Struts2

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.