Struts 2中访问Web元素的4种方法

在Struts 2中,Action类和Web对象之间没有直接关系,但可以通过Action访问Web对象,在Action中访问Web对象有4中方式

  • 直接访问Web对象——Servlet依赖容器方式
  • 通过ActionContext访问——Map依赖容器方式
  • 通过IoC访问Servlet对象——Map Ioc方式
  • 通过IoC访问Servlet对象——Servler IoC方式

一、 Action示例

    设计控制类(Action类),分别使用以下4种方式

1、直接访问Web对象——Servlet依赖容器方式

    Struts 2 框架提供org.apache.struts2.ServletActionContext辅助类来获得Web对象。

  • HttpServletRequest request=ServletActionContext.getRequest();
  • HttpServletRequest response=ServletActionContext.getResponse();
  • ServletContext application=ServletActionContext.getServletContext();
  • PageContext pagecontext=ServletActionContext.getPageContext();
  • 而HttpSession session的获取需要两步:
HttpServletRequest request=ServletActionContext.getRequest();
HttpSession session=request.getSession();

    设计Action类如下

package action;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
public class testAction {

    private HttpServletRequest request;
    private HttpSession session;
    private ServletContext application;
    private int x;
    private int y;
    private int sum;
    public testAction() {
        request=ServletActionContext.getRequest();
        session=request.getSession();
        application=session.getServletContext();
    }
    public int getX() {return x;}
    public void setX(int x) {this.x = x;}
    public int getY() {return y;}
    public void setY(int y) {this.y = y;}
    public int getSum() {return sum;}
    public String execute(){
        sum=x+y;
        request.setAttribute("x", x);
        session.setAttribute("y", y);
        application.setAttribute("sum2", sum);
        if(sum>=0)
            return "+";
        else
            return "-";
    }
}

2、通过ActionContext访问——Map依赖容器方式

    ActionContext类位于com.opensymphoney.xwork2中,提供了一系列相关的方法用于访问保存在ServletContext、HttpSession、HttpServletRequest中的信息,并将结果存储在Map中
    ActionContext类中常用方法如下

  • public static ActionContext getContext():获得ActionContext对象
  • public Object get(Object key):在ActionContext中查找Key的值
  • public void put(Object key,Object value):向当前ActionContext存入值
  • public void setApplication(Map application):设置application的值
  • public void setSession(Map session):设置session的值
  • public Map getParameters():从请求对象中获取请求参数
  • public Map getApplication():获取ServletContext中保存的Attribute.
  • public Map getSession():获取HttpSession中保存的Attribute

    设计Action类如下

package action;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
public class testAction {

    private Map<String,Object> request;
    private Map<String,Object> session;
    private Map<String,Object> application;
    private int x;
    private int y;
    private int sum;
    public testAction() {
        ActionContext context=ActionContext.getContext();
        request=(Map<String, Object>) context.get("request");
        session=context.getSession();
        application=context.getApplication();
    }
    public int getX() {return x;}
    public void setX(int x) {this.x = x;}
    public int getY() {return y;}
    public void setY(int y) {this.y = y;}
    public int getSum() {return sum;}
    public String execute(){
        sum=x+y;
        request.put("x", x);
        session.put("y", y);
        application.put("sum2", sum);
        if(sum>=0)
            return "+";
        else
            return "-";

    }
}

3、通过IoC访问Servlet对象——Map Ioc方式

    在Struts 2框架中,通过IoC方式将Servlet对象注入到Action中,需要在Action中实现以下接口

  1. org.apache.struts2.interceptor.RequestAware
    该接口有void setRequest(Map map)方法,实现该方法的Action可访问HttpRequest对象
  2. org.apache.struts2.interceptor.SessionAware
    该接口有void setSession(Map map)方法,实现该方法的Action可访问HttpSession对象
  3. org.apache.struts2.interceptor.ApplicationAware
    该接口有void setApplication(Map map)方法,实现该方法的Action可访问HttpApplication对象

    设计Action类如下

package action;
import java.util.Map;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;

public class testAction implements RequestAware,SessionAware,ApplicationAware{

