MVC三层架构在各框架中的特征

MVC三层架构在各框架中的特征_第1张图片

1.从结构上分析jsp+servlet图解原理:

     在基于mvc设计模式下的最原始的jsp+Servlet框架,在某种程度上是不能够达到mvc最直观的体现。当客户端发送请求到服务器时,服务器会将从客户端接收到的信息通过封装技术进行解析,在交给web容器,web容器会通过一篇web.xml的配置文件去找与之相匹配的servlet,此时servlet会开启一个线程,只要是符合web.xml中所配置的某一个servlet时,该servelt就会启动,然后servlet在去调用service或者service的接口实现类,serviceimpl)在去调用dao(impl),dao通过从数据库(Database)那获取到数据,在封装到model实体类中去。Servlet在完成这一系类的动作后,最终将为我们返回一个jsp页面,最后该页面在返回到客户端展示给客户。

2.web.xml配置上:

[xml] view plain copy print ?
  1. <code class="hljs xml" style="padding:0.5em; margin:0px; display:block; color:rgb(101,123,131); overflow-x:auto; background:rgb(253,246,227)"><span class="hljs-pi" style="padding:0px; margin:0px; color:rgb(147,161,161)"><?xml version="1.0"?></span>  
  2. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">web-app</span>></span>   
  3.   <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet</span>></span>  
  4.         <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet-name</span>></span>TextSerevlet<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet-name</span>></span>  
  5.         <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet-class</span>></span>com.lh.serivlet.UserSerivlet<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet-class</span>></span>  
  6.    <span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet</span>></span>  
  7.    <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet-mapping</span>></span>  
  8.         <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet-name</span>></span>TextSerevlet<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet-name</span>></span>  
  9.          <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">url-pattern</span>></span>/accessServlet<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">url-pattern</span>></span>  
  10.   <span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet-mapping</span>></span>  
  11.  <span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">web-app</span>></span></code>  

注:需要用到的每一篇servlet都要写入它的配置文件。Web.xml的配置量大,繁杂。从性能优异的角度考虑,它的运行性能是最快的,但是开发性能就很有难度。

3.控制器:

[java] view plain copy print ?
  1. <code class="hljs java" style="padding:0.5em; margin:0px; display:block; color:rgb(101,123,131); overflow-x:auto; background:rgb(253,246,227)">Jsp+servlet的控制器是:工程中的每一个xxxServlet就充当一个控制器,但是必须去extends HttpServlet,并重写  
  2. <span class="hljs-function" style="padding:0px; margin:0px"><span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">public</span> <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">void</span> <span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">doGet</span><span class="hljs-params" style="padding:0px; margin:0px">(HttpServletRequest request, HttpServletResponse response)</span>  
  3. <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">throws</span> ServletException, IOException </span>{  
  4.     doPost(request, response);   
  5. }  
  6. <span class="hljs-function" style="padding:0px; margin:0px"><span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">public</span> <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">void</span> <span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">doPost</span><span class="hljs-params" style="padding:0px; margin:0px">(HttpServletRequest request, HttpServletResponse response)</span>  
  7. <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">throws</span> ServletException, IOException </span>{    
  8. }</code>  

4.作用域对象

ServletContext(上下文),也叫全局的作用域对象。

Session(会话)服务器的一次会话中或者服务器超时。

Request(请求)在一次请求的过程中。

Respones(响应)

5.数据绑定:没有,只能通过数组去获取页面的数据。

6.类型转换:jsp页面的数据转换为String类型。也可以是基本数据类型。但是要进行强制转换。

7.有效性验证:可以在某一个需要用到Servlet的类中用Java代码进行验证。也可以在jsp页面书写ajax代码进行验证或者是定义function()函数,在函数中用正则表达式加以判断。

8.操作业务层对象调业务方法:在某一个XxxServletextends HttpServlet并重写doPost()doGet()方法,我们只需在doGet()或者doPost()中用request.getParameter()方法来得到页面的数据。如果我们要操作到业务层我们只需在当前的这个类中实例化出业务层的类,即XxxService dao = new XxxServiceImpl();然后获取实体Bean对象中的属性。最后在我们需要用到数据访问层的那一个具体的业务实现方法就用实例化出的dao调用所需方法即可。

9.返回:可以返回对象,也可以返回具体要跳转的那个页面。 

10.js页面做显示:EL表达式,JSP脚本表达式

11.标签:HTML标签,JSP自定义标记库,JST L标准标记库。

12.文件上传:页面需要怎样获取?<form action=file method=post enctype=multipart/form-data>

             服务器怎么获得?1.必须导入文件上传的jar包。

