If you pass data only through request, session, application, you do not need to get the corresponding object or you can pass the data, the code is as follows:
Scopeaction.java:
Package Com.itheima.action;import Com.opensymphony.xwork2.actioncontext;public class Scopeaction {public String Execute () {/* * We do not pass data through fields (add Setxxx, getxxx methods), we want to pass data through the domain (request, session, application) *. How to do it? STRUTS2 uses a low-intrusive, action and servlet without any association, how to get the domain and pass the 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 does not get to the domain object, here we can also get the domain object by other methods:
In the above scopeaction, add the following method:
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