有两种方式可以实现在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

结果如下:

发布评论

分享到:

IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

struts2:JSON在struts中的应用(JSP页面中将对象转换为JSON字符串提交、JSP页面中获取后台Response返回的JSON对象)详解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。