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

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

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

一、搭建环境

1、下载需要的jar包

①需要的最少spring mvc jar包

②maven pom.xml配置

 <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>

2、web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>deposit</display-name> <servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/scripts/*</url-pattern>
<url-pattern>/styles/*</url-pattern>
</servlet-mapping> <!--初始化spring的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<!--启动spring容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 初始化DispatcherServlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--springmvc框架在 web应用程序WEB-INF目录中寻找一个名为[servlet-name]-servlet.xml的文件,可通过<init-param>修改默认文件路径配置
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc/springmvc-servlet.xml</param-value>
</init-param>-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

3、springmvc-servlet.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 启用spring mvc 注解 -->
<context:annotation-config />
<!-- 设置使用注解的类所在的jar包,注入view层控制类 -->
<context:component-scan base-package="cn.tancp.framework.springmvc.controller" /> <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"></property>
</bean> <!--注册springmvc拦截类-->
<mvc:interceptors>
<bean class="cn.tancp.framework.springmvc.interceptor.CommonInterceptor" />
</mvc:interceptors>
</beans>

4、applicationContext.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 设置使用注解的类所在的jar包,注入service层类 -->
<context:component-scan base-package="cn.tancp.framework.springmvc.service" />
</beans>

5、controller类

 package cn.tancp.framework.springmvc.controller;

 import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; @Controller
public class SystemController { @RequestMapping(value = "/",method = RequestMethod.GET)
public String home() {
return "index";
} @RequestMapping(value = "/login", method = RequestMethod.GET)
public String login() {
return "login";
}
}

6、springmvc拦截器

 package cn.tancp.framework.springmvc.interceptor;

 import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; public class CommonInterceptor extends HandlerInterceptorAdapter { @Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2) throws Exception {
System.out.println("Pre-Handle");
return true;
} @Override
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("After-Completion");
} @Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
System.out.println("Post-Handle");
}
}

二、关键点

1、使用xml标签需要引用相应模式文档,否则报错

2、DispatcherServlet路径配置

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

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

3、xml中classpath:

classpath 代表  /WEB-INF /classes/  这个路径
常用的场景:
在SSH架构中,配置spring的上下文环境:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
里面的 classpath:applicationContext.xml 也可以使用 /WEB-INF /classes/ applicationContext.xml 代替 注意:
classpath 和 classpath* 区别:
classpath:只会到你的class路径中查找找文件;
classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找

4、HandlerInterceptor接口preHandle方法返回值

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

5、SpringMVC的springmvc-servlet.xml文件中配置扫描包,不要包含 service的注解,Spring的applicationContext.xml文件中配置扫描包,不要包含controller的注解。

如下:
springmvc-servlet.xml的配置:

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

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

因为springmvc-servlet.xml与applicationContext.xml不是同时加载,加载springmvc-servlet.xml时spring会将所有带@Service注解的类都扫描到容器中,等到加载applicationContext.xml的时候,会因为容器已经存在Service类,使得cglib将不对Service进行代理,直接导致的结果就是在applicationContext 中的事务配置不起作用。
同样applicationContext.xml的配置如下:

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

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

springMVC学习篇 - 搭建环境及关键点的更多相关文章

  1. SpringMVC 学习 十 SSM环境搭建(三)springMVC文件配置

    SpringMVC文件配置的详细过程,可以查看springMVC环境搭建的注解配置篇<springMVC学习三 注解开发环境搭建> <?xml version="1.0&q ...

  2. 深度学习之PyTorch实战(1)——基础学习及搭建环境

    最近在学习PyTorch框架,买了一本<深度学习之PyTorch实战计算机视觉>,从学习开始,小编会整理学习笔记,并博客记录,希望自己好好学完这本书,最后能熟练应用此框架. PyTorch ...

  3. SpringMVC 学习 九 SSM环境搭建 (二) Spring配置文件的编写

    spring配置文件中需要干的事情 (一)开启  Service与pojo包的注解扫描 注意:spring 扫描与表对应的实体类,以及service层的类,不能用来扫描Controller层的类,因为 ...

  4. SpringMVC 学习 八 SSM环境搭建(一) web.xml配置

    第一步:导入jar包 注意包的兼容性,以后采用maven会好很多 第二步:配置web.xml 在web.xml中,主要的配置内容有以下几点 (1)spring容器配置文件的位置 <!-- spr ...

  5. vue开发项目详细教程(第一篇 搭建环境篇)

    最近做vue做项目碰到了不少坑,看了三天文档便开始上手做项目了,不是我牛b,是因为项目紧,我没有时间去深入学习,所以只能一边学一边做了. 我要做的项目是一个官方网站(包括管理后台),也因为是我第一次使 ...

  6. 【原】Python学习_Django搭建环境及创建第一个项目

    1.Window 平台安装 Python 下载安装包    https://www.python.org/downloads/windows/ 2.Pyhton环境变量配置 右键点击"计算机 ...

  7. C 语言学习 之搭建环境和熟悉命令

    Open Terminal 打开终端To run a command as administrator (user "root"), use "sudo <comm ...

  8. Spring+SpringMVC+MyBatis深入学习及搭建(十六)——SpringMVC注解开发(高级篇)

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7085268.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十五)——S ...

  9. Zend Framework学习日记(1)--环境搭建篇(转)

    Zend Framework学习日记(1)--环境搭建篇 (1)开发工具 Zend Framework框架:http://framework.zend.com/download/latest 包含2个 ...

随机推荐

  1. HDU 5515 Game of Flying Circus 二分

    Game of Flying Circus Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem ...

  2. HDU 4813 Hard Code 水题

    Hard Code Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.act ...

  3. delphi 取得汉字的第一个字母

    功能说明://取得汉字的第一个字母 function GetPYIndexChar( hzchar:string):char;begin  caseWORD(hzchar[1])shl8+WORD(h ...

  4. 【读jQuery源码有感系列一】callee

    <script type="text/javascript"> /*调用自身*/ function calleeDemo() { try{ } catch (error ...

  5. LVS 详解

    http://zh.linuxvirtualserver.org/node/25 http://chrinux.blog.51cto.com/6466723/1198748 http://www.cn ...

  6. CentOS5.6 安装RabbitMQ

    步骤参考官方地址:http://www.rabbitmq.com/install-rpm.html我们这个版本按照官方的不能正确安装. 1.安装erlang(官网地址http://www.erlang ...

  7. BootStrap2学习日记10---单选框和复选框

    <label>选择你的性别</label> <label class="radio"> <input type="radio&q ...

  8. Intellij IDEA 使用Debug模式运行非常慢

    今天在用Debug的时候,idea运行非常慢,搜了一下有人说: 自己检查发现果然如此,把在方法前的断点去掉(移到方法体内),就正常了.

  9. Modified LCS

    Input Output Sample Input 3 5 3 4 15 3 1 10 2 2 7 3 3 100 1 1 100 1 2 Sample Output 4 3 50超时代码,因为K很大 ...

  10. java.lang.IllegalStateException

    java.lang.IllegalStateExceptionorg.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFac ...