STRUTS2 GET request, session, application, and pass data in action

Source: Internet
Author: User

Assuming that the data is passed only through request, session, and application, there is no need to get the corresponding object to be able to pass the data, such as the following code:

Scopeaction.java:

Package Com.itheima.action;import Com.opensymphony.xwork2.actioncontext;public class Scopeaction {public String Execute () {/* * We do not pass data through fields (join Setxxx, getxxx method), we want to pass data through the domain (request, session, application) *. How can talent do it? STRUTS2 uses the low-intrusive, action and servlet no matter what association, how to acquire the domain and pass data, * Then use the Actioncontext class */actioncontext context = Actioncontext.getcontext (); Context.getapplication (). Put ("Application", "application domain");// Pass data through the ServletContext domain context.getsession (). Put ("session", "session field");//Pass data context.put through session field ("Request", " Request Domain ");//pass data through the request domain return" success ";}}
Struts2.xml:

<action name= "scopeaction_*" class= "com.itheima.action.ScopeAction" method= "{1}" > <result name= "Success" >/msg.jsp</result></action>
MSG.JSP:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" Utf-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Address bar:


=============================================================================================

The above method did not get to the domain object. Here we can also get domain objects by other means:

Add the following methods to the above scopeaction, for example:

Public String Doexe () {HttpServletRequest request = Servletactioncontext.getrequest (); Request.setattribute ("Request" , "request Domain"); ServletContext context = Servletactioncontext.getservletcontext (); Context.setattribute ("Application", "application domain"); HttpSession session = Request.getsession (); Session.setattribute ("Session", "Session Domain");//httpservletresponse response = Servletactioncontext.getresponse (); return "Success";}

Other and the same as above. Then enter in the Address bar:

===============================================================================================

Another way is to inject through the framework:

Scope2action.java:

Package Com.itheima.action;import Javax.servlet.servletcontext;import javax.servlet.http.HttpServletRequest; Import Javax.servlet.http.httpservletresponse;import Org.apache.struts2.interceptor.servletrequestaware;import Org.apache.struts2.interceptor.servletresponseaware;import Org.apache.struts2.util.servletcontextaware;public Class Scope2action implements Servletrequestaware, Servletresponseaware, servletcontextaware{private HttpServletRequest request;private httpservletresponse response;private servletcontext servletContext;@ overridepublic void Setservletcontext (ServletContext arg0) {this.servletcontext = arg0;} @Overridepublic void Setservletresponse (HttpServletResponse arg0) {this.response = arg0;} @Overridepublic void Setservletrequest (HttpServletRequest arg0) {this.request = arg0;} Public String Execute () {Request.setattribute ("request", "requesting domain"); request.getsession (). SetAttribute ("session", "Conversation Domain") ; Servletcontext.setattribute ("Application", "application Domain"); return "Success";}}
Struts2.xml:

<action name= "scope2action" class= "com.itheima.action.Scope2Action" ><result name= "Success" >/msg.jsp </result></action>



STRUTS2 GET request, session, application, and pass data in action

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.