spring mvc

mvc

mvc是web程序开发一种通用的架构。mvc的核心思想:业务数据的抽取和业务数据呈现相分离。

spring MVC基本概念





理解过程:浏览器的request过来,DispatcherServlet拦截request,DispatcherServlet通过HandlerMapping去寻找controller,将功能代理给HandlerMapping。 HandlerMapping找到了controller和HandlerInterceptor,形成一个执行链条,作为一个HandlerAdapyer。 写controller就是为了生成相应的model,返回给DispatcherServlet,然后通过视图解析器, 返回view对象,将模型数据传给view,就呈现我们的页面。

基于maven的spring MVC 例子

  • 打开myeclipse,新建一个maven的工程,如何在myeclipse中建立一个maven工程,详细的请看maven中讲解。
  • 配置整个工程需要的依赖坐标,具体的配置信息如下:
[java]  view plain  copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>com.example.test.springmvc</groupId>  
  5.     <artifactId>springmvc</artifactId>  
  6.     <!-- <packaging>war</packaging> -->  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <name>springmvc Maven Webapp</name>  
  9.     <url>http://maven.apache.org</url>  
  10.     <properties>  
  11.         <spring.version>4.3.10.RELEASE</spring.version>  
  12.     </properties>  
  13.     <dependencies>  
  14.         <dependency>  
  15.             <groupId>org.springframework</groupId>  
  16.             <artifactId>spring-framework-bom</artifactId>  
  17.             <version>4.3.10.RELEASE</version>  
  18.             <type>pom</type>  
  19.         </dependency>  
  20.         <dependency>  
  21.             <groupId>org.springframework</groupId>  
  22.             <artifactId>spring-webmvc</artifactId>  
  23.             <version>4.3.10.RELEASE</version>  
  24.         </dependency>  
  25.         <dependency>  
  26.             <groupId>junit</groupId>  
  27.             <artifactId>junit</artifactId>  
  28.             <version>3.8.1</version>  
  29.             <scope>test</scope>  
  30.         </dependency>  
  31.         <dependency>  
  32.             <groupId>javax.servlet</groupId>  
  33.             <artifactId>javax.servlet-api</artifactId>  
  34.             <version>3.0.1</version>  
  35.             <scope>provided</scope>  
  36.         </dependency>  
  37.         <dependency>  
  38.             <groupId>commons-lang</groupId>  
  39.             <artifactId>commons-lang</artifactId>  
  40.             <version>2.6</version>  
  41.         </dependency>  
  42.         <dependency>  
  43.             <groupId>org.slf4j</groupId><!--基于日志管理的包 -->  
  44.             <artifactId>slf4j-log4j12</artifactId>  
  45.             <version>1.7.25</version>  
  46.             <scope>test</scope>  
  47.         </dependency>  
  48.   
  49.     </dependencies>  
  50.     <build>  
  51.         <finalName>springmvc</finalName>  
  52.         <plugins>  
  53.             <plugin>  
  54.                 <groupId>org.mortbay.jetty</groupId>  
  55.                 <artifactId>maven-jetty-plugin</artifactId>  
  56.                 <version>6.1.25</version>  
  57.             </plugin>  
  58.         </plugins>  
  59.     </build>  
  60. </project>  

  • 在web.xml下配置DispatherServlet(前端控制器)