13.过滤器:我们自己定义的一个类XxxFilter 必须implements Filter接口,并实现它的三个方法,destroy()doFilter(ServletRequest request, ServletResponse response,

FilterChain chain) throws IOException, ServletException init(FilterConfig arg0)。在doFilter()中调用chain.doFilter(request, response)方法,表示将请求交给下一个组件。

MVC三层架构在各框架中的特征_第2张图片

1.从结构上分析Struts1图解原理:

    客户端发送请求到服务器,服务器将收到的信息传给web容器,这时,web容器会通过到web.xml配置文件中去查找那篇符合命名规范的action属性中的*.do,在交给ActionServlet(中央核心控制器),通过该配置,web容器将信息传给具体的某一个XxxAction,XxxAction会继承Action,并重写它的

[java] view plain copy print ?
  1. <code class="hljs vbscript" style="padding:0.5em; margin:0px; display:block; color:rgb(101,123,131); overflow-x:auto; background:rgb(253,246,227)"><span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">public</span> ActionForward <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">execute</span>(ActionMapping mapping, ActionForm form,  
  2. HttpServletRequest <span class="hljs-built_in" style="padding:0px; margin:0px; color:rgb(38,139,210)">request</span>, HttpServletResponse <span class="hljs-built_in" style="padding:0px; margin:0px; color:rgb(38,139,210)">response</span>)  
  3. throws Exception {  
  4. ........  
  5.                   }</code>  

方法,同时,我们可以在该XxxAction中去调用JavaBean中的实体对象。但是,需要注意的是,在ActionServletAction这一过程中,我们是通过Form组件技术来对jsp页面的信息来进行了一次封装,达到了前端页面(jsp)和后台的实体Bean对象之间的一次数据的绑定。最后再是到Struts-config.xml文件中进行进行配置,通过正确的配置找到所需的jsp.

2web.xml配置上:

[xml] view plain copy print ?
  1. <code class="hljs xml" style="padding:0.5em; margin:0px; display:block; color:rgb(101,123,131); overflow-x:auto; background:rgb(253,246,227)"><span class="hljs-pi" style="padding:0px; margin:0px; color:rgb(147,161,161)"><?xml version="1.0" encoding="UTF-8"?></span>  
  2. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">web-app xmlns="http:</span>//<span class="hljs-attribute" style="padding:0px; margin:0px; color:rgb(181,137,0)">java.sun.com</span>/<span class="hljs-attribute" style="padding:0px; margin:0px; color:rgb(181,137,0)">xml</span>/<span class="hljs-attribute" style="padding:0px; margin:0px; color:rgb(181,137,0)">ns</span>/<span class="hljs-attribute" style="padding:0px; margin:0px; color:rgb(181,137,0)">j2ee</span><span class="hljs-attribute" style="padding:0px; margin:0px; color:rgb(181,137,0)">xmlns:xsi</span>=<span class="hljs-value" style="padding:0px; margin:0px; color:rgb(42,161,152)">"http://www.w3.org/2001/XMLSchema-instance"</span> <span class="hljs-attribute" style="padding:0px; margin:0px; color:rgb(181,137,0)">version</span>=<span class="hljs-value" style="padding:0px; margin:0px; color:rgb(42,161,152)">"2.4"</span> <span class="hljs-attribute" style="padding:0px; margin:0px; color:rgb(181,137,0)">xsi:schemaLocation</span>=<span class="hljs-value" style="padding:0px; margin:0px; color:rgb(42,161,152)">"http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"</span>></span>  
  3.   <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet</span>></span>  
  4.     <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet-name</span>></span>action<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet-name</span>></span>  
  5.     <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet-class</span>></span>org.apache.struts.action.ActionServlet<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet-class</span>></span>  
  6.       
  7.     <span class="hljs-comment" style="padding:0px; margin:0px; color:rgb(147,161,161)"><!-- 主配置文件,param-name的值为config --></span>  
  8.     <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">init-param</span>></span>  
  9.       <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">param-name</span>></span>config<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">param-name</span>></span>  
  10.       <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">param-value</span>></span>/WEB-INF/struts-lx.xml,/WEB-INF/struts-zh.xml<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">param-value</span>></span>  
  11.     <span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">init-param</span>></span>  
  12.     <span class="hljs-comment" style="padding:0px; margin:0px; color:rgb(147,161,161)"><!-- 子配置文件,param-name的值只能以config/开头   
  13.     <init-param>  
  14.       <param-name>config/m1</param-name>  
  15.       <param-value>/WEB-INF/struts-zh.xml</param-value>  
  16.     </init-param>  
  17.     --></span>  
  18.       
  19.     <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">init-param</span>></span>  
  20.       <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">param-name</span>></span>debug<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">param-name</span>></span>  
  21.       <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">param-value</span>></span>3<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">param-value</span>></span>  
  22.     <span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">init-param</span>></span>  
  23.     <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">init-param</span>></span>  
  24.       <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">param-name</span>></span>detail<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">param-name</span>></span>  
  25.       <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">param-value</span>></span>3<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">param-value</span>></span>  
  26.     <span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">init-param</span>></span>  
  27.     <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">load-on-startup</span>></span>0<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">load-on-startup</span>></span>  
  28.   <span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet</span>></span>  
  29.     
  30.   <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet-mapping</span>></span>  
  31.     <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet-name</span>></span>action<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet-name</span>></span>  
  32.     <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">url-pattern</span>></span>*.do<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">url-pattern</span>></span>  
  33.   <span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet-mapping</span>></span>  
  34.   <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">filter</span>></span>  
  35. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">filter-name</span>></span>CharsetFilter<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">filter-name</span>></span>  
  36. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">filter-class</span>></span>  
  37. com.lovo.struts.filter.CharsetFilter  
  38. <span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">filter-class</span>></span>  
  39. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">init-param</span>></span>  
  40. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">param-name</span>></span>encoding<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">param-name</span>></span>  
  41. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">param-value</span>></span>utf-8<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">param-value</span>></span>  
  42. <span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">init-param</span>></span>  
  43. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">init-param</span>></span>  
  44. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">param-name</span>></span>ignore<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">param-name</span>></span>  
  45. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">param-value</span>></span>true<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">param-value</span>></span>  
  46. <span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">init-param</span>></span>  
  47. <span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">filter</span>></span>  
  48. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">filter-mapping</span>></span>  
  49. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">filter-name</span>></span>CharsetFilter<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">filter-name</span>></span>  
  50. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">url-pattern</span>></span>*.do<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">url-pattern</span>></span>  
  51. <span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">filter-mapping</span>></span>  
  52. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">filter-mapping</span>></span>  
  53. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">filter-name</span>></span>CharsetFilter<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">filter-name</span>></span>  
  54. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">url-pattern</span>></span>*.jsp<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">url-pattern</span>></span>  
  55. <span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">filter-mapping</span>></span>  
  56.   <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">welcome-file-list</span>></span>  
  57.     <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">welcome-file</span>></span>index.jsp<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">welcome-file</span>></span>  
  58.   <span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">welcome-file-list</span>></span>  
  59. <span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">web-app</span>></span></code>  

