【转】Spring MVC 3.x 基本配置
WEB-INF/web.xml
例1

<?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>SpringSvcDemo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

例2

<?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>SpringSvcDemo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 自定义位置 -->
<param-value>/WEB-INF/SpringMVC.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>

说明:
<load-on-startup>1</load-on-startup>:是启动顺序,让这个Servlet随Servletp容器一起启动。
<servlet-name>SpringMVC</servlet-name>:servlet的名称。 DispatcherServlet的初始化后,它会在 Web应用程序的WEB - INF文件夹中查找一个文件名[servlet-name]-servlet.xml的配置,这里将会查找SpringMVC-servlet.xml,假如不想采用默认的方式查找,当然也可以自定义位置(见web.xml例2)
<url-pattern>*.do</url-pattern>:映射URL模式*. do的DispatcherServlet,Servlet拦截匹配规则可以自已定义,拦截哪种URL合适?当映射为@RequestMapping("/user/add")时,为例:
- 拦截*.do、*.htm, 例如:/user/add.do,这是最传统的方式,最简单也最实用。不会导致静态文件(jpg,js,css)被拦截。
- 拦截/,例如:/user/add可以实现现在很流行的REST风格。很多互联网类型的应用很喜欢这种风格的URL。弊端:会导致静态文件(jpg,js,css)被拦截后不能正常显示。想实现REST风格,事情就是麻烦一些。后面有解决办法还算简单(见下面).
- 拦截/*,这是一个错误的方式,请求可以走到Action中,但转到jsp时再次被拦截,不能访问到jsp。
WEB-INF/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:tx="http://www.springframework.org/schema/tx"
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-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 自动扫描的包名 -->
<context:component-scan base-package="com.test" />
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />
<!-- 视图解释类 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

在上面的XML配置文件,我们已经定义了一个标记<context:component-scan>。 这将允许Spring从com.test包中载入所有组件和它的所有子包。 这将载入我们Controller类。 另外,我们定义一个bean viewResolver视图解释,并添加前缀字符串/WEB-INF/view/以及后缀.JSP。 所以一个为hello 的@RequestMapping("/hello") 请求映射对应的视图为/WEB-INF/jsp/hello.jsp
访问静态文件(jpg,js,css)的方法:
<url-pattern>/</url-pattern>的拦截方法虽然可以实现REST URL,会导致静态文件也被拦截,可以通过配置来设置访问.

<?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:tx="http://www.springframework.org/schema/tx"
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-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 自动扫描的包名 -->
<context:component-scan base-package="com.test" />
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven /> <mvc:resources location="/WEB-INF/view/image/" mapping="/image/**"/>
<mvc:resources location="/WEB-INF/view/js/" mapping="/js/**"/>
<mvc:resources location="/WEB-INF/view/css/" mapping="/css/**"/> <!-- 视图解释类 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

/WEB-INF/view/image/dogs.jpg在视图hello.jsp中就可以正常访问了

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring 3.x MVC Series: Hello World</title>
</head>
<body>
<h1>${message}</h1>
<img alt="dogs" src="data:image/dogs.jpg">
</body>
</html>

转载原文:http://www.cnblogs.com/xiepeixing/p/4235750.html
【转】Spring MVC 3.x 基本配置的更多相关文章
- Spring MVC 使用tomcat中配置的数据源
Spring MVC 使用tomcat中配置的数据源 配置tomcat数据源 打开tomcat目录下的conf目录,编辑sever.xml目录.在<GlobalNamingResources&g ...
- Spring MVC的web.xml配置详解(转)
出处http://blog.csdn.net/u010796790 1.spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name) 2.在w ...
- Spring MVC 学习总结(八)——Spring MVC概要与环境配置(IDEA+Maven+Tomcat7+JDK8、示例与视频)
一.MVC概要 MVC是模型(Model).视图(View).控制器(Controller)的简写,是一种软件设计规范,用一种将业务逻辑.数据.显示分离的方法组织代码,MVC主要作用是降低了视图与业务 ...
- Spring MVC + Velocity实现国际化配置
国际化介绍 web开发中,国际化是需要考虑的一个问题,而且这个问题一般是越早敲定越好(不然等到系统大了,翻译是个问题).下面是结合实际项目(Spring MVC+Velocity)对实现国际化的一些总 ...
- Spring mvc的web.xml配置详解
1.spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name) 2.在web.xml配置监听器ContextLoaderListener(l ...
- Spring MVC 的xml一些配置
1.可以自动加载注解驱动,通过注解找到对应Controller <!-- spring MVC 注解驱动 --> <mvc:annotation-driven></mvc ...
- spring mvc:练习:javaConfig配置和注解
Spring4 MVC HelloWorld 注释/JavaConfig为示例,一步一步以简单的方式学习Spring4 MVC 的注解,项目设置,代码,部署和运行. 我们已经使用XML配置开发了一个H ...
- spring mvc框架web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...
- spring, spring mvc, mybatis整合文件配置详解
转自:http://www.cnblogs.com/wxisme/p/4924561.html 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用 ...
- 1、Spring MVC的web.xml配置详解(转)
版权声明:本文为博主原创文章,转载请注明出处http://blog.csdn.net/u010796790 1.spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilt ...
随机推荐
- EF 表联合查询 join
有两张表m_Dept.m_User,联合查询 linq方式.EF方式 private void Add() { List<m_Dept> lst = new List<m_Dept& ...
- 点滴积累【JS】---JS小功能(操作Table--动态添加删除表格及数据)
效果: 代码: <head runat="server"> <title></title> <style type="text/ ...
- Java遍历包中所有类
PackageUtil 类 import java.io.File; import java.net.URL; import java.net.URLClassLoader; import java. ...
- tensorflow 之模型的保存与加载(一)
怎样让通过训练的神经网络模型得以复用? 本文先介绍简单的模型保存与加载的方法,后续文章再慢慢深入解读. #!/usr/bin/env python3 #-*- coding:utf-8 -*- ### ...
- 蓝牙(CoreBluetooth)-外部设备(服务端)
蓝牙(CoreBluetooth)-外部设备(服务端) 主要内容 1. 创建外部管理器对象 2. 设置本地外设的服务和特征 3. 添加服务和特征到到你的设置的数据库中 4. 向外公布你的的服务 5. ...
- Quotations中页面弹出的问题
- 使用WinSCP这个软件使linux和win7互传文件
使用这个软件之前首先win7要可以ping通linux系统,且linux要开启,关机可不能通啊!!!!!!!!! 双击这个快捷方式 主机名写ip地址 我们可以将虚拟机上的文件下载下来进行使用 也可以将 ...
- JSON简述
JSON(JavaScript Object Notation) JavaScript 对象表示法,是一种轻量级的数据交换格式.类似于XML. 基础结构 JSON基于两种结构(即由两种结构组成:对象( ...
- Risk UVA - 12264 拆点法+最大流+二分 最少流量的节点流量尽量多。
/** 题目:Risk UVA - 12264 链接:https://vjudge.net/problem/UVA-12264 题意:给n个点的无权无向图(n<=100),每个点有一个非负数ai ...
- import _mysql----ImportError: DLL load failed: %1 不是有效的 Win32 应用程序。
背景:安装了mysql,练习sql 操作,提示 ImportError DLL load failed: %1 不是有效的 Win32 应用程序 解决方法: 操作系统win10,64位,查看安装的my ...