Struts2学习笔记(二):Struts2、Servlet和Filter作为控制器的实现与对比

Struts2学习笔记(一):开发环境配置中,已经比较详细的介绍了在eclipse中搭建Struts2开发环境的步骤和注意事项,今天开始正式应用Struts2作为控制器实现MVC架构。本文主要用示例代码从最简单的请求转发比较Servlet、Filter和Struts2作为控制器的不同。

(1)Servlet作为控制器的实现

①网页中设置链接

<a href="userServlet.do">使用Servlet进行转发</a>

②创建UserServlet类:

package com.jiajia.servlets;
 
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class UserServlet extends HttpServlet {
       private static finallong serialVersionUID = 1L;
      
    public UserServlet() {
        super();
    }
 
       protected voiddoGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
              doPost(request,response);
       }
 
       protected voiddoPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
              //在request域中设置msg属性
              request.setAttribute("msg","Servlet");
              //转发到success.jsp
              request.getRequestDispatcher("/success.jsp").forward(request,response);
       }
 
}
③在web.xml中进行配置

<servlet>
    <display-name>userServlet</display-name>
    <servlet-name>userServlet</servlet-name>
    <servlet-class>com.jiajia.servlets.UserServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>userServlet</servlet-name>
    <url-pattern>/userServlet.do</url-pattern>
  </servlet-mapping>

(2)Filter作为控制器的实现

①网页中设置链接

<a href="user.filter">使用Filter进行转发</a>
②创建UserFilter类

package com.jiajia.filters;
 
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
 
public class UserFilter implements Filter {
 
    public UserFilter() { }
 
       public void destroy() {   }
 
       public voiddoFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {
              request.setAttribute("msg","Filter");
              request.getRequestDispatcher("/success.jsp").forward(request,response);
       }
 
       public voidinit(FilterConfig fConfig) throws ServletException {}
 
}

③web.xml中进行配置

<filter>
   <display-name>userFilter</display-name>
   <filter-name>userFilter</filter-name>
   <filter-class>com.jiajia.filters.UserFilter</filter-class>
  </filter>
  <filter-mapping>
   <filter-name>userFilter</filter-name>
    <!--
           此处为了与Servlet的*.do和Struts2的*.action做区分,才用了*.filter,实际上这种用法并不多见
    -->
   <url-pattern>*.filter</url-pattern>
   <dispatcher>REQUEST</dispatcher>
  </filter-mapping>

(3)Struts2作为控制器的实现

①配置Struts2的开发环境,参照Struts2学习笔记(一):开发环境配置

②网页中设置链接

<a href="user.action">使用Struts2进行转发</a>
③创建UserAction类

package com.jiajia.actions;
 
import java.util.Map;
 
import org.apache.struts2.interceptor.RequestAware;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class UserAction extends ActionSupport implementsRequestAware {
 
       private static finallong serialVersionUID = 1L;
      
       private Map<String,Object> request;
       @Override
       public String execute()throws Exception {
              request.put("msg","Struts2");
              returnsuper.execute();
       }
 
       @Override
       public voidsetRequest(Map<String, Object> arg0) {
              this.request =arg0;
       }
}

④struts.xml中进行配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
       "-//Apache SoftwareFoundation//DTD Struts Configuration 2.3//EN"
       "http://struts.apache.org/dtds/struts-2.3.dtd">
 
<struts>
 
    <packagename="default" namespace="/"extends="struts-default">
 
        <actionname="user" class="com.jiajia.actions.UserAction">
            <resultname="success">/success.jsp</result>
        </action>
       
    </package>
 
</struts>
(4)三者的对比

①servlet能完成的功能filter都能够实现,因为filter中可以访问servlet的完整的API;

②filter能完成的功能,servlet并不一定能够做到;

③Struts2本质上就是一个filter,只是struts2对其做了很多的封装,使得我们使用起来更加方便。

完整代码下载:http://download.csdn.net/detail/jiajia333666/9043815

相关文章
相关标签/搜索
每日一句
    每一个你不满意的现在,都有一个你没有努力的曾经。
公众号推荐
   一个历史类的公众号,欢迎关注
一两拨千金