注:从上面的配置中我们可以看出,servlet的配置是必不可少的。

3.控制器:ActionServletstruts1的中央核心控制器,它在web.xml中配置成自动启动的Servlet,在启动中央核心控制器时会读取配置文件(struts-config.xml)的配置信息,为struts中不同的模块初始化相应的对象。 

6.类型转换:LoginForm loginF = (LoginForm)form;

7.有效性验证:验证框架,不能重写Validate方法。也可以自定义验证规则,但是必须做配置。

9.返回:Action将业务处理的不同结果返回一个目标响应对象给中央核心控制器,或者返回一个页面mapping.findForward("success")

10.js页面做显示:EL表达式,自定义标签库,直接在jsp页面上写java代码。

11.标签:                   

MVC三层架构在各框架中的特征_第3张图片


3.基于web开发中Struts2框架的结构图:


MVC三层架构在各框架中的特征_第4张图片

1.从结构上分析Struts2图解原理

      Struts2的思想是基于POJO(简单的老的Java对象)的一种新思想而产生的。一度影响了重量级的容器。它的实现是靠用简单的javabean对象去完成各种复杂的功能。我们也可以把这一实现的过程称为POJO

Struts2中是没有容器的。当客户端发送一个请求时,在服务器上会经过层层过滤器,最后到达FilterDispatcher(Struts2中的中央核心控制器),中央核心控制器在通过层层的拦截器去找相应的Action,最后Action返给我们一个所访问的页面,由于在Struts2中的拦截器是上下文相互环绕的,所以,在当Action中的Resulet返给我们一个页面的同时还会在次进行后期的拦截器处理,就这样一层层的拦截,直到将最终所取得的信息传给HttpServletResponse,在接着就是HttpServletResponse会将得到的信息响应给HttpServletRequest.但在这个响应的过程中也再次经过了过滤器的过滤。直到信息完整的到达HttpServletRequest

2web.xml配置上:

