1.@GetMapping/@PostMapping 是spring 4.3的新特性
2.在Springboot里面使用jsp, 需要另外加入org.apache.tomcat.embed.tomcat-embed-jasper 包
还在配置文件加入两个配置项
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
方法的返回值是JSP文件路径+文件名
不能使用@RestController
3.在Spring-boot 中使用freemarker 的步骤
1.在pom.xml 里面加入如下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2.默认的freemarker的模板文件在classpath:templates 默认扩展名.ftl
可以可以下面配置 自定义模板路径
spring.freemarker.template-loader-path=classpath:/templates2/
3.更换web容器
springboot web 默认使用的是tomcat,可以更换成jetty
步骤:
1.排除
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
2.加入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
3.springboot如何处理静态资源
1.src/main/webapp目录下,就可以直接访问
2.默认的静态资源路径是"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"
3.可以通过spring.resources.staticLocations=修改静态资源路径
4.如何在springboot 使用servlet的Api
1.编写servlet、filter、listener,然后加上相应的注解(@WebServlet("/user.do")、@WebFilter、@WebListener)
2.然后加上@ServletComponentScan注解(servlet3)
或者通过以下方法(servlet2.5)
@Bean
public ServletRegistrationBean createR(){
return new ServletRegistrationBean(new UserServlet(), "/test");
}
@Bean
public FilterRegistrationBean createF(@Qualifier("createR") ServletRegistrationBean servletRegistrationBean){
new FilterRegistrationBean(new LogFilter());
}
@Bean
public ServletListenerRegistrationBean creaL(){
return new ServletListenerRegistrationBean(new ContextLister());
}
@Configuration
public class WebConifuration extends WebMvcConfigurerAdapter{
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LogHandlerIncepter());
}
}
5.拦截器的使用步骤
1.写一个拦截器,实现HandlerInterceptor
2.写一个类,继承WebMvcConfigurerAdapter抽象类,重写addInterceptors方法,通过该方法将拦截器加进去。
public class LogHandlerIncepter implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
System.out.println("--------preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
System.out.println("--------postHandle");
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
System.out.println("--------afterCompletion");
}
}
HandlerInterceptor方法说明:
1.preHandle 进入controller之前
2.postHandle 页面渲染之前
3.afterCompletion 页面渲染之后,一般用于资源清除操作