struts2:在Action中使用Servlet的API,设置、读取各种内置对象的属性
生活随笔
收集整理的這篇文章主要介紹了
struts2:在Action中使用Servlet的API,设置、读取各种内置对象的属性
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
有兩種方式可以實現在Action中使用Servlet的API。一種是使用org.apache.struts2.ServletActionContext類,另一種是使用com.opensymphony.xwork2.ActionContext類。
struts2推薦的是使用第二種方式:使用ActionContext類來對request、session和application對象進行操作。
方式一:使用ServletActionContext類(緊耦合)
1. 創建控制層
package com.clzhang.struts2.demo4;import org.apache.struts2.ServletActionContext;public class ServletActionContextTestAction {private String usernameRequest;private String usernameSession;private String usernameApplication;public String getUsernameApplication() {return usernameApplication;}public String getUsernameRequest() {return usernameRequest;}public void setUsernameRequest(String usernameRequest) {this.usernameRequest = usernameRequest;}public String getUsernameSession() {return usernameSession;}public void setUsernameSession(String usernameSession) {this.usernameSession = usernameSession;}public void setUsernameApplication(String usernameApplication) {this.usernameApplication = usernameApplication;}public String execute() {usernameRequest = "request級別的用戶名:張三";usernameSession = "session級別的用戶名:李四";usernameApplication = "application級別的用戶名:趙五";ServletActionContext.getRequest().setAttribute("usernameRequest",usernameRequest);ServletActionContext.getRequest().getSession().setAttribute("usernameSession", usernameSession);ServletActionContext.getServletContext().setAttribute("usernameApplication", usernameApplication);return "showResult";} }2. 修改配置文件struts.xml
<action name="servletActionContextTest" class="com.clzhang.struts2.demo4.ServletActionContextTestAction"><result name="showResult">/struts2/demo4/showResult.jsp</result></action>3. 創建顯示JSP文件(showResult.jsp)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page isELIgnored="false"%> <%@ taglib uri="/struts-tags" prefix="s"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><body>#request.usernameRequest值是:<s:property value="#request.usernameRequest" /><br />#session.usernameSession值是:<s:property value="#session.usernameSession" /><br />#application.usernameApplication值是:<s:property value="#application.usernameApplication" /><br /></body> </html>4. 測試
打開IE,輸入地址:http://127.0.0.1:8080/st/struts2/servletActionContextTest.action
結果如下:
方式二:使用ActionContext類(松耦合)
1. 創建控制層
package com.clzhang.struts2.demo4;import java.util.Map;import com.opensymphony.xwork2.ActionContext;public class ActionContextTestAction {public String execute() {// 第一種方法向request對象中放數據// 因為ActionContext類沒有getRequest()這樣的方法,所以需要使用下面的方式獲取request對象Map request = (Map) ActionContext.getContext().get("request");request.put("requestValue", "this is reqeustValue");// 第二種方法向request對象中放數據ActionContext.getContext().put("otherrequest","this is otherrequest value");Map session = (Map) ActionContext.getContext().getSession();session.put("sessionValue", "this is sessionValue");Map application = (Map) ActionContext.getContext().getApplication();application.put("applicationValue", "this is applicationValue");return "showScopeValue";} }2. 修改配置文件struts.xml
<action name="actionContextTest" class="com.clzhang.struts2.demo4.ActionContextTestAction"><result name="showScopeValue">/struts2/demo4/showScopeValue.jsp</result></action>3. 創建顯示JSP文件(showScopeValue.jsp)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="s"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><body>request:<s:property value="#request.requestValue" /><br /><br />otherrequest:<s:property value="#request.otherrequest" /><br /><br />session:<s:property value="#session.sessionValue" /><br /><br />application:<s:property value="#application.applicationValue" /><br /><br /></body> </html>4. 測試
打開IE,輸入地址:http://127.0.0.1:8080/st/struts2/actionContextTest.action
結果如下:
總結
以上是生活随笔為你收集整理的struts2:在Action中使用Servlet的API,设置、读取各种内置对象的属性的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: failed to get the ta
- 下一篇: 封装 js 插件 实例