[xml] view plain copy print ?
  1. <code class="hljs xml" style="padding:0.5em; margin:0px; display:block; color:rgb(101,123,131); overflow-x:auto; background:rgb(253,246,227)">  <span class="hljs-pi" style="padding:0px; margin:0px; color:rgb(147,161,161)"><?xml version="1.0" encoding="UTF-8"?></span>  
  2.   <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">web-app version="2.5" xmlns="http:</span>//<span class="hljs-attribute" style="padding:0px; margin:0px; color:rgb(181,137,0)">java.sun.com</span>/<span class="hljs-attribute" style="padding:0px; margin:0px; color:rgb(181,137,0)">xml</span>/<span class="hljs-attribute" style="padding:0px; margin:0px; color:rgb(181,137,0)">ns</span>/<span class="hljs-attribute" style="padding:0px; margin:0px; color:rgb(181,137,0)">javaee</span>"  
  3. <span class="hljs-attribute" style="padding:0px; margin:0px; color:rgb(181,137,0)">xmlns:xsi</span>=<span class="hljs-value" style="padding:0px; margin:0px; color:rgb(42,161,152)">"http://www.w3.org/2001/XMLSchema-instance"</span>  
  4. <span class="hljs-attribute" style="padding:0px; margin:0px; color:rgb(181,137,0)">xsi:schemaLocation</span>=<span class="hljs-value" style="padding:0px; margin:0px; color:rgb(42,161,152)">"http://java.sun.com/xml/ns/javaee   
  5. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"</span>></span>  
  6. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">filter</span>></span>  
  7. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">filter-name</span>></span>struts2<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">filter-name</span>></span>  
  8. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">filter-class</span>></span>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">filter-class</span>></span>  
  9. <span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">filter</span>></span>  
  10. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">filter-mapping</span>></span>  
  11.         <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">filter-name</span>></span>struts2<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">filter-name</span>></span>  
  12.         <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">url-pattern</span>></span>/*<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">url-pattern</span>></span>  
  13.         <span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">filter-mapping</span>></span>  
  14. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">welcome-file-list</span>></span>  
  15. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">welcome-file</span>></span>index.jsp<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">welcome-file</span>></span>  
  16. <span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">welcome-file-list</span>></span>  
  17. <span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">web-app</span>></span></code>  

3.控制器:核心控制器:FilterDispatcher在web应用中负责拦截所有的用户请求如果用户的请求以 .ation结尾,则该请求被struts2框架处理业务控制器: Action(在struts2中起作用的实际上不是用户自定义的Action,而是系统自动生成的action代理,但该代理是以用户自定义的action为目标的) 

4.作用域对象:有前面的struts1中我们可以从Action的execute方法中将请求和响应当做参数传递给了Action,但是在Struts2中我们会发现Action的execute方法是没有任何的参数,所以也就无法得到作用域对象。那么,Struts2又是如何得到作用域对象的呢?分析:Struts2的过滤器启动的时候,首先就会初始化一个叫做ServletActionContext的类。它掌管着所有的作用域对象。代码展示如下:

[java] view plain copy print ?
  1. <code class="hljs java" style="padding:0.5em; margin:0px; display:block; color:rgb(101,123,131); overflow-x:auto; background:rgb(253,246,227)"><span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">import</span> org.apache.struts2.ServletActionContext;  
  2. <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">public</span> <span class="hljs-class" style="padding:0px; margin:0px"><span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">class</span> <span class="hljs-title" style="padding:0px; margin:0px; color:rgb(181,137,0)">ContextAction</span> <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">implements</span> <span class="hljs-title" style="padding:0px; margin:0px; color:rgb(181,137,0)">Action</span> </span>{  
  3.     <span class="hljs-function" style="padding:0px; margin:0px"><span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">public</span> String <span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">execute</span><span class="hljs-params" style="padding:0px; margin:0px">()</span> <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">throws</span> Exception </span>{  
  4.         <span class="hljs-comment" style="padding:0px; margin:0px; color:rgb(147,161,161)">//获得请求       </span>  
  5. HttpServletRequest request = ServletActionContext.getRequest();  
  6.         <span class="hljs-comment" style="padding:0px; margin:0px; color:rgb(147,161,161)">//获得会话        </span>  
  7. HttpSession session = request.getSession();  
  8.         <span class="hljs-comment" style="padding:0px; margin:0px; color:rgb(147,161,161)">//获得上下文对象       </span>  
  9. ServletContext application = ServletActionContext.getServletContext();  
  10.       }  
  11. }</code>  

10.js页面做显示:OGNL

11.标签:OGNL表达式、EL表达式、Struts2框架自身提供的标签库、html标签。

MVC三层架构在各框架中的特征_第5张图片

1.从结构上分析springMVC图解原理:

      springMVC的设计思想被评委教科书式的框架,它是最能体现出MVC设计模式中的分离与互用的极致。所以,基于springMVC的原理我们是非常有必要进行掌握并深入理解的。我们必须清楚的了解spring的MVC框架主要由DispatcherServlet、处理器映射、处理器、视图解析器、视图组成浏览器发送请求到spring容器,spring容器通过在web.xml配置文件中找到中央核心控制器(DispatcherServlet)的url配置,并且在web.xml中还必须要配置一个初始化的加载参数(详见下面2),当DispatcherServlet接收到一个请求后,它会通过请求的HandlerMapping处理映射路径去找到在spring-servlet.xml配置文件中的一个处理器(Controller),该处理器会找到具体的某一个Xxxservlet,Xxxservlet会将所获取到的信息间接的传给DispatcherServlet,在这一个过程中会有一个ModelAndView的对象产生(体现出了springMVC分离的思想)。当DispatcherServlet得到这个ModelAndView后会将信息传给ViewResolver,这时ViewResolver会将符合格式的view返回出来,最后再将符合格式的数据响应到浏览器端。 

