[101]一步一步学懂spring - 使用Java配置的方式搭建SpringMvc
2017-07-27 09:06
465 查看
Servlet3.0的时代,可以不再使用web.xml来进行对web进行配置。可以通过注解和Java的方式进行配置。Spring3之后,也可以通过注解和Java配置的方式来取代xml进行配置。
下面使用Servlet3.0整合SpringMvc。
首先是取代web.xml的配置。我们可以自定义一个类来实现WebApplicationInitializer接口,重写onStartup方法。
然后进行SpringMvc的配置类,来取代原来的springmvc的xml配置
最后实现一个测试的Controller和一个jsp(这一步和原来的相同)
jsp
注意问题:
1.我们把jsp放到了src/main/resources/pages/目录下,这个是SpringBoot的习惯配置。运行时目录是在/WEB-INF/classes/pages下。
同样的,我们的静态资源也会习惯放到src/main/resources下,那么这些静态的资源我们该如何访问呢?springmvc的静态资源的地址映射可以给我们提供帮助。这个时候我们就需要对springmvc进行配置,原来的时候要去xml配置文件中配置,现在使用Java的方式该如何进行配置呢?这里springmvc给我们提供了一个默认的配置类 WebMvcConfigurerAdapter,在我们的配置类中继承这个类就有了所有的默认配置,重写配置的方法就可以单独对某些我们需要的进行配置。代码如下:
2.这里仅仅配置了一个hello,那么我们的数据源的配置,其他的原来在spring的xml配置该如何配置呢?
对于bean的配置,比如数据源等,可以参考[001]一步一步学懂spring - 初识Java配置
对于事务的配置,可以参考[002]一步一步学懂spring - AOP和自定义注解
3.在配置数据源的时候,需要一些常量,不希望写死到Java配置中。可以参考:[003]一步一步学懂spring - Spring EL
4.项目是使用maven来进行管理的,下面贴一下pom.xml的配置,当然你可以把这些jar统一放到WEB-INF/lib目录下。
最后打包放到tomcat下运行即可。
请求地址
http://localhost:8080/hy-02/
运行结果如下
下面使用Servlet3.0整合SpringMvc。
首先是取代web.xml的配置。我们可以自定义一个类来实现WebApplicationInitializer接口,重写onStartup方法。
package com.hy.test1; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; public class WebConfig implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.register(MvcConfig.class); // 注册配置类 context.setServletContext(servletContext); ServletRegistration.Dynamic servlet = servletContext.addServlet("dispathcher", new DispatcherServlet(context)); // 加载springmvc的核心servlet servlet.addMapping("/"); // 设置servlet的请求路径 servlet.setLoadOnStartup(1); // 立即加载,非第一次请求的时候再进行加载 } }
然后进行SpringMvc的配置类,来取代原来的springmvc的xml配置
package com.hy.test1; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; @Configuration @ComponentScan("com.hy.test1") @EnableWebMvc // 启用springmvc public class MvcConfig { // 配置视图解析器(这里使用的是jsp) @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/classes/pages/"); // 设置前缀 resolver.setSuffix(".jsp"); // 设置后缀 resolver.setViewClass(JstlView.class); return resolver; } }
最后实现一个测试的Controller和一个jsp(这一步和原来的相同)
package com.hy.test1; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class MyController { @RequestMapping("/") public String index() { return "index"; } }
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>hello</title> </head> <body> hello </body> </html>
注意问题:
1.我们把jsp放到了src/main/resources/pages/目录下,这个是SpringBoot的习惯配置。运行时目录是在/WEB-INF/classes/pages下。
同样的,我们的静态资源也会习惯放到src/main/resources下,那么这些静态的资源我们该如何访问呢?springmvc的静态资源的地址映射可以给我们提供帮助。这个时候我们就需要对springmvc进行配置,原来的时候要去xml配置文件中配置,现在使用Java的方式该如何进行配置呢?这里springmvc给我们提供了一个默认的配置类 WebMvcConfigurerAdapter,在我们的配置类中继承这个类就有了所有的默认配置,重写配置的方法就可以单独对某些我们需要的进行配置。代码如下:
package com.hy.test1; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; @Configuration @ComponentScan("com.hy.test1") @EnableWebMvc // 启用springmvc public class MvcConfig { // 配置视图解析器(这里使用的是jsp) @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/classes/pages/"); // 设置前缀 resolver.setSuffix(".jsp"); // 设置后缀 resolver.setViewClass(JstlView.class); return resolver; } /** * 静态资源映射 */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/"); // 把/assets下的静态资源映射的路径为/assets/ } }
2.这里仅仅配置了一个hello,那么我们的数据源的配置,其他的原来在spring的xml配置该如何配置呢?
对于bean的配置,比如数据源等,可以参考[001]一步一步学懂spring - 初识Java配置
对于事务的配置,可以参考[002]一步一步学懂spring - AOP和自定义注解
3.在配置数据源的时候,需要一些常量,不希望写死到Java配置中。可以参考:[003]一步一步学懂spring - Spring EL
4.项目是使用maven来进行管理的,下面贴一下pom.xml的配置,当然你可以把这些jar统一放到WEB-INF/lib目录下。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hy</groupId> <artifactId>hy-02</artifactId> <packaging>war</packaging> <version>1.0.0</version> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> <version>3.1.0</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <scope>provided</scope> <version>2.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>hy-02</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding><!-- 指定编码格式,否则在DOS下运行mvn compile命令时会出现莫名的错误,因为系统默认使用GBK编码 --> </configuration> </plugin> </plugins> </build> </project>
最后打包放到tomcat下运行即可。
请求地址
http://localhost:8080/hy-02/
运行结果如下
相关文章推荐
- 使用Java配置的方式完成SpringMVC的搭建
- 【spring in action 学习--springMVC搭建】在不使用web.xml的情况下,配置Srping MVC工程
- JAVA_WEB项目之使用Spring的xml配置方式在项目中管理Lucene检索框架
- Spring-hibernate使用全xml配置结合方式搭建开发
- Spring利用java配置方式进行快速搭建
- 【Spring in action】基于Java配置SpringMVC中使用Spring及SpringMVC各种请求详解
- SpringMVC+Spring4+hibernate5使用java类配置spring和springmvc环境(不用配置web.xml)
- Spring4.x的Java配置方式SpringMVC(Web)
- Java Web开发之Spring | SpringMvc | Mybatis | Hibernate整合、配置、使用
- java 基于springboot使用ssh(spring + springmvc + hibernate)分库配置多数据源方式
- 用java 配置方式 搭建springmvc + spring data jpg + mysql
- spring使用java配置定时任务的几种配置方式及示例
- javaWeb项目SpringMVC3.2.1与Mybatis3.0.4整合实例(Mybaits-spring配置文件的几种方式)之一SqlSessionDaoSupport方式
- 【SpringBoot】使用Java配置方式
- Spring Bean配置方式之一:Java配置
- 一步一步深入spring(6)--使用基于XML配置的spring实现的AOP
- java spring配置文件applicationcontext.xml的使用
- Spring 中 MongDB java Bean 的映射配置(注解方式)
- spring学习笔记8--使用spring进行面向切面的(AOP)编程(2)XML配置方式
- 【Java Web】: 使用Tomcat与MyEclipse搭建Web应用及其相关的配置