spring mvc

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>

web部署描述符web.xml是java web 应用必不可少的配置文件。

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

为了让spring加载 *-servlet.xml之外的配置文件,需要定servlet监听器contextLoaderListener。默认情况下加载配置文件/WEB-INF/applicationcontext.xml,但是也可以在上下文参数中指定资金的文件。可以指定多个用逗号或者空格分隔的配置文件。

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml
            /WEB-INF/spring-security-servlet.xml</param-value>
  </context-param>
 <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

ContextLoaderListener 加载指定的Bean配置文件到跟应用上下文中,而每个DispatcherServlet将其配置文件加载到自己的上下文,并引用根应用上下文为其上级。每个DispatcherServlet实例加载的上下文可以访问甚至覆盖跟应用上下文中声明的Bean。子上下文可以访问父上下文中的内容,但父上下文不能访问子上下文中的内容。

激活Spring MVC 注解扫描

<context:component-scan base-package="com.broadwave" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
 </bean>
 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
 </bean>

<mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案。<mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。

<mvc:default-servlet-handler/>

会把"/**" url,注册到SimpleUrlHandlerMapping的urlMap中,把对静态资源的访问由HandlerMapping转到org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler处理并返回.DefaultServletHttpRequestHandler使用就是各个Servlet容器自己的默认Servlet.

补充说明:多个HandlerMapping的执行顺序问题:

DefaultAnnotationHandlerMapping的order属性值是:0

<mvc:resources/ >自动注册的 SimpleUrlHandlerMapping的order属性值是: 2147483646

<mvc:default-servlet-handler/>自动注册 的SimpleUrlHandlerMapping 的order属性值是: 2147483647

spring会先执行order值比较小的。当访问一个a.jpg图片文件时,先通过 DefaultAnnotationHandlerMapping 来找处理器,一定是找不到的,因为我们没有叫a.jpg的Action。然后再按order值升序找,由于最后一个 SimpleUrlHandlerMapping 是匹配 "/**"的,所以一定会匹配上,就可以响应图片

@RequestMapping("list")
	public void memberList() {
	}
//返回值为void的方法默认会返回与方法签名同名的视图(也就是<span style="font-size: 1em; line-height: 1.5;">memberList</span><span style="font-size: 1em; line-height: 1.5;">)</span>

自定义一个拦截器,要实现HandlerInterceptor接口

@Component
public class Intercept	implements HandlerInterceptor {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub

	}
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		// TODO Auto-generated method stub

	}

时间: 2024-08-24 17:57:51

spring mvc的相关文章

Spring MVC @RequestMapping Annotation Example with Controller, Methods, Headers, Params, @RequestPar

Spring MVC @RequestMapping Annotation Example with Controller, Methods, Headers, Params, @RequestParam, @PathVariable Pankaj July 4, 2014 Spring @RequestMapping is one of the most widely used Spring MVC annotation.org.springframework.web.bind.annotat

Spring 3 MVC: Themes In Spring-Tutorial With Example---reference

Welcome to Part 6 of Spring 3.0 MVC Series. In previous article we saw how to add Internationalization i18n and Localization L10n support to Spring 3.0 based web application. We usedLocaleChangeInterceptor to intercept the change in locale andReloada

初试 spring web mvc

作为一名code需要了解更多的知识,编程方面的东西太多了,是个逐渐积累的过程.最近学习一下spring web mvc,写下我个人的一些经验. 1.准备jar包.spring mvc 已经到了版本4,网上的很多资料已经不在适用.给出的下载地址也无法适用.这是非常痛苦的.我费了好大功夫才获得这些JAR包. 在官方介绍文档里获得http://docs.spring.io/spring/docs/4.1.0.BUILD-SNAPSHOT/spring-framework-reference/htmls

Spring 4 MVC HelloWorld - XML示例

构建Spring MVC 4的HelloWorld实例. 使用以下技术: Spring 4.0.6.RELEASE Maven 3 JDK 1.8 如果您已经熟悉maven使用eclipse 创建项目,请随意跳过第1步. 让我们开始. 步骤1:在Eclipse中创建一个新的Maven webapp项目 Goto File-> New - > Maven Project, 取消选中"使用默认工作区位置",以防您希望在其他位置创建项目 点击下一步 从选项列表中选择maven w

Spring 4 MVC HelloWorld - 注解/JavaConfig实例

在之前的Spring 4 MVC HelloWorld - 实例中,有spring-mvc.xml和web.xml这两个xml文件.现在通过java配置,将删除这个两个xml文件. 1.pom.xml文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="

spring MVC &lt;mvc:annotation-driven&gt;

研究SpringMvc 3.2的<mvc:annotation-driven>默认干了什么 如果不配置其他参数,大致相当于以下的配置文件(参考自org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser) <!-- 配置路径扩展名映射的媒体类型 --> <bean name="pathExtensionContentNegotiationStrategy" clas

Spring 4 MVC+Apache Tiles 3 Example

In this post we will integrate Apache Tiles 3 with Spring MVC 4, using annotation-based configuration. Apache Tiles is a template based, composite view framework: it allows to reuse page pieces across the application, keeping consistent look and feel

Spring Boot——2分钟构建spring web mvc REST风格HelloWorld

之前有一篇<5分钟构建spring web mvc REST风格HelloWorld>介绍了普通方式开发spring web mvc web service.接下来看看使用spring boot如何快速构建一个. Spring Boot使我们更容易去创建基于Spring的独立和产品级的可以”即时运行“的应用和服务.支持约定大于配置,目的是尽可能快地构建和运行Spring应用. 之前我们创建基于Spring的项目需要考虑添加哪些Spring依赖和第三方的依赖.使用Spring Boot后,我们可

(Spring文档翻译)Part V, the Web 17.1 Spring Web MVC framework介绍

指南文档的这个部分涵盖了Spring框架对表现层(特别是基于Web的表现层)以及WebSocket消息风格的web应用的支持. Spring框架拥有自己的web框架,Spring Web MVC,包含在前面几个章节.之后的几章是关于Spring框架对其他web技术的集成支持,像JSF等. 再之后是Spring框架的MVC porlet 框架. Spring 的MVC框架围绕着DispatcherServlet设计,DispatcherServlet将请求转发给handler,用可配置的handl

spring 3 mvc hello world + mavern +jetty

Spring 3 MVC hello world example By mkyong | August 2, 2011 | Updated : June 15, 2015 In this tutorial, we show you a Spring 3 MVC hello world example, using Maven build tool. Technologies used : Spring 3.2.13.RELEASE Maven 3 JDK 1.6 Eclipse 4.4 Boos