2web.xml配置上:

[xml] view plain copy print ?
  1. <code class="hljs xml" style="padding:0.5em; margin:0px; display:block; color:rgb(101,123,131); overflow-x:auto; background:rgb(253,246,227)"> <span class="hljs-pi" style="padding:0px; margin:0px; color:rgb(147,161,161)"><?xml version="1.0" encoding="UTF-8"?></span>  
  2.     <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">web-app version="2.5" xmlns="http:</span>//<span class="hljs-attribute" style="padding:0px; margin:0px; color:rgb(181,137,0)">java.sun.com</span>/<span class="hljs-attribute" style="padding:0px; margin:0px; color:rgb(181,137,0)">xml</span>/<span class="hljs-attribute" style="padding:0px; margin:0px; color:rgb(181,137,0)">ns</span>/<span class="hljs-attribute" style="padding:0px; margin:0px; color:rgb(181,137,0)">javaee</span>"  
  3. <span class="hljs-attribute" style="padding:0px; margin:0px; color:rgb(181,137,0)">xmlns:xsi</span>=<span class="hljs-value" style="padding:0px; margin:0px; color:rgb(42,161,152)">"http://www.w3.org/2001/XMLSchema-instance"</span>  
  4. <span class="hljs-attribute" style="padding:0px; margin:0px; color:rgb(181,137,0)">xsi:schemaLocation</span>=<span class="hljs-value" style="padding:0px; margin:0px; color:rgb(42,161,152)">"http://java.sun.com/xml/ns/javaee   
  5. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"</span>></span>  
  6. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet</span>></span>  
  7. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet-name</span>></span>mySpring<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet-name</span>></span>  
  8. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet-class</span>></span>org.springframework.web.servlet.DispatcherServlet<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet-class</span>></span>  
  9. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">init-param</span>></span>  
  10. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">param-name</span>></span>contextConfigLocation<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">param-name</span>></span>  
  11. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">param-value</span>></span>/WEB-INF/classes/mySpring-servlet.xml<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">param-value</span>></span>  
  12. <span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">init-param</span>></span>  
  13. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">load-on-startup</span>></span>1<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">load-on-startup</span>></span>  
  14. <span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet</span>></span>  
  15. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet-mapping</span>></span>  
  16. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet-name</span>></span>mySpring<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet-name</span>></span>  
  17. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">url-pattern</span>></span>*.spring<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">url-pattern</span>></span>  
  18. <span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">servlet-mapping</span>></span>  
  19. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">welcome-file-list</span>></span>  
  20. <span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">welcome-file</span>></span>index.jsp<span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">welcome-file</span>></span>  
  21. <span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">welcome-file-list</span>></span>  
  22.       <span class="hljs-tag" style="padding:0px; margin:0px"></<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">web-app</span>></span>  
  23. 注:<span class="hljs-tag" style="padding:0px; margin:0px"><<span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">init-param</span>></span>参数的配置是必不可少的。必须是在/WEB-INF目录下的classes子目录下的我们自己定义的  
  24. 哪一篇xxx-servlet.xml文件。</code>  

3.控制器DispatcherServlet,该控制器的作用是将请求转发到相应的应用控制器。       

4.作用域对象:springMVC中不推荐使用。

5.数据绑定:springMVC支持的数据绑定有6种,1.有基本数据类型绑定(StringString[])。2.简单对象类型绑定(类似与struts1中的ActionForm)。3.List类型绑定(必须创建ArrayList对象来进行绑定)4.Set类型绑定(必须在Set对象中Add相应的数量的模型对象)。5.Map类型绑定(必须依靠Bean对象)。6.复合数据类型绑定(要求在自己定义的Bean对象上的某个属性也必须是一个Bean对象)。

