一、

二、用Java文件配置web application

1.

 package spittr.config;

 import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

 import spittr.web.WebConfig;

 public class SpitterWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

   @Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { RootConfig.class };
} @Override
protected Class<?>[] getServletConfigClasses() {
//Specify configuration class
return new Class<?>[] { WebConfig.class };
} @Override
protected String[] getServletMappings() {
//Map DispatcherServlet to "/"
return new String[] { "/" };
} }

(1)值得注意的是SpitterWebInitializer继承了AbstractAnnotationConfigDispatcherServletInitializer,所以在Servlet3.1以上的容器中,会自动将此类作为DispatcherServlet和Spring Application Context的配置文件

从Servlect3.0开始,窗器会在classpath中寻找实现javax.servlet.ServletContainerInitializer的类作为配置文件,而Spring提供了实现SpringServletContainerInitializer、WebApplicationInitializer、AbstractAnnotationConfigDispatcherServletInitializer

(2)在Spring web application通常有两种context:Dispactcherservlet、ContextLoaderListener

Dispactcherservlet用来装载springmvc相关组件,如controller、view resolvers、handler mappings,ContextLoaderListener是装载应用的其他组件,通常是中间层和后台打交道的组件,如service、dao。 上述配置文件通过getServletConfigClasses()以返回的java配置文件来定义Dispatcherservlet,用getRootConfigClasses()以返回的配置文件定义ContextLoaderListener,其实就是相当于以前的web.xml配置

2.

package spittr.web;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration
@EnableWebMvc
@ComponentScan("spittr.web")
public class WebConfig extends WebMvcConfigurerAdapter { @Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
} @Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
//Configure static content handling
configurer.enable();
//configurer.enable();是you’re asking DispatcherServlet to forward
//requests for static resources to the servlet container’s default servlet and not to try to
//handle them itself.
} @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// TODO Auto-generated method stub
super.addResourceHandlers(registry);
} }

在xml中是用 <mvc:annotation-driven>

3.

package spittr.config;

import java.util.regex.Pattern;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;
import org.springframework.core.type.filter.RegexPatternTypeFilter; import spittr.config.RootConfig.WebPackage; @Configuration
@Import(DataConfig.class)
@ComponentScan(basePackages={"spittr"},
excludeFilters={
@Filter(type=FilterType.CUSTOM, value=WebPackage.class)
})
public class RootConfig {
public static class WebPackage extends RegexPatternTypeFilter {
public WebPackage() {
super(Pattern.compile("spittr\\.web"));
}
}
}

4.

 package spittr.config;

 import javax.sql.DataSource;

 import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; @Configuration
public class DataConfig { @Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("schema.sql")
.build();
} @Bean
public JdbcOperations jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
} }

5.

SPRING IN ACTION 第4版笔记-第五章Building Spring web applications-001-SpringMVC介绍的更多相关文章

  1. SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-004-以query parameters的形式给action传参数(@RequestParam、defaultValue)

    一. 1.Spring MVC provides several ways that a client can pass data into a controller’s handler method ...

  2. SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-007-表单验证@Valid、Error

    一. Starting with Spring 3.0, Spring supports the Java Validation API in Spring MVC . No extra config ...

  3. SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-005-以path parameters的形式给action传参数(value=“{}”、@PathVariable)

    一 1.以path parameters的形式给action传参数 @Test public void testSpittle() throws Exception { Spittle expecte ...

  4. SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-002-Controller的requestMapping、model

    一.RequestMapping 1.可以写在方法上或类上,且值可以是数组 package spittr.web; import static org.springframework.web.bind ...

  5. SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-006-处理表单数据(注册、显示用户资料)

    一.显示注册表单 1.访问资源 @Test public void shouldShowRegistration() throws Exception { SpitterRepository mock ...

  6. SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-003-示例项目用到的类及配置文件

    一.配置文件 1.由于它继承AbstractAnnotationConfigDispatcherServletInitializer,Servlet容器会把它当做配置文件 package spittr ...

  7. SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-005- 异常处理@ResponseStatus、@ExceptionHandler、@ControllerAdvice

    No matter what happens, good or bad, the outcome of a servlet request is a servlet response. If an e ...

  8. SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-003- 上传文件multipart,配置StandardServletMultipartResolver、CommonsMultipartResolver

    一.什么是multipart The Spittr application calls for file uploads in two places. When a new user register ...

  9. SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-002- 在xml中引用Java配置文件,声明DispatcherServlet、ContextLoaderListener

    一.所有声明都用xml 1. <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...

随机推荐

  1. VBA实现随意输入组合码,查询唯一标识码

    记录背景: 需要在excel中查询出组合码,对应的唯一标识码. 举例 组合码:4+5+6+9+1*2   标识码:A1 界面随意输入组合码:1*2+4+5+6+9  输出标识码:A1 VBA实现: P ...

  2. Aix命令大全

    AIX服务器系统命令简介 在AIX操作系统上有很多的命令.这里介绍一些系统级的命令,它将有助于回答一些常见问题.大家以此做参考,并补充修改. 以下命令在AIX 5.1上测试通过. 正文 以下命令在AI ...

  3. Oracle查询慢, 特别是更新慢问题

    近期, 客户发现查询比较慢, 特别是更新更慢. 原来是oracle listerner log太大导致.  (C:\app\Administrator\diag\tnslsnr\ServerName\ ...

  4. ios开发之UIView的frame、bounds跟center属性的区别(附图)

    博文暂时想到什么写什么,不顺理成章,不顺章成篇. 先看几个概念 坐标点Poit:向右侧为X轴正方向的值x,原点下侧为Y轴正方向的值y 大小Size:由宽度width和高度height构成,表示一个矩形 ...

  5. 12天学好C语言——记录我的C语言学习之路(Day 8)

    12天学好C语言--记录我的C语言学习之路 Day 8: 从今天开始,我们获得了C语言中很有力的一个工具,那就是函数.函数的魅力不仅于此,一个程序到最后都是由众多函数组成的,我们一定要用好函数,用熟练 ...

  6. ubuntu 恢复gnome-panel

    Ubuntu重启panel 的方法首先进入终端, 依次输入以下命令1.gconftool --recursive-unset /apps/panel2.rm -rf ~/.gconf/apps/pan ...

  7. 16_会话技术_Session案例

    [购物车中的信息保存] [Book.java] package com.Higgin.shopping; public class Book { private String id; private ...

  8. Custom Action : dynamic link library

    工具:VS2010, Installshield 2008 实现功能: 创建一个C++ win32 DLL的工程,MSI 工程需要调用这个DLL,并将Basic MSI工程中的两个参数,传递给DLL, ...

  9. QtSQL学习笔记(2)- 连接到数据库

    要使用QSqlQuery或者QSqlQueryModel访问一个数据库,首先需要创建并打开一个或多个数据库连接(database connections). 一般地,数据库连接是根据连接名(conne ...

  10. SQL技巧之分组求和

    这是CSDN问答里面有人提出的一道问题,题目如下. 表格如下: 得出结果如下: 求精简的SQL语句. SQL查询语句: with a as( select rank() over (partition ...