上一节完成后,运行起来后,应该能看到index.jsp的HelloWorld内容了,在这一节加入SpringMVC支持。
1、首先新建一个config文件,用来配置SpringMVC的拦截器。
src/main/java-org.aurora.config-ApplicationContextServletConfigruation.java
package org.aurora.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
public class ApplicationContextSevlteConfigruation {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
2、因为用到了spring-webmvc,spring-webmvc又依赖了cglib,所以在pom.xml中加入这两个依赖。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
</dependency>
3、将拦截器加入到web容器中,方法和web.xml文件的配置一样,只是不用xml文件格式了。修改src/main/java-org.aurora.config-ApplicationInitialize.java,增加一个dispatcher的监听器。这个监听器拦截所有“/”下的资源。
package org.aurora.config;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class ApplicationInitialize implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
servletContext.addListener(new ContextLoaderListener(rootContext));
/**
* Spring mvc dispatcher
*/
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
4、下面的问题是到哪里去找SpringMVC的拦截器呢,xml时代是有个默认的地方,默认的名称,比如上面的描述,就应该是到WEB-INF下面去找到一个叫dispatcher-servlet.xml的文件,web容器会自动加载进来,当然,也可以自定义路径,我们这里也自定义了,这个类名叫ApplicationContextServletConfigruation,所以,这个容器中还需要加入一行代码,表示到哪里去寻找这个文件。
package org.aurora.config;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class ApplicationInitialize implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.scan("org.aurora.config");
servletContext.addListener(new ContextLoaderListener(rootContext));
/**
* Spring mvc dispatcher
*/
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
就是这句rootContext.scan("org.aurora.config");就好比去xml中的dispatcher-*.xml一样,这里使用包扫描方式将“org.aurora.config”下面所有有@Configuration注解的类全部扫描一遍。
5、修改一下index.jsp,这个文件以后不用了,让他自动跳转到home.jsp去。
<jsp:forward page="home" />
6、我们看到,这个页面跳转到了一个叫home的地址,这是个虚拟地址,SpringMVC会将这个地址处理成真实的页面,所以,这里还需要建立一个核心类,这和SpringMVC以前的版本没有什么区别。
src/main/java-org.aurora.controller-HomeController.java
package org.aurora.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HomeController {
@RequestMapping(value = "/home", method = RequestMethod.GET)
public String login() {
return "home";
}
}
7、现在构建一个home.jsp页面,因为我们拦截的是"/WEB-INF/views/"下的所有".jsp"文件,所以需要在WEB-INF目录下建立一个views目录存放jsp文件。src/main/webapp/WEB-INF/views/home.jsp
<html>
<head>
<title>Home Page</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
8、测试一下,还是访问http://localhost:8080/index.jsp,发现404错误,找不到home,这个问题是因为src/main/java/org/aurora/config/ApplicationContextSevlteConfigruation.java的配置里只是设置了拦截器,具体是拦截什么没有说明,所以这里需要添加一个注解
package org.aurora.config;
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.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@ComponentScan(basePackages = {"org.aurora.controller"})
public class ApplicationContextSevlteConfigruation {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
就是这一句@ComponentScan(basePackages = {"org.aurora.controller"}),指定到org.aurora.controller包里去扫描所有有@controller注解的类
9、再测试一下,应该正常了。
10、为了日后方便,再加入一个注解@EnableWebMvc,这个注解一次性为我们导入了springmvc需要的很多bean。最终代码如下:
package org.aurora.config;
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.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"org.aurora.controller"})
public class ApplicationContextSevlteConfigruation {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
分享到:
相关推荐
赠送原API文档:spring-cloud-gateway-server-3.1.1-javadoc.jar; 赠送源代码:spring-cloud-gateway-server-3.1.1-sources.jar; 赠送Maven依赖信息文件:spring-cloud-gateway-server-3.1.1.pom; 包含翻译后的...
赠送原API文档:spring-cloud-gateway-server-3.1.1-javadoc.jar; 赠送源代码:spring-cloud-gateway-server-3.1.1-sources.jar; 赠送Maven依赖信息文件:spring-cloud-gateway-server-3.1.1.pom; 包含翻译后的...
赠送原API文档:spring-cloud-commons-3.1.1-javadoc.jar; 赠送源代码:spring-cloud-commons-3.1.1-sources.jar; 赠送Maven依赖信息文件:spring-cloud-commons-3.1.1.pom; 包含翻译后的API文档:spring-cloud-...
赠送原API文档:spring-cloud-openfeign-core-3.1.1-javadoc.jar; 赠送源代码:spring-cloud-openfeign-core-3.1.1-sources.jar; 赠送Maven依赖信息文件:spring-cloud-openfeign-core-3.1.1.pom; 包含翻译后的...
赠送原API文档:spring-cloud-openfeign-core-3.1.1-javadoc.jar; 赠送源代码:spring-cloud-openfeign-core-3.1.1-sources.jar; 赠送Maven依赖信息文件:spring-cloud-openfeign-core-3.1.1.pom; 包含翻译后的...
赠送原API文档:spring-cloud-context-3.1.1-javadoc.jar; 赠送源代码:spring-cloud-context-3.1.1-sources.jar; 赠送Maven依赖信息文件:spring-cloud-context-3.1.1.pom; 包含翻译后的API文档:spring-cloud-...
赠送原API文档:spring-cloud-commons-3.1.1-javadoc.jar; 赠送源代码:spring-cloud-commons-3.1.1-sources.jar; 赠送Maven依赖信息文件:spring-cloud-commons-3.1.1.pom; 包含翻译后的API文档:spring-cloud-...
hadoop-annotations-3.1.1.jar hadoop-common-3.1.1.jar hadoop-mapreduce-client-core-3.1.1.jar hadoop-yarn-api-3.1.1.jar hadoop-auth-3.1.1.jar hadoop-hdfs-3.1.1.jar hadoop-mapreduce-client-hs-3.1.1.jar ...
赠送原API文档:spring-cloud-context-3.1.1-javadoc.jar; 赠送源代码:spring-cloud-context-3.1.1-sources.jar; 赠送Maven依赖信息文件:spring-cloud-context-3.1.1.pom; 包含翻译后的API文档:spring-cloud-...
标题 "spring3.1.1-mybatis3.1.1-struts2.3.4.1所有整合jar包" 暗示了这是一个用于构建基于Java的Web应用程序的集成框架,其中包含了Spring 3.1.1、MyBatis 3.1.1和Struts2 2.3.4.1的核心库。这些技术都是企业级Java...
赠送原API文档:spring-cloud-loadbalancer-3.1.1-javadoc.jar; 赠送源代码:spring-cloud-loadbalancer-3.1.1-sources.jar; 赠送Maven依赖信息文件:spring-cloud-loadbalancer-3.1.1.pom; 包含翻译后的API文档...
赠送原API文档:spring-cloud-loadbalancer-3.1.1-javadoc.jar; 赠送源代码:spring-cloud-loadbalancer-3.1.1-sources.jar; 赠送Maven依赖信息文件:spring-cloud-loadbalancer-3.1.1.pom; 包含翻译后的API文档...
资源分类:Python库 所属语言:Python 资源全名:futures-3.1.1-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
VeloView-3.1.1-26022015-Windows-64bit.exe VeloView-3.1.1-26022015-Windows-64bit.exe VeloView-3.1.1-26022015-Windows-64bit.exe
openbabel-3.1.1-cp36-cp36m-win_amd64
赠送原API文档:commons-math3-3.1.1-javadoc.jar; 赠送源代码:commons-math3-3.1.1-sources.jar; 赠送Maven依赖信息文件:commons-math3-3.1.1.pom; 包含翻译后的API文档:commons-math3-3.1.1-javadoc-API文档...
赠送原API文档:common-image-3.1.1-javadoc.jar; 赠送源代码:common-image-3.1.1-sources.jar; 赠送Maven依赖信息文件:common-image-3.1.1.pom; 包含翻译后的API文档:common-image-3.1.1-javadoc-API文档-...
matplotlib-3.1.1-cp37-cp37m-win_amd64.whl
org.springframework.aop-3.1.1.RELEASE org.springframework.asm-3.1.1.RELEASE org.springframework.aspects-3.1.1.RELEASE org.springframework.beans-3.1.1.RELEASE org.springframework.context.support-3.1.1....
配置springmvc的spring-webmvc的jar包,可用于ssm框架的使用