6.类型转换:springMVC中有两种类型转换的方式.1.属性编辑器(propertyEditor,属性编辑器是我们最传统的做法。我们只需要通过一个@InitBider注解来完成,方法中的参数是WebRequestDateBinder或者WebDateBinder。在这个方法中,我们可以通过重写PropertyEditorSupport中的setAsText()来定义自己的转换规则。

2.类型转换器(Converter),相对于第一种属性编辑器它的功能更大,它可以对任何类型的数据进行转换。我们在编写一个类型转换器时,一定要去implements它的Converter接口。 

7.有效性验证:通过注解的形式来注入@pattern@valid来进行有效性验证,这也是我们常用到的验证注入。

1.导包

2.书写配置springMVC自己提供的文件上传的配置。 

[xml] view plain copy print ?
  1. <code class="hljs applescript" style="padding:0.5em; margin:0px; display:block; color:rgb(101,123,131); overflow-x:auto; background:rgb(253,246,227)"><bean <span class="hljs-property" style="padding:0px; margin:0px">id</span>=<span class="hljs-string" style="padding:0px; margin:0px; color:rgb(42,161,152)">"multipartResolver"</span>  
  2.       <span class="hljs-type" style="padding:0px; margin:0px">class</span>=<span class="hljs-string" style="padding:0px; margin:0px; color:rgb(42,161,152)">"org.springframework.web.multipart.commons.CommonsMultipartResolver"</span>>  
  3.  <<span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">property</span> <span class="hljs-property" style="padding:0px; margin:0px">name</span>=<span class="hljs-string" style="padding:0px; margin:0px; color:rgb(42,161,152)">"maxUploadSize"</span> value=<span class="hljs-string" style="padding:0px; margin:0px; color:rgb(42,161,152)">"100000000"</span>>  
  4.  </<span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">property</span>>  
  5. </bean></code>  

3.jsp页面,注意文件上传的<form>表单里面的属性。

[xml] view plain copy print ?
  1. <code class="hljs nimrod" style="padding:0.5em; margin:0px; display:block; color:rgb(101,123,131); overflow-x:auto; background:rgb(253,246,227)">         <form action=<span class="hljs-string" style="padding:0px; margin:0px; color:rgb(42,161,152)">"upload.spring"</span> name=<span class="hljs-string" style="padding:0px; margin:0px; color:rgb(42,161,152)">"uploadForm"</span>   
  2.            <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">method</span>=<span class="hljs-string" style="padding:0px; margin:0px; color:rgb(42,161,152)">"post"</span> enctype=<span class="hljs-string" style="padding:0px; margin:0px; color:rgb(42,161,152)">"multipart/form-data"</span>>  
  3.            ............  
  4.          </form></code>  

4.书写控制器UploadController在控制器的方法处理中,从作用域中获取file文件,通过path路径和文件名将文件存入到当前工程的文件夹下。

[java] view plain copy print ?
  1. <code class="hljs gradle" style="padding:0.5em; margin:0px; display:block; color:rgb(101,123,131); overflow-x:auto; background:rgb(253,246,227)"><span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">package</span> com.lh.controller;  
  2.    
  3. <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">import</span> java.io.<span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">File</span>;  
  4.    
  5. <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">import</span> javax.servlet.http.HttpServletRequest;  
  6.    
  7. <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">import</span> org.springframework.stereotype.Controller;  
  8. <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">import</span> org.springframework.web.bind.annotation.RequestMapping;  
  9. <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">import</span> org.springframework.web.bind.annotation.RequestParam;  
  10. <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">import</span> org.springframework.web.multipart.commons.CommonsMultipartFile;  
  11.    
  12. @Controller  
  13. <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">public</span> <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">class</span> UploadController {  
  14. @RequestMapping(<span class="hljs-string" style="padding:0px; margin:0px; color:rgb(42,161,152)">"/upload"</span>)  
  15. <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">public</span> String upload(HttpServletRequest request  
  16. ,@RequestParam(<span class="hljs-string" style="padding:0px; margin:0px; color:rgb(42,161,152)">"file"</span>)CommonsMultipartFile <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">file</span>){  
  17. <span class="hljs-comment" style="padding:0px; margin:0px; color:rgb(147,161,161)">//从作用域中获取文件的path路径,通过request.getSession().getServletContext().getRealPath("upload")。</span>  
  18. String path = request.getSession().getServletContext().getRealPath(<span class="hljs-string" style="padding:0px; margin:0px; color:rgb(42,161,152)">"upload"</span>);  
  19. <span class="hljs-comment" style="padding:0px; margin:0px; color:rgb(147,161,161)">//通过file来获取文件名。</span>  
  20. String name = <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">file</span>.getOriginalFilename();  
  21. <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">try</span> {  
  22. <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">file</span>.getFileItem().<span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">write</span>(<span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">new</span> <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">File</span>(path+<span class="hljs-string" style="padding:0px; margin:0px; color:rgb(42,161,152)">"/"</span>+name));  
  23. } <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">catch</span> (Exception e) {  
  24. <span class="hljs-comment" style="padding:0px; margin:0px; color:rgb(147,161,161)">// TODO Auto-generated catch block</span>  
  25. e.printStackTrace();  
  26. <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">return</span> <span class="hljs-string" style="padding:0px; margin:0px; color:rgb(42,161,152)">"upload"</span>;  
  27. }  
  28. <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">return</span> <span class="hljs-string" style="padding:0px; margin:0px; color:rgb(42,161,152)">"success"</span>;  
  29. }   
  30. }</code>  

