使用web.xml和springmvc-servlet.xml配置文件进行Web配置和Spring MVC配置。但Spring Boot推荐使用Java配置的方式进行项目配置
创建Spring MVC的Java配置
在该配置类中使用@Configuration注解声明该类为Java配置类;使用@EnableWebMvc注解开启默认配置,如ViewResolver;使用@ComponentScan注解扫描注解的类;使用@Bean注解配置视图解析器;该类需要实现WebMvcConfigurer接口来配置Spring MVC。
创建Web的Java配置
创建Web的Java类WebConfig。该类需要实现WebApplicationInitializer接口替代web.xml文件的配置。实现该接口将会自动启动Servlet容器。在WebConfig类中需要使用AnnotationConfigWebApplicationContext注册Spring MVC的Java配置类SpringMVCConfig,并和当前ServletContext关联。最后,在该类中需要注册Spring MVC的DispatcherServlet。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
没注册的用户,请<a href="index/register">注册</a>!<br>
已注册的用户,去<a href="index/login">登录</a>!
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
登录页面!
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
注册页面!
</body>
</html>
package config;
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.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan("controller")
public class SpringMVCConfig implements WebMvcConfigurer {
/**
* 配置视图解析器
*/
@Bean
public InternalResourceViewResolver getViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
/**
* 配置静态资源
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/html/**").addResourceLocations("/html/");
}
}
package config;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
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 arg0) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringMVCConfig.class);// 注册Spring MVC的Java配置类SpringMVCConfig
ctx.setServletContext(arg0);// 和当前ServletContext关联
/**
* 注册Spring MVC的DispatcherServlet
*/
Dynamic servlet = arg0.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* “@Controller”表示IndexController的实例是一个控制器
*
* @Controller相当于@Controller("indexController") 或@Controller(value =
* "indexController")
*/
@Controller
@RequestMapping("/index")
public class IndexController {
@RequestMapping("/login")
public String login() {
/**
* login代表逻辑视图名称,需要根据Spring MVC配置 文件中internalResourceViewResolver的前缀和后缀找到对应的物理视图
*/
return "login";
}
@RequestMapping("/register")
public String register() {
return "register";
}
}