[java]  view plain  copy
  1. <!DOCTYPE web-app PUBLIC  
  2.  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  
  3.  "http://java.sun.com/dtd/web-app_2_3.dtd" >  
  4.   
  5. <web-app>  
  6.     <display-name>Archetype Created Web Application</display-name>  
  7.   
  8.     <servlet>  
  9.         <servlet-name>springmvc-dispatcher</servlet-name><!-- DispatcherServlet对应的上下文配置,默认是/WEB-INF/$servlet-name$-servlet.xml -->  
  10.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  11.         <init-param>  
  12.             <param-name>contextconfiglocation</param-name>  
  13.             <param-value>WEB-INF/config/springmvc-dispatcher-servlet.xml</param-value><pre code_snippet_id="2502422" snippet_file_name="blog_20170727_2_818388" name="code" class="java"><!-- 这里改变默认的位置,所以要在对应的位置创建该文件--></pre> </init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springmvc-dispatcher</servlet-name><url-pattern>/</url-pattern><pre code_snippet_id="2502422" snippet_file_name="blog_20170727_3_5976602" name="code" class="java"><!--这里过滤所有的请求--></pre> </servlet-mapping></web-app>  
  14. <pre></pre>  
  15. <ul>  
  16. <li>创建上述的文件,并进行配置。</li></ul>  
  17. <div><pre code_snippet_id="2502422" snippet_file_name="blog_20170727_4_7231120" name="code" class="java"></pre><pre code_snippet_id="2502422" snippet_file_name="blog_20170727_4_7231120" name="code" class="java"><?xml version="1.0" encoding="UTF-8"?>  
  18. <beans xmlns="http://www.springframework.org/schema/beans"  
  19.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  20.     xmlns:context="http://www.springframework.org/schema/context"  
  21.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  22.     xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd  
  23.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  24.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">  
  25.     <context:annotation-config/>激活注解  
  26.     <!-- 配置包扫描,扫描controller -->  
  27.     <context:component-scan base-package="com.example.springmvc">  
  28.     <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/><!-- 只用于管理@controller的bean,忽略@service等其他的bean。 -->  
  29.       
  30.     </context:component-scan>  
  31.     <!-- HandlerMapping, 无需配置, Spring MVC可以默认启动。 DefaultAnnotationHandlerMapping   
  32.         annotation-driven HandlerMapping -->  
  33.       
  34.     <!-- 注解驱动,配置映射器和适配器 --><!-- 扩充了注解驱动,可以将请求参数绑定到控制器参数 -->  
  35.     <mvc:annotation-driven></mvc:annotation-driven>  
  36.     <!-- 视图页面配置 -->  
  37.     <!-- 配置ViewResolver。 可以用多个ViewResolver。 使用order属性排序。 InternalResourceViewResolver放在最后。 -->  
  38.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  39.         <property name="prefix" value="/WEB-INF/jsp/" />  
  40.         <property name="suffix" value=".jsp" />  
  41.     </bean>  
  42. </beans></pre><br>  
  43. <br>  
  44. <ul>  
  45. <li>创建controller类</li></ul>  
  46. <div><pre code_snippet_id="2502422" snippet_file_name="blog_20170727_5_9673336" name="code" class="java">package com.example.springmvc;  
  47.   
  48. import org.springframework.stereotype.Controller;  
  49. import org.springframework.web.bind.annotation.RequestMapping;  
  50.   
  51. @Controller  
  52. @RequestMapping("/hello")  
  53. public class HelloSpringMvc {  
  54.     @RequestMapping("/mvc")  
  55.     public String helloMvc(){  
  56.         return "home";//返回的是home.jsp  
  57.     }  
  58.   
  59. }  
  60. </pre><br>  
  61. </div>  
  62. </div>  
  63. <pre></pre>  
  64. <pre></pre>  
  65. <pre></pre>  
由此可见访问地址:http://localhost:8080/项目名/hello/mvc

spring上下文配置

细心的小伙伴,在上述中,可以看到没有进行spring配置。

[java]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  5.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"></web-app>  

在web.xml中升级web-app版本为2.4,可以直接到jsp中使用er表达式。

  • 在上述的web.xml中添加spring上下文
[java]  view plain  copy
  1. <!-- 配置spring上下文 -->  
  2.     <context-param>  
  3.         <param-name>contextlocation</param-name>  
  4.         <param-value>/WEB-INF/applicationContext.xml</param-value><!-- 指明spring上下文位置 -->  
  5.     </context-param>  
  6.     <listener>  
  7.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  8.     </listener>  

  • 创建applicationContext.xml文件
[java]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  5.     xsi:schemaLocation="  
  6.         http://www.springframework.org/schema/beans  
  7.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.         http://www.springframework.org/schema/context   
  9.         http://www.springframework.org/schema/context/spring-context.xsd  
  10.         http://www.springframework.org/schema/mvc  
  11.         http://www.springframework.org/schema/mvc/spring-mvc.xsd">  
  12.   
  13.     <context:annotation-config /><!-- //激活注解 -->  
  14.   
  15.     <context:component-scan base-package="com.imooc.mvcdemo">  
  16.         <context:exclude-filter type="annotation"  
  17.             expression="org.springframework.stereotype.Controller" /><!-- 在这个spring容器中就不需要管理controller -->  
  18.     </context:component-scan>  
  19. </beans>  

spring MVC中controller的变量绑定

