struts2 access web elements

10144 단어 struts2
Four ways to access web elements in Struts2 and the way to obtain background values ​​from the front JSP page Four ways:

1. Access request, session, application objects through ActionContext
2. Access request, session, application objects by implementing RequestAware, SessionAware, and ApplicationAware interfaces
3. Access request, session, application objects through ServletActionContext
4. Access request, session, application objects by implementing the ServletRequestAware interface



Demo code:
method one:
/**
 *  ActionContext request,session,application 
 * @author  
 */
public class UserAction1 extends ActionSupport{
	/**
	 *  
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * request
	 */
	private Map <String,Object>request;
	/**
	 * response
	 */
	private Map <String,Object>session;
	/**
	 * application
	 */
	private Map <String,Object>application;
	/**
	 *   
	 * @return  
	 */
	@SuppressWarnings("unchecked")
	@Override
	public String execute(){
		System.out.println(" ActionContext request,session,application ");
		//  
		request = (Map<String,Object>)ActionContext.getContext().get("request");
		session = ActionContext.getContext().getSession();
		application = ActionContext.getContext().getApplication();
		//  
		request.put("requestKey", "requestValue");
		session.put("sessionKey", "sessionValue");
		application.put("applicationKey", "applicationValue");
		return "success";
	}
}
Method two:

/**
 *  RequestAware、SessionAware、ApplicationAware request,session,application 
 * @author  
 */
public class UserAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
	/**
	 *  
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * request
	 */
	private Map <String,Object>request;
	/**
	 * response
	 */
	private Map <String,Object>session;
	/**
	 * application
	 */
	private Map <String,Object>application;
	/**
	 *  
	 */
	@SuppressWarnings("unchecked")
	@Override
	public String execute(){
		System.out.println(" RequestAware、SessionAware、ApplicationAware request,session,application ");
		//  
		request.put("requestKey", "requestValue");
		session.put("sessionKey", "sessionValue");
		application.put("applicationKey", "applicationValue");
		return "success";
	}
	/* 
	 *  RequestAware 
	 */
	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}
	/* 
	 *  ApplicationAware 
	 */
	@Override
	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}
	/* 
	 *  SessionAware 
	 */
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
}
Method three:

+/**
 *  ServletActionContext request,session,application 
 * @author  
 */
public class UserAction3 extends ActionSupport{
	/**
	 *  
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * request
	 */
	private HttpServletRequest request;
	/**
	 * response
	 */
	private HttpSession session;
	/**
	 * application
	 */
	private ServletContext application;
	/**
	 *  
	 */
	@SuppressWarnings("unchecked")
	@Override
	public String execute(){
		System.out.println(" ServletActionContext request,session,application ");
		//  
		request = ServletActionContext.getRequest();
		session = request.getSession();
		application = session.getServletContext();
		//  
		request.setAttribute("requestKey", "requestValue");
		session.setAttribute("sessionKey", "sessionValue");
		application.setAttribute("applicationKey", "applicationValue");
		return "success";
	}
}
Method four:

/**
 *  ServletRequestAware request,session,application 
 * @author  
 */
public class UserAction4 extends ActionSupport implements ServletRequestAware{
	/**
	 *  
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * request
	 */
	private HttpServletRequest request;
	/**
	 * response
	 */
	private HttpSession session;
	/**
	 * application
	 */
	private ServletContext application;
	/**
	 *  
	 */
	@SuppressWarnings("unchecked")
	@Override
	public String execute(){
		System.out.println(" ServletRequestAware request,session,application ");
		//  
		request.setAttribute("requestKey", "requestValue");
		session.setAttribute("sessionKey", "sessionValue");
		application.setAttribute("applicationKey", "applicationValue");
		return "success";
	}
	/* 
	 *  ServletRequestAware 
	 */
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		this.session = request.getSession();
		this.application = session.getServletContext();
	}
}
The above action supporting struts.xml and jsp page
struts.xml

<struts>
	<!--  :  -->
	<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">
<html>
	<head>
		<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 is 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>
	</head>

	<body>
		<form name="form1">
			<div>
				Struts2 web <br>
				 :<input type="button" value="submit1" onclick="sub('<%=basePath%>login/login1')"><br>	
				 :<input type="button" value="submit2" onclick="sub('<%=basePath%>login/login2')"><br>
				 :<input type="button" value="submit3" onclick="sub('<%=basePath%>login/login3')"><br>
				 :<input type="button" value="submit4" onclick="sub('<%=basePath%>login/login4')"><br>
			</div>
		</form>
	</body>
</html>
success.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">
<html>
	<head>
		<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 is my page">
		<!--
		<link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
		-->
	</head>

	<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>
</html>
How to get the background value of the front jsp page


|<%=request.getAttribute("requestKey")%>
|<%=session.getAttribute("sessionKey")%>
|<%=application.getAttribute("applicationKey")%>


Reference link:
http://blog.CSDN.net/what to do 653983/article/details/8039292

좋은 웹페이지 즐겨찾기