springmvc注解开发
ps : 这东西比较简单,用来为我们使用springboot打基础
一 : 工程搭建
(一) 导包
<!-- servlet --><dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
<!-- logback -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>1.2.3</version>
</dependency>
(二) 日志配置
logback.xml
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<resetJUL>true</resetJUL>
</contextListener>
<jmxConfigurator/>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>logback: %d{HH:mm:ss.SSS} %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- 将springweb下面的包设置成debug,报错信息会比较全面 -->
<logger name="org.springframework.web" level="debug"/>
<root level="info">
<appender-ref ref="console" />
</root>
</configuration>
(三) web配置
WebApplicationInitializer
是Spring
提供用来配置Servlet 3.0+
配置的接口,从而实现了替代web.xml
的位置。实现此接口将会自动被SpringServletContainerInitializer
(用来启动Servlet3.0容器)获取到。新建
WebApplicationContext
,注册配置类,并将其和当前servletContext
关联。注册
Spring MVC
的DispatcherServlet
。public class WebInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext context =
new AnnotationConfigWebApplicationContext();
context.register(SpringMvcConfig.class);
context.setServletContext(servletContext);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(context));
dispatcher.addMapping("/");
dispatcher.setLoadOnStartup(1);
}
}
(四) springmvc配置
@EnableWebMvc
会开启一些默认配置,比如ViewResolver
和MessageConverter
注意 : 这里的视图解析器(
springmvc
的核心渲染机制)无论使用哪个实现类,其方法名(也就是id
)要写其接口的名字@Configuration
@EnableWebMvc
@ComponentScan("club.info.springmvc")
public class SpringMvcConfig {
/*
视图解析器
*/
@Bean
public InternalResourceViewResolver viewResolver(){
return new InternalResourceViewResolver(){{
setPrefix("/");
setSuffix(".jsp");
setViewClass(JstlView.class);
}};
}
}
(五) 页面
index.jsp
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
二 : 基本配置
springmvc
中的一些定制配置需要我们继承一个类(WebMvcConfigurerAdapter)
- 并且在配置类上标注
@EnableWebMvc
(一) 静态资源映射
@Configuration@EnableWebMvc
@ComponentScan("club.info.springmvc")
public class SpringMvcConfig implements WebMvcConfigurer {
/*
静态资源映射
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/img/**").addResourceLocations("classpath:/img/");
}
}
(二) 拦截器
自定义拦截器
public class HelloInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
System.out.println("前置...");
return true;
}
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("后置...");
}
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response,
Object handler,
Exception ex) throws Exception {
System.out.println("完成...");
}
}
配置拦截器
@Configuration
@EnableWebMvc
@ComponentScan("club.info.springmvc")
public class SpringMvcConfig implements WebMvcConfigurer {
/*
拦截器
*/
@Bean
public HelloInterceptor helloInterceptor(){
return new HelloInterceptor();
}
/*
配置拦截器
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(helloInterceptor()).addPathPatterns("/hello");
}
}
(未完待续)
以上是 springmvc注解开发 的全部内容, 来源链接: utcz.com/z/510990.html