[java]  view plain  copy
  1. package com.example.springmvc;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4.   
  5. import org.springframework.stereotype.Controller;  
  6. import org.springframework.web.bind.annotation.PathVariable;  
  7. import org.springframework.web.bind.annotation.RequestMapping;  
  8. import org.springframework.web.bind.annotation.RequestMethod;  
  9. import org.springframework.web.bind.annotation.RequestParam;  
  10.   
  11. @Controller  
  12. @RequestMapping("/hello")  
  13. public class HelloSpringMvc {  
  14.     private String name;  
  15.       
  16.     //处理请求类型:http://localhost:8080/项目名/hello/requestone/name=  
  17.     @RequestMapping(value="/requestone",method=RequestMethod.GET)  
  18.     public String requestMethod(@RequestParam("name")String name){  
  19.         return "home";  
  20.     }  
  21.       
  22.     //处理请求类型:http://localhost:8080/项目名/hello/requestone/liu    {name}   {}是指变量name  
  23.         @RequestMapping(value="/requestone/{name}",method=RequestMethod.GET)  
  24.         public String requestMethodtwo(@PathVariable("name")String name){  
  25.             return "home";  
  26.         }  
  27.         //处理请求类型:http://localhost:8080/项目名/hello/requestthree/name=?  
  28.         @RequestMapping(value="/requestthree")  
  29.         public String requestMethodthree(HttpServletRequest request){  
  30.         String name =   request.getParameter("name");  
  31.         System.out.println(name+"sdsadsada");  
  32.             return "home";  
  33.         }  
  34.             <pre code_snippet_id="2502422" snippet_file_name="blog_20170727_9_4941017" name="code" class="java"><span style="white-space:pre">      </span>//处理请求类型:http://localhost:8080/项目名/hello/requestthree/add</pre><span style="white-space:pre"> </span>@RequestMapping(value="/create",method = RequestMethod.GET,params="add")//只有参数变量,没有值<br>  
  35. <span style="white-space:pre"></span>public String createCourse(){<br>  
  36. <span style="white-space:pre"></span>return "course/create";<br>  
  37. <span style="white-space:pre"></span>}}  
  38. <pre></pre>  
  39. <h3><a name="t5"></a>Binding数据绑定</h3>  
  40. <div>将请求中的字段按照名字匹配的原则填入模型对象。下面一个简单例子:</div>  
  41. <div></div>  
  42. <div>controller类</div>  
  43. <div></div>  
  44. <div><pre code_snippet_id="2502422" snippet_file_name="blog_20170727_10_7067587" name="code" class="java">  @RequestMapping(value="/create",method = RequestMethod.GET,params="add")  
  45.         public String createCourse(){  
  46.             return "course/create";  
  47.         }  
  48.           
  49.           
  50.         @RequestMapping(value="/save",method = RequestMethod.GET)  
  51.         public String saveCourse(Course c){  
  52.               
  53.             System.out.println(c.getName());  
  54.             return "course/show";</pre><br>  
  55. 数据实体类对象</div>  
  56. <div><br>  
  57. </div>  
  58. <div><pre code_snippet_id="2502422" snippet_file_name="blog_20170727_11_273954" name="code" class="java">package com.example.springmvc.model;  
  59.   
  60. public class Course {  
  61.   private String name;  
  62.   
  63. public String getName() {  
  64.     return name;  
  65. }  
  66.   
  67. public void setName(String name) {  
  68.     this.name = name;  
  69. }  
  70.     
  71. }  
  72. </pre><br>  
  73. <pre code_snippet_id="2502422" snippet_file_name="blog_20170727_12_2051861" name="code" class="java">  <form method="get" action="<%=request.getContextPath()%>/hello/save">  
  74.    <input type="text" name="name"/>  
  75.    <button type="submit">提交</button>  
  76.    </form></pre>  
  77. <div><br>  
  78. </div>  
  79. 注意表单元素的name属性一定要和数据实体类一致,否则不能完成数据的绑定</div>  
  80. <div><br>  
  81. </div>  
  82. <pre></pre>  

Spring mvc文件上传

提供文件上传工作,作为公共服务。只需要进行相应的配置就可以使用。

首先先在spring mvc 核心配置文件中(springmvc-dispatcher-servlet.xml),进行如下的配置:

文件解析的bean

[java]  view plain  copy
  1. <bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  2.         <property name="maxUploadSize" value="209715200" /><!-- 设置文件上传的最大字节 -->  
  3.         <property name="defaultEncoding" value="UTF-8" />  
  4.         <property name="resolveLazily" value="true" /> <!-- 设置文件是否延迟加载,来捕获大小异常 -->  
  5.     </bean>  

提供一个文件上传的表单

[java]  view plain  copy
  1. <form method="post" action="<%=request.getContextPath() %>/hello/end" enctype="multipart/form-data">  
  2.   <input type="file" name="file"/>  
  3.   <input type="submit">提交</input>  
  4.     
  5.   </form>  

controller

[java]  view plain  copy
  1. @RequestMapping(value="/startupload",method = RequestMethod.GET)  
  2.     public  String startupload(){  
  3.         return "file";  
  4.     }  
  5.       
  6.     @RequestMapping(value="/end",method = RequestMethod.POST)  
  7.     public  String endupload(@RequestParam("file")MultipartFile file){  
  8.         System.out.println(file.getOriginalFilename());  
  9.         return "course/show";  
  10.     }  

在这里利用requuestparam绑定绑定表单元素,通过MultipartFile获取文件的信息,进行文件相应的操作。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值