Servlet3.0无web.xml,全注解配置
项目结构
创建项目
pom
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
</dependencies>
Spring容器配置
在config包下定义ApplicationConfig.java
,它对应web.xml中ContextLoaderListener
的配置
@Configuration
@ComponentScan(basePackages = "cn.zysheep",
excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = Controller.class)})
public class ApplicationConfig {
//在此配置除了Controller的其它bean,比如:数据库链接池、事务管理器、业务bean等。
}
servletContext配置
config包下定义WebConfig.java
,它对应spring mvc 核心类DispatcherServlet配 置。
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "cn.zysheep"
,includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = Controller.class)})
//springmvc需要扫描controller接口
public class WebConfig implements WebMvcConfigurer {
//视图解析器
@Bean
public InternalResourceViewResolver viewResolver(){
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
// 添加一个视图控制
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
}
}
加载 Spring容器
在init包下定义Spring容器初始化类SpringApplicationInitializer
,此类实现WebApplicationInitializer
接口, Spring容器启动时加载WebApplicationInitializer接口的所有实现类。
public class SpringApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{ApplicationConfig.class}; //指定rootContext的配置类
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{WebConfig.class}; //指定servletContext的配置类
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
SpringApplicationInitializer
相当于web.xml,使用了servlet3.0开发则不需要再定义web.xml, ApplicationConfig.class
对应以下配置的application-context.xml,WebConfig.class
对应以下配置的spring- mvc.xml,web.xml的内容参考
<web‐app>
<listener>
<listener‐class>org.springframework.web.context.ContextLoaderListener</listener‐class>
</listener>
<context‐param>
<param‐name>contextConfigLocation</param‐name>
<param‐value>/WEB‐INF/application‐context.xml</param‐value>
</context‐param>
<servlet>
<servlet‐name>springmvc</servlet‐name>
<servlet‐class>org.springframework.web.servlet.DispatcherServlet</servlet‐class>
<init‐param>
<param‐name>contextConfigLocation</param‐name>
<param‐value>/WEB‐INF/spring‐mvc.xml</param‐value>
</init‐param>
<load‐on‐startup>1</load‐on‐startup>
</servlet>
<servlet‐mapping>
<servlet‐name>springmvc</servlet‐name>
<url‐pattern>/</url‐pattern>
</servlet‐mapping>
</web‐app>
测试
login.jsp
webapp/WEB-INF/views
目录创建login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>用户登录</title></head>
<body>
<form action="/login" method="post">
用户名:<input type="text" name="username"><br>
密 码: <input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
controller
在cn/zysheep/controller目录下创建LoginController
@RestController
public class LoginController {
@PostMapping("/login")
public String login(@RequestParam("username") String username,
@RequestParam("password") String password){
return username+"=="+password;
}
}
在配置类WebConfig中添加一个视图控制器,即重写addViewControllers方法
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
}
配置tomcat