5.测试文件是否上传成功。

在springMVC中实现拦截器,调用它的3个方法。:

[java] view plain copy print ?
  1. <code class="hljs monkey" style="padding:0.5em; margin:0px; display:block; color:rgb(101,123,131); overflow-x:auto; background:rgb(253,246,227)"><span class="hljs-number" style="padding:0px; margin:0px; color:rgb(42,161,152)">1</span>.书写拦截器有两种方式:  
  2. <span class="hljs-number" style="padding:0px; margin:0px; color:rgb(42,161,152)">1</span>-<span class="hljs-number" style="padding:0px; margin:0px; color:rgb(42,161,152)">1</span>继承HandlerInterceptorAdaptor类,代码展示如下:  
  3. package com.lh.interceptor;  
  4. <span class="hljs-preprocessor" style="padding:0px; margin:0px; color:rgb(203,75,22)"><span class="hljs-keyword" style="padding:0px; margin:0px">import</span> javax.servlet.http.HttpServletRequest;</span>  
  5. <span class="hljs-preprocessor" style="padding:0px; margin:0px; color:rgb(203,75,22)"><span class="hljs-keyword" style="padding:0px; margin:0px">import</span> javax.servlet.http.HttpServletResponse;</span>  
  6. <span class="hljs-preprocessor" style="padding:0px; margin:0px; color:rgb(203,75,22)"><span class="hljs-keyword" style="padding:0px; margin:0px">import</span> org.springframework.web.servlet.ModelAndView;</span>  
  7. <span class="hljs-preprocessor" style="padding:0px; margin:0px; color:rgb(203,75,22)"><span class="hljs-keyword" style="padding:0px; margin:0px">import</span> org.springframework.web.servlet.handler.HandlerInterceptorAdapter;</span>  
  8. <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">public</span> <span class="hljs-class" style="padding:0px; margin:0px"><span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">class</span> <span class="hljs-title" style="padding:0px; margin:0px; color:rgb(181,137,0)">Interceptor1</span> </span>  
  9. extends HandlerInterceptorAdapter  
  10.  {</code>  

[java] view plain copy print ?
  1. <code class="hljs processing" style="padding:0.5em; margin:0px; display:block; color:rgb(101,123,131); overflow-x:auto; background:rgb(253,246,227)">   <span class="hljs-comment" style="padding:0px; margin:0px; color:rgb(147,161,161)">//在控制器传递到拦截器将数据交给DisPacheServlet的过程中将资源清理。</span>  
  2.     <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">public</span> <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">void</span>   
  3. afterCompletion(HttpServletRequest request,  
  4.             HttpServletResponse response, <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">Object</span> handler, Exception ex)  
  5.             <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">throws</span> Exception {  
  6.     System.out.<span class="hljs-built_in" style="padding:0px; margin:0px; color:rgb(38,139,210)">println</span>(<span class="hljs-string" style="padding:0px; margin:0px; color:rgb(42,161,152)">"The Main of claening...."</span>);  
  7.     }  
  8.    <span class="hljs-comment" style="padding:0px; margin:0px; color:rgb(147,161,161)">//在方法之后</span>  
  9.     <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">public</span> <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">void</span>   
  10. postHandle(HttpServletRequest request,  
  11.             HttpServletResponse response, <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">Object</span> handler,  
  12.             ModelAndView modelAndView)  
  13.  <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">throws</span> Exception {  
  14.         System.out.<span class="hljs-built_in" style="padding:0px; margin:0px; color:rgb(38,139,210)">println</span>(<span class="hljs-string" style="padding:0px; margin:0px; color:rgb(42,161,152)">"The Main of after..."</span>);         
  15.     }  
  16. <span class="hljs-comment" style="padding:0px; margin:0px; color:rgb(147,161,161)">//在方法之前</span>  
  17. <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">public</span> <span class="hljs-built_in" style="padding:0px; margin:0px; color:rgb(38,139,210)">boolean</span>   
  18. preHandle(HttpServletRequest request,  
  19.         HttpServletResponse response, <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">Object</span> handler)  
  20.  <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">throws</span> Exception {  
  21.     System.out.<span class="hljs-built_in" style="padding:0px; margin:0px; color:rgb(38,139,210)">println</span>(<span class="hljs-string" style="padding:0px; margin:0px; color:rgb(42,161,152)">"The Main of Before...."</span>);  
  22.     <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">return</span> <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">true</span>;  
  23. }  
  24. }</code>  

