你的位置:首页 > Java教程

[Java教程]springMVC学习篇

springMVC是spring家族中一个重要的组件,和struts一样作为一套前台框架被广泛的应用于各种项目。

之前在很多项目组都用到springMVC,只感觉很强大,但是对这套框架的知识了解比较少。这几天项目组没那么忙正学习下,过程中遇到很多问题也查找不少资料,整理出来与大家共享。

本文介绍简单springmvc框架环境搭建和我在学习中遇到问题查找资料时间比较长的一些点,这里这称之为关键点。

一、搭建环境

1、下载需要的jar包

①需要的最少spring mvc jar包

②maven pom.

 1 <dependencies> 2    <dependency> 3      <groupId>org.springframework</groupId> 4      <artifactId>spring-webmvc</artifactId> 5      <version>3.0.1.RELEASE</version> 6    </dependency> 7    <dependency> 8      <groupId>jstl</groupId> 9      <artifactId>jstl</artifactId>10      <version>1.2</version>11    </dependency>12 </dependencies>

 

2、web.

 1 <??> 2 <web-app ="http://www.w3.org/2001/  3   ="http://java.sun.com/  4   xsi:schemaLocation="http://java.sun.com/  5     id="WebApp_ID" version="2.5"> 6   <display-name>deposit</display-name> 7    8   <servlet-mapping> 9     <servlet-name>default</servlet-name>10     <url-pattern>/scripts/*</url-pattern>11     <url-pattern>/styles/*</url-pattern>12   </servlet-mapping>13   14   <!--初始化spring的配置文件-->15   <context-param>16     <param-name>contextConfigLocation</param-name>17     <param-value>classpath:spring/applicationContext.</param-value>18   </context-param>19   <!--启动spring容器-->20   <listener>21     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>22   </listener>23   24   <!-- 初始化DispatcherServlet -->25   <servlet>26     <servlet-name>springmvc</servlet-name>27     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>28     <!--springmvc框架在 web应用程序WEB-INF目录中寻找一个名为[servlet-name]-servlet.29       <init-param>30         <param-name>contextConfigLocation</param-name>31         <param-value>classpath:spring/springmvc/springmvc-servlet.32       </init-param>-->33     <load-on-startup>1</load-on-startup>34   </servlet>35   <servlet-mapping>36     <servlet-name>springmvc</servlet-name>37     <url-pattern>/*</url-pattern>38   </servlet-mapping>39 </web-app>

 

3、springmvc-servlet.

 1 <??> 2 <beans ="http://www.springframework.org/schema/beans" 3   ="http://www.w3.org/2001/ 4   ="http://www.springframework.org/schema/context" 5   ="http://www.springframework.org/schema/mvc" 6   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 8   http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 9   10   <!-- 启用spring mvc 注解 -->11   <context:annotation-config />12   <!-- 设置使用注解的类所在的jar包,注入view层控制类 -->13   <context:component-scan base-package="cn.tancp.framework.springmvc.controller" />14 15   <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->16   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">17     <property name="prefix" value="/WEB-INF/"/>18     <property name="suffix" value=".jsp"></property>19   </bean>20   21   <!--注册springmvc拦截类-->22   <mvc:interceptors>23     <bean class="cn.tancp.framework.springmvc.interceptor.CommonInterceptor" />24   </mvc:interceptors>25 </beans>

 

4、applicationContext.

 1 <??>   2 <beans   3   ="http://www.springframework.org/schema/beans"   4   ="http://www.w3.org/2001/   5   ="http://www.springframework.org/schema/context"   6   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   7   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">   8     9   <!-- 设置使用注解的类所在的jar包,注入service层类 -->10   <context:component-scan base-package="cn.tancp.framework.springmvc.service" />11 </beans>

 

5、controller类

 1 package cn.tancp.framework.springmvc.controller; 2  3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.RequestMethod; 6  7 @Controller 8 public class SystemController { 9 10   @RequestMapping(value = "/",method = RequestMethod.GET)11   public String home() {12     return "index";13   }14   15   @RequestMapping(value = "/login", method = RequestMethod.GET)16   public String login() {17     return "login";18   }19 }

 

6、springmvc拦截器

 1 package cn.tancp.framework.springmvc.interceptor; 2  3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 import org.springframework.web.servlet.ModelAndView; 6 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; 7  8 public class CommonInterceptor extends HandlerInterceptorAdapter { 9   10   @Override11   public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,12       Object arg2) throws Exception {13     System.out.println("Pre-Handle");14     return true;15   }16   17   @Override18   public void afterCompletion(HttpServletRequest arg0,19       HttpServletResponse arg1, Object arg2, Exception arg3)20       throws Exception {21     System.out.println("After-Completion");22   }23 24   @Override25   public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,26       Object arg2, ModelAndView arg3) throws Exception {27     System.out.println("Post-Handle");28   }29 }

 

二、关键点

1、使用

 

2、DispatcherServlet路径配置

 

在研究springmvc拦截器时,想配置映射为“/”访问项目根路径方法,但始终会访问到index.jsp页面上去,后来发现这个路径配置成了*.shtml

原因:之前以为配置*.shtml只会让后缀名为shtml的链接访问,而实际只会将*.shtml springMVC容器内。

 

3、

classpath 代表 /WEB-INF /classes/ 这个路径      常用的场景:      在SSH架构中,配置spring的上下文环境:        <context-param>          <param-name>contextConfigLocation</param-name>          <param-value>classpath:applicationContext.</param-value>        </context-param>      里面的 classpath:applicationContext.

 

4、HandlerInterceptor接口preHandle方法返回值

 在配置springmvc拦截器时如果preHandle方法的返回值是false,则不会跳到相应被拦截的页面,也不会进入下一个拦截器

 

5、SpringMVC的springmvc-servlet.

如下:
springmvc-servlet.

<!-- 设置使用注解的类所在的jar包,注入view层控制类 --><context:component-scan base-package="cn.tancp.framework.springMVC.controller" />

Spring MVC启动时的配置文件,包含组件扫描、url映射以及设置freemarker参数,让spring不扫描带有@Service注解的类。

因为springmvc-servlet.同样applicationContext.

<!-- 设置使用注解的类所在的jar包,注入service层类 --><context:component-scan base-package="cn.tancp.framework.springmvc.service" />

扫描包路径,不扫描带有@Controller注解的类。因为这些类已经随容器启动时,在springmvc.