    private Map<String,Object> request;
    private Map<String,Object> session;
    private Map<String,Object> application;
    private int x;
    private int y;
    private int sum;

    @Override
    public void setApplication(Map<String, Object> arg0) {
        // TODO Auto-generated method stub
        this.application=arg0;
    }
    @Override
    public void setSession(Map<String, Object> arg0) {
        // TODO Auto-generated method stub
        this.session=arg0;
    }
    @Override
    public void setRequest(Map<String, Object> arg0) {
        // TODO Auto-generated method stub
        this.request=arg0;
    }
    public testAction() {
        ActionContext context=ActionContext.getContext();
        request=(Map<String, Object>) context.get("request");
        session=context.getSession();
        application=context.getApplication();
    }
    public int getX() {return x;}
    public void setX(int x) {this.x = x;}
    public int getY() {return y;}
    public void setY(int y) {this.y = y;}
    public int getSum() {return sum;}
    public String execute(){
        sum=x+y;
        request.put("x", x);
        session.put("y", y);
        application.put("sum2", sum);
        if(sum>=0)
            return "+";
        else
            return "-";
    }
}

4、通过IoC访问Servlet对象——Servler IoC方式

    在Struts 2框架中,通过IoC方式将Servlet对象注入到Action中,需要在Action中实现以下接口

  1. org.apache.struts2.interceptor.ServletContextAware
    该接口有void setServletContext(ServletContext servletContext)方法,实现该方法的Action可访问ServletContext对象
  2. org.apache.struts2.interceptor.ServletRequestAware
    该接口有void setServletRequest(HttpServletRequest requst)方法,实现该方法的Action可访问HttpServletRequest对象
  3. org.apache.struts2.interceptor.ServletResponseAware
    该接口有void setServletResponse(HttpServletResponse response)方法,实现该方法的Action可访问HttpServletResponse对象

    设计Action类如下

package action;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;

public class testAction implements ServletRequestAware{

    private HttpServletRequest request;
    private HttpSession session;
    private ServletContext application;
    private int x;
    private int y;
    private int sum;
    @Override
    public void setServletRequest(HttpServletRequest request) {
        // TODO Auto-generated method stub
        this.request=request;
        this.session=request.getSession();
        this.application=session.getServletContext();   
    }
    public int getX() {return x;}
    public void setX(int x) {this.x = x;}
    public int getY() {return y;}
    public void setY(int y) {this.y = y;}
    public int getSum() {return sum;}
    public String execute(){
        sum=x+y;
        request.setAttribute("x", x);
        session.setAttribute("y", y);
        application.setAttribute("sum2", sum);
        if(sum>=0)
            return "+";
        else
            return "-";
    }
}

二、 测试

1.在MyEclipse中创建test工程,导入Struts2必须的jar包或者使用MyEclipse自动添加Struts 2支持
2.修改web.xml配置文件,在web.xml中添加如下的配置信息

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

3.编写jsp页面

(1)input.jsp


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>数据提交页面</title>
  </head>
  <body>
   <form action="add" method="post">
    请输入两个整数:<br><br>
    加数:<input name="x"><br><br>
    被加数:<input name="y"><br><br>
    <input type="submit" value="求和">
   </form>
  </body>
</html>

(2)positive.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>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
  </head>

  <body>
    代数和为非负数:<s:property value="sum"/><br>
    加数:<s:property value="#request.x" /><br>
    被加数:<s:property value="#session.y" /><br>
     和值:<s:property value="#application.sum2" /><br>

  </body>
</html>

(3)negative.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>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
  </head>

  <body>
    代数和为非负数:<s:property value="sum"/><br>
    加数:<s:property value="#request.x" /><br>
    被加数:<s:property value="#session.y" /><br>
     和值:<s:property value="#application.sum2" /><br>

  </body>
</html>

4.修改struts.xml配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <package name="default" namespace="/" extends="struts-default">
        <action name="add" class="action.testAction">
            <result name="+">/positive.jsp</result>
            <result name="-">/negative.jsp</result>

        </action>
    </package>
</struts>    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值