因为暂时没跑通,但是又想回家捣鼓,所以在简书上写下来。
源码来自spring实战第5章。不使用web.xml配置,而是使用java bean配置。
目录结构如下:
- src
- spittr
- Spittle.java
- spittr.config
- SpittrWebAppInitializer.java
- WebConfig.java
- RootConfig.java
- spittr.data
- SpittleRepository.java
- spittr.web
- HomeController.java
- SpittleController.java
- SpittleControllerTest.java
- spittr
下面贴源码:
SpittrWebAppInitializer.java
package spittr.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
/**
* 扩展了AbstractAnnotationConfigDispatcherServletInitializer的类会自动配置DispatcherServlet和Spring应用上下文
* @author c00284934
*
*/
public class SpittrWebAppInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
/**
* 返回带有@Configuration注解的类将会用来配置ContextLoaderListener创建的应用上下文中的bean
* ContextLoaderListener加载的bean通常是驱动应用后端的中间层和数据层组件
*/
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{RootConfig.class};
}
/**
* 带有@Configuration注解的类会用来定义为DispatcherServlet应用上下文中的bean
*/
@Override
protected Class<?>[] getServletConfigClasses() {
/**
* 当DispatcherServlet启动的时候,它会创建Spring应用上下文,并加载配置文件中所声明的bean
*/
return new Class<?>[]{WebConfig.class};//指定配置类
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};//将DispatcherServlet映射到“/”
}
}
WebConfig.java
package spittr.config;
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.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc //启用SpringMVC
@ComponentScan("spittr.web") //启用组件扫描
public class WebConfig extends WebMvcConfigurerAdapter{
public ViewResolver viewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver(); //配置jsp视图解析器
resolver.setPrefix("/WEB-INF/view/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) { //配置静态资源的处理
//我们要求DispatcherServlet将对静态资源的请求转发到Servlet容器中默认的Servlet上,而不是使用DispatcherServlet本身来处理此类请求
configurer.enable();
}
}
RootConfig.java
package spittr.config;
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.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan(basePackages={"spittr"},excludeFilters={@Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)})
public class RootConfig {
}
HomeController.java
package spittr.web;
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="/",method=RequestMethod.GET)
public String home(){
return "home";
}
}
请求http://localhost:8080/spittr/ 会执行HomeController 的home(),然后返回home.jsp(因为在WebConfig .java的视图解析器里已经配置好前后缀了)
下面是home.jsp: (使用了jstl标签库,需要导入jstl.jar standard.jar)
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" session="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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=ISO-8859-1">
<title>Spitter</title>
</head>
<body>
<h1>Welcome</h1>
<a href="<c:url value="/spittr" /> " >Spittles</a> |
<a href="<c:url value="/spittr/register" /> " >Spittles</a>
</body>
</html>
Spittle.java