four ways to access web elements in Struts2 (access to Requst, session, application)
Four different ways:
1. Access to request,session,application objects via Actioncontext
2. Access to Request,session,application objects by implementing Requestaware, Sessionaware, Applicationaware interfaces
3. Access to request,session,application objects via Servletactioncontext
4. Access to Request,session,application objects by implementing the Servletrequestaware interface
Demo Code:
Way One: 1. Access to request,session,application objects via Actioncontext
View Plaincopy to Clipboardprint?
public class UserAction1 extends actionsupport{
Private static final long serialversionuid = 1L;
Private Map <String,Object>request;
Private Map <String,Object>session;
Private Map <String,Object>application;
@SuppressWarnings ("Unchecked")
@Override
Public String execute () {
SYSTEM.OUT.PRINTLN ("Access to request,session,application objects through Actioncontext");
Class
Request = (map<string,object>) actioncontext.getcontext (). Get ("request");
Session = Actioncontext.getcontext (). GetSession ();
application = Actioncontext.getcontext (). Getapplication ();
assigning values
Request.put ("Requestkey", "Requestvalue");
Session.put ("SessionKey", "Sessionvalue");
Application.put ("Applicationkey", "Applicationvalue");
Return "Success";
}
}
In the foreground JSP page, get the data in the session provided by the background in the following three ways (Requeste, application in the same way) with struts tag: <s:property value= "#session. Key"/>
Using El expression: ${session.key}/${key}
In Java code: <%=session.getattribute ("key")%>
Mode two: 2. To access Request,session,application objects by implementing the Requestaware, Sessionaware, Applicationaware interfaces
public class UserAction2 extends Actionsupport implements requestaware,sessionaware,applicationaware{
Private static final long serialversionuid = 1L;
Private Map <String,Object>request;
Private Map <String,Object>session;
Private Map <String,Object>application;
@SuppressWarnings ("Unchecked")
@Override
Public String execute () {
System.out.println ("Access Request,session,application object by implementing Requestaware, Sessionaware, Applicationaware interface");
assigning values
Request.put ("Requestkey", "Requestvalue");
Session.put ("SessionKey", "Sessionvalue");
Application.put ("Applicationkey", "Applicationvalue");
Return "Success";
}
@Override
public void Setrequest (map<string, object> request) {
This.request = Request;
}
@Override
public void Setapplication (map<string, object> application) {
this.application = Application;
}
@Override
public void Setsession (Map<string, object> session) {
This.session = session;
}
}
Mode three: 3. Access to request,session,application objects via Servletactioncontext
View Plaincopy to Clipboardprint?
public class UserAction3 extends actionsupport{
Private static final long serialversionuid = 1L;
private HttpServletRequest request;
Private HttpSession session;
Private ServletContext Application;
@SuppressWarnings ("Unchecked")
@Override
Public String execute () {
SYSTEM.OUT.PRINTLN ("Access to request,session,application objects through Servletactioncontext");
Class
Request = Servletactioncontext.getrequest ();
Session = Request.getsession ();
application = Session.getservletcontext ();
assigning values
Request.setattribute ("Requestkey", "Requestvalue");
Session.setattribute ("SessionKey", "Sessionvalue");
Application.setattribute ("Applicationkey", "Applicationvalue");
Return "Success";
}
}
Mode four: 4. Access to request,session,application objects by implementing the Servletrequestaware interface
public class UserAction4 extends Actionsupport implements servletrequestaware{
Private static final long serialversionuid = 1L;
private HttpServletRequest request;
Private HttpSession session;
Private ServletContext Application;
@SuppressWarnings ("Unchecked")
@Override
Public String execute () {
SYSTEM.OUT.PRINTLN ("Access to the Request,session,application object by implementing the Servletrequestaware Interface");
assigning values
Request.setattribute ("Requestkey", "Requestvalue");
Session.setattribute ("SessionKey", "Sessionvalue");
Application.setattribute ("Applicationkey", "Applicationvalue");
Return "Success";
}
@Override
public void Setservletrequest (HttpServletRequest request) {
This.request = Request;
This.session = Request.getsession ();
This.application = Session.getservletcontext ();
}
}
the Struts.xml and JSP pages on the action kit above
Struts.xml
View Plaincopy to Clipboardprint?
<struts>
<!--configuration Development mode: Modify without restarting the server-->
<constant name= "Struts.devmode" value= "true"/>
<package name= "" namespace= "/login" extends= "Struts-default" >
<action name= "login*" class= "Com.wj.struts2.action.useraction{1}" >
<result name= "Success" >/success.jsp</result>
<result name= "Failure" >/failure.jsp</result>
</action>
</package>
</struts>
index.jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
<% @taglib uri= "/struts-tags" prefix= "s"%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://"
+ request.getservername () + ":" + request.getserverport ()
+ path + "/";
%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<base href= "<%=basePath%>" >
<title>Struts2_AccessWebElements</title>
<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This are my page" >
<!--
<link rel= "stylesheet" type= "Text/css" href= "Styles.css" mce_href= "Styles.css" >
-->
<mce:script language= "JavaScript" ><!--
function Sub (str) {
document.form1.action = str;
Document.form1.submit ();
}
--></mce:script>
<body>
<form name= "Form1" >
<div>
Four ways to access web elements in Struts2 <br>
Mode one: <input type= "button" value= "Submit1" onclick= "sub (' <%=basepath%>login/login1 ')" ><br>
Mode two: <input type= "button" value= "Submit2" onclick= "sub (' <%=basepath%>login/login2 ')" ><br>
Mode three: <input type= "button" value= "Submit3" onclick= "sub (' <%=basepath%>login/login3 ')" ><br>
Mode four: <input type= "button" value= "SUBMIT4" onclick= "sub (' <%=basepath%>login/login4 ')" ><br>
</div>
</form>
</body>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<base href= "<%=basePath%>" >
<title>Struts2_AccessWebElements</title>
<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This are my page" >
<!--
<link rel= "stylesheet" type= "Text/css" href= "Styles.css" mce_href= "Styles.css" >
-->
<body>
Requestkey---<s:property value= "#request. Requestkey"/>|<%=request.getattribute ("Requestkey")%>< Br>
SessionKey---<s:property value= "#session. SessionKey"/>|<%=session.getattribute ("SessionKey")%>< Br>
Applicationkey---<s:property value= "#application. Applicationkey"/>|<%=application.getattribute (" Applicationkey ")%><br>
--------------------------------------------
<s:debug></s:debug>
</body>
How foreground JSP pages get background values
<s:property value= "#request. Requestkey"/>| <%=request.getattribute ("Requestkey")%>
<s:property value= "#session. SessionKey"/>|<%= Session.getattribute ("SessionKey")%>
<s:property value= "#application. Applicationkey"/>|<%= Application.getattribute ("Applicationkey")%>