1-2.实现HandlerInterceptor接口 代码展示如下:

[java] view plain copy print ?
  1. <code class="hljs java" style="padding:0.5em; margin:0px; display:block; color:rgb(101,123,131); overflow-x:auto; background:rgb(253,246,227)"><span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">package</span> com.lh.interceptor;  
  2. <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">import</span> javax.servlet.http.HttpServletRequest;  
  3. <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">import</span> javax.servlet.http.HttpServletResponse;  
  4. <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">import</span> org.springframework.web.servlet.HandlerInterceptor;  
  5. <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">import</span> org.springframework.web.servlet.ModelAndView;  
  6. <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">public</span> <span class="hljs-class" style="padding:0px; margin:0px"><span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">class</span> <span class="hljs-title" style="padding:0px; margin:0px; color:rgb(181,137,0)">Interceptor2</span>   
  7. <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">implements</span> <span class="hljs-title" style="padding:0px; margin:0px; color:rgb(181,137,0)">HandlerInterceptor</span>  
  8. </span>{  
  9.    <span class="hljs-comment" style="padding:0px; margin:0px; color:rgb(147,161,161)">//在控制器传递到拦截器将数据交给DisPacheServlet的过程中将资源清理。</span>  
  10.     <span class="hljs-function" style="padding:0px; margin:0px"><span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">public</span> <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">void</span> <span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">afterCompletion</span><span class="hljs-params" style="padding:0px; margin:0px">(HttpServletRequest arg0,  
  11.             HttpServletResponse arg1, Object arg2, Exception arg3)</span>  
  12.             <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">throws</span> Exception </span>{  
  13.         System.out.println(<span class="hljs-string" style="padding:0px; margin:0px; color:rgb(42,161,152)">"The Mian of cleaning2....."</span>);         
  14.     }  
  15.    <span class="hljs-comment" style="padding:0px; margin:0px; color:rgb(147,161,161)">//在方法之后</span>  
  16.     <span class="hljs-function" style="padding:0px; margin:0px"><span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">public</span> <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">void</span> <span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">postHandle</span><span class="hljs-params" style="padding:0px; margin:0px">(HttpServletRequest arg0, HttpServletResponse arg1,  
  17.             Object arg2, ModelAndView arg3)</span> <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">throws</span> Exception </span>{  
  18.     System.out.println(<span class="hljs-string" style="padding:0px; margin:0px; color:rgb(42,161,152)">"The Mian of After..."</span>);         
  19.     }  
  20.     <span class="hljs-comment" style="padding:0px; margin:0px; color:rgb(147,161,161)">//在方法之前</span>  
  21.     <span class="hljs-function" style="padding:0px; margin:0px"><span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">public</span> <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">boolean</span> <span class="hljs-title" style="padding:0px; margin:0px; color:rgb(38,139,210)">preHandle</span><span class="hljs-params" style="padding:0px; margin:0px">(HttpServletRequest arg0, HttpServletResponse arg1,  
  22.             Object arg2)</span> <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">throws</span> Exception </span>{  
  23.         System.out.println(<span class="hljs-string" style="padding:0px; margin:0px; color:rgb(42,161,152)">"The Mian of Befor....."</span>);  
  24.         <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">return</span> <span class="hljs-keyword" style="padding:0px; margin:0px; color:rgb(133,153,0)">true</span>;  
  25.     }  
  26. }</code>  

2. 在xml中配置拦截器:

[xml] view plain copy print ?
  1. <code class="hljs apache" style="padding:0.5em; margin:0px; display:block; color:rgb(101,123,131); overflow-x:auto; background:rgb(253,246,227)">  <span class="hljs-tag" style="padding:0px; margin:0px"><mvc:interceptors></span>  
  2.       <span class="hljs-tag" style="padding:0px; margin:0px"><mvc:interceptor></span>  
  3.         <span class="hljs-tag" style="padding:0px; margin:0px"><mvc:mapping path="/register.spring"></span><span class="hljs-tag" style="padding:0px; margin:0px"></mvc:mapping></span>  
  4.         <span class="hljs-tag" style="padding:0px; margin:0px"><bean class="com.lh.interceptor.Interceptor1"></span><span class="hljs-tag" style="padding:0px; margin:0px"></bean></span>  
  5.       <span class="hljs-tag" style="padding:0px; margin:0px"></mvc:interceptor></span>      
  6.   <span class="hljs-tag" style="padding:0px; margin:0px"></mvc:interceptors></span></code>  

  注:如果有多个拦截器就需要配置拦截器栈(先进后出)。
3.在tomcat服务器上发布,测试拦截效果是否达到。

文章转载自:http://blog.csdn.net/u012562943/article/details/51392772

你可能感兴趣的:(java,mvc)