struts2与Servlet解耦

struts2与Servlet解耦

方式一:

action中关键代码:

HttpServletRequest request =ServletActionContext.getRequest();

       request.setAttribute("username", "username_request");

      

             

       Map sessionMap =ServletActionContext.getContext().getSession();

        sessionMap.put("username", "username_session");

      

        ServletContext sc = ServletActionContext.getServletContext();

       sc.setAttribute("username", "username_application");

在项目开发可以先定义一个BaseAction:

public class BaseAction extends  ActionSupport
{
    
    private static final long    serialVersionUID    = 3437750672575106983L;

    HttpServletRequest request=ServletActionContext.getRequest();
    HttpServletResponse response=ServletActionContext.getResponse();
    Map  sessionMap=ServletActionContext.getContext().getSession();
    ServletContext sc=ServletActionContext.getServletContext();
}

再通过继承BaseAction进行复用:

public  class ContextAction  extends  BaseAction{

    private static final long serialVersionUID = -6803448020752037119L;

    public String test(){
            
        System.out.println("context1");
        request.setAttribute("username", "username_request");//request        
        sessionMap.put("username", "username_session");//session        
        sc.setAttribute("username", "username_application");//servlet
        return "attr";
    }

 方式二:实现接口的方式

public class ContextAction extends ActionSupport
    implements ServletRequestAware, ServletResponseAware, ServletContextAware, SessionAware{


    HttpServletRequest request;
    
    HttpServletResponse response;
    
    Map sessionMap;
    
    ServletContext sc;

    
    public String test(){
        System.out.println("ContextAction *********** test()");
        
        request.setAttribute("username", "username_request");//request
        
        sessionMap.put("username", "username_session");//sessopmn
        
        sc.setAttribute("username", "username_application");//servlet
        

        
        return "attr";
    }

    public void setSession(Map<String, Object> session) {
        this.sessionMap = session;
        
    }

    public void setServletContext(ServletContext context) {
        this.sc = context;
        
    }

    public void setServletResponse(HttpServletResponse response) {
        this.response = response;
        
    }

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
        
    }

在实际开发中先定义一个BaseAction2(基本action)

public class BaseAction2 extends ActionSupport
implements ServletRequestAware, ServletResponseAware, ServletContextAware, SessionAware{

    private static final long serialVersionUID = 2131084805823257294L;

    protected HttpServletRequest request;
    
    protected  HttpServletResponse response;
    
    protected  Map sessionMap;
    
    protected  ServletContext sc;

    public void setSession(Map<String, Object> session) {
        // TODO Auto-generated method stub
        this.sessionMap=session;
    }

    public void setServletContext(ServletContext context) {
        // TODO Auto-generated method stub
        this.sc=context;
    }

    public void setServletResponse(HttpServletResponse response) {
        // TODO Auto-generated method stub
        this.response=response;
        
    }

    public void setServletRequest(HttpServletRequest request) {
        // TODO Auto-generated method stub
        this.request=request;
    }

}

其他使用servlet对象的action通过继承BaseAction2

public  class ContextAction  extends  BaseAction2{
    private static final long serialVersionUID = -6803448020752037119L;

    public String test(){
            
        System.out.println("context1");
        request.setAttribute("username", "username_request");//request        
        sessionMap.put("username", "username_session");//session        
        sc.setAttribute("username", "username_application");//servlet
        return "attr";
    }

显示页面:

使用struts标签需要引入:<%@ taglib uri="/struts-tags"  prefix="s"%>

<%@pagelanguage="java"pageEncoding="utf-8"%>

<%@ taglib uri="/struts-tags"  prefix="s"%>

<html>

  <head>

    <title>My JSP 'index.jsp' starting page</title>

    <meta http-equiv="description"content="This is my page">

  </head>

  <body>

   <!--获取request -->

    jstlrequest:${requestScope.username}<br>

    <!-- struts2标签访问: -->

   strutsrequest:<s:property value="%{#request.username}" /><br>  

    <!--获取session -->

    jstlsession:${sessionScope.username}<br>

    strutssession:<s:property value="%{#session.username}" /><br>

    <!-- 获取appliaction -->

    jstlapplication:${applicationScope.username}<br>

   strutsappliaction:<s:property value="%{#application.username}"/> 

  </body>

</html>

 

 

使用jstl标签需引入:<%@tagliburi="http://java.sun.com/jstl/core_rt" prefix="c"%>

使用用:request对象:

action中: 

    HttpServletRequest request = ServletActionContext.getRequest();

    request.setAttribute("username", "username_request");

    request.setAttribute("listAll", DAOFactory.getPersonDAOInstance().queryAll());

jsp中:

${requestScope.username}<!--一个对象-->

使用struts自定义标签获取:

<!--集合对象-->

  使用jstl获取:

  <c:forEach items="${requestScope.listAll}"var="list">

       <tr>

           <td align="center">${list.id}</td><!-- 输出id -->

           <td>${list.name}</td><!-- 输出name -->

           <td>${list.password}</td><!-- 输出password -->

           <td align="center"><a href="test!queryById?id=${list.id}">修改</a></td>

           <td align="center"><a href="test!delete?id=${list.id}">删除</a></td>  

          

       </tr>

    </c:forEach>

使用session对象:

action中:

    HttpSession session_user = ServletActionContext.getRequest().getSession();

    //把登录的用户名保存到session中

    session_user.setAttribute("name",name);

jsp中:

${sessionScope.name}

使用context对象

action中:

ServletContextsc = ServletActionContext.getServletContext();

sc.setAttribute("username", "username_application");

jsp中:

<!-- 获取appliaction -->

${applicationScope.username}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值