springMVC学习篇 - 搭建环境及关键点
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学习篇 - 搭建环境及关键点的更多相关文章
- SpringMVC 学习 十 SSM环境搭建(三)springMVC文件配置
SpringMVC文件配置的详细过程,可以查看springMVC环境搭建的注解配置篇<springMVC学习三 注解开发环境搭建> <?xml version="1.0&q ...
- 深度学习之PyTorch实战(1)——基础学习及搭建环境
最近在学习PyTorch框架,买了一本<深度学习之PyTorch实战计算机视觉>,从学习开始,小编会整理学习笔记,并博客记录,希望自己好好学完这本书,最后能熟练应用此框架. PyTorch ...
- SpringMVC 学习 九 SSM环境搭建 (二) Spring配置文件的编写
spring配置文件中需要干的事情 (一)开启 Service与pojo包的注解扫描 注意:spring 扫描与表对应的实体类,以及service层的类,不能用来扫描Controller层的类,因为 ...
- SpringMVC 学习 八 SSM环境搭建(一) web.xml配置
第一步:导入jar包 注意包的兼容性,以后采用maven会好很多 第二步:配置web.xml 在web.xml中,主要的配置内容有以下几点 (1)spring容器配置文件的位置 <!-- spr ...
- vue开发项目详细教程(第一篇 搭建环境篇)
最近做vue做项目碰到了不少坑,看了三天文档便开始上手做项目了,不是我牛b,是因为项目紧,我没有时间去深入学习,所以只能一边学一边做了. 我要做的项目是一个官方网站(包括管理后台),也因为是我第一次使 ...
- 【原】Python学习_Django搭建环境及创建第一个项目
1.Window 平台安装 Python 下载安装包 https://www.python.org/downloads/windows/ 2.Pyhton环境变量配置 右键点击"计算机 ...
- C 语言学习 之搭建环境和熟悉命令
Open Terminal 打开终端To run a command as administrator (user "root"), use "sudo <comm ...
- Spring+SpringMVC+MyBatis深入学习及搭建(十六)——SpringMVC注解开发(高级篇)
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7085268.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十五)——S ...
- Zend Framework学习日记(1)--环境搭建篇(转)
Zend Framework学习日记(1)--环境搭建篇 (1)开发工具 Zend Framework框架:http://framework.zend.com/download/latest 包含2个 ...
随机推荐
- Codeforces Round #Pi (Div. 2) A. Lineland Mail 水题
A. Lineland MailTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/567/probl ...
- [Angular 2] Understanding @Injectable
In order to resolve a dependency, Angular’s DI uses type annotations. To make sure these types are p ...
- mybatis0210 mybatis和ehcache缓存框架整合
.1mybatis和ehcache缓存框架整合 一般不用mybatis来管理缓存而是用其他缓存框架在管理缓存,因为其他缓存框架管理缓存会更加高效,因为别人专业做缓存的而mybatis专业做sql语句的 ...
- mysql_upgrade命令
mysql 创建存储过程失败.查看错误日志,发现如下信息:*********************************************************************** ...
- Shell脚本调试工具set
可以使用set命令的x选项,显示所有命令执行及变量值的变化过程等. 具体使用方法:首先使用set -x开启调试模式,最后使用命令set +x关闭调试模式. 一个简单示例演示如何使用set命令进行脚本调 ...
- java_线程-锁
package com.demo.test3; import java.util.concurrent.CountDownLatch; /** * @author QQ: 1236897 * */ / ...
- PHP 正则表达式语法
则表达式简介 在某些应用中,往往有时候需要根据一定的规则来匹配(查找)确认一些字符串,如要求用户输入的 QQ 号码为数字且至少 5 位.用于描述这些规则的工具就是正则表达式. 最简单的匹配 最简单的匹 ...
- java 开源缓存框架--转载
原文地址:http://www.open-open.com/13.htm JBossCache/TreeCache JBossCache是一个复制的事务处理缓存,它允许你缓存企业级应用数据来更好的 ...
- Lifting the Stone
我们需要把一块石头平稳的从地板上拿起来.石头的底面是多边形且各个部分的高度都一样,我们需要找出石头的重心. input 测试案例 T; 每组第一行给出N,表示定点数. 接下来N行,每行连个数,表示坐 ...
- Android(java)学习笔记99:android的短信发送器研究
1.第一种可以调用系统内部的短信程序. 之前我曾经出现过一个bug就是报错: android.content.ActivityNotFoundException: No Activity found ...