一、注解說明
在spring-boot+spring mvc 的項目中,有些時候我們需要自己配置一些項目的設置,就會涉及到這三個,那么,他們之間有什么關系呢?
首先,@EnableWebMvc=WebMvcConfigurationSupport,使用了@EnableWebMvc注解等於擴展了WebMvcConfigurationSupport但是沒有重寫任何方法。
所以有以下幾種使用方式:
-
@EnableWebMvc+extends WebMvcConfigurationAdapter,在擴展的類中重寫父類的方法即可,這種方式會屏蔽springboot的@EnableAutoConfiguration中的設置
-
extends WebMvcConfigurationSupport,在擴展的類中重寫父類的方法即可,這種方式會屏蔽springboot的@EnableAutoConfiguration中的設置
-
extends WebMvcConfigurationAdapter,在擴展的類中重寫父類的方法即可,這種方式依舊使用springboot的@EnableAutoConfiguration中的設置
具體哪種方法適合,看個人對於項目的需求和要把控的程度
在WebMvcConfigurationSupport(@EnableWebMvc)和@EnableAutoConfiguration這兩種方式都有一些默認的設定
而WebMvcConfigurationAdapter則是一個abstract class
具體如何類內如何進行個性化的設置,可以參考以下文章:
Spring Boot:定制HTTP消息轉換器
EnableWebMvc官方文檔
- 使用 @EnableWebMvc 注解,需要以編程的方式指定視圖文件相關配置
/** * Web MVC 配置適配器 * @ClassName: WebAppConfigurer * @Description: * @author OnlyMate * @Date 2018年8月28日 下午2:39:31 * * WebAppConfigurer extends WebMvcConfigurerAdapter 在Spring Boot2.0版本已過時了,用官網說的新的類替換 * */ @Configuration public class WebAppConfigurer implements WebMvcConfigurer { /** * springmvc視圖解析 * @Title: viewResolver * @Description: TODO * @Date 2018年8月28日 下午4:46:07 * @author OnlyMate * @return */ @Bean public InternalResourceViewResolver viewResolver(){ InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/views/"); viewResolver.setSuffix(".jsp"); // viewResolver.setViewClass(JstlView.class); // 這個屬性通常並不需要手動配置,高版本的Spring會自動檢測 return viewResolver; } /** * SpringBoot設置首頁 */ @Override public void addViewControllers(ViewControllerRegistry registry) { WebMvcConfigurer.super.addViewControllers(registry); registry.addViewController("/").setViewName("index"); registry.setOrder(Ordered.HIGHEST_PRECEDENCE); } }
- 使用 @EnableAutoConfiguration 注解,會讀取 application.properties 或 application.yml 文件中的配置
## 視圖
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
或
spring:
http:
encoding:
charset: UTF-8
enabled: true
force: true
messages:
encoding: UTF-8
profiles:
active: dev
mvc:
view:
prefix: /WEB-INF/views/
suffix: .jsp
二、模版引擎
Spring boot 在springmvc的視圖解析器方面就默認集成了ContentNegotiatingViewResolver和BeanNameViewResolver,在視圖引擎上就已經集成自動配置的模版引擎,如下:
1. FreeMarker
2. Groovy
3. Thymeleaf
4. Velocity (deprecated in 1.4)
6. Mustache
JSP技術spring boot 官方是不推薦的,原因有三:
1. 在tomcat上,jsp不能在嵌套的tomcat容器解析即不能在打包成可執行的jar的情況下解析
2. Jetty 嵌套的容器不支持jsp
3. Undertow
而其他的模版引擎spring boot 都支持,並默認會到classpath的templates里面查找模版引擎,這里假如我們使用freemarker模版引擎
1.現在pom.xml啟動freemarker模版引擎視圖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
2.定義一個模版后綴是ftp,注意是在classpath的templates目錄下
3.在controller上返回視圖路徑
@Controller public class HelloWorldController { private Logger logger = LoggerFactory.getLogger(HelloWorldController.class); @Value("${question}") private String question; @Value("${answer}") private String answer; @Value("${content}") private String content; @ResponseBody @RequestMapping("/hello") public String index() { return "hello world"; } @ResponseBody @RequestMapping(value="/hello1") public String index1() { return question + answer; } @ResponseBody @RequestMapping(value="/hello2") public String index2() { return content; } @RequestMapping(value="/hello3") public String index3(Model model) { try { model.addAttribute("question", question); model.addAttribute("answer", answer); } catch (Exception e) { logger.info("HelloWorldController ==> index3 method: error", e); } return "/hello"; } }
如果使用@RestController,默認就會在每個方法上加上@Responsebody,方法返回值會直接被httpmessageconverter轉化,如果想直接返回視圖,需要直接指定modelAndView。
public class HelloWorldController{ private Logger logger = LoggerFactory.getLogger(HelloWorldController.class); @Value("${question}") private String question; @Value("${answer}") private String answer; @Value("${content}") private String content; @RequestMapping("/hello3") public ModelAndView hello3() { try { model.addAttribute("question", question); model.addAttribute("answer", answer); } catch (Exception e) { logger.info("HelloWorldController ==> index3 method: error", e); } return new ModelAndView("hello"); } }
4.配置首頁
/** * Web MVC 配置適配器 * @ClassName: WebAppConfigurer * @Description: * @author OnlyMate * @Date 2018年8月28日 下午2:39:31 * * WebAppConfigurer extends WebMvcConfigurerAdapter 在Spring Boot2.0版本已過時了,用官網說的新的類替換 * */ @Configuration public class WebAppConfigurer implements WebMvcConfigurer {/** * SpringBoot設置首頁 */ @Override public void addViewControllers(ViewControllerRegistry registry) { WebMvcConfigurer.super.addViewControllers(registry); registry.addViewController("/").setViewName("index"); registry.setOrder(Ordered.HIGHEST_PRECEDENCE); } }
5.訪問 http://localhost:8088/springboot
雖然,jsp技術,spring boot 官方不推薦,但考慮到是常用的技術,這里也來集成下jsp技術
1.首先,需要在你項目上加上webapp標准的web項目文件夾
2.配置jsp的前綴和后綴
參考頭部 ‘注解說明’
3.在controller中返回視圖
同freemaker的第三步
4.配置首頁
同freemaker的第四步
5.訪問 http://localhost:8088/springboot
注意
如果出現freemarker模版引擎和jsp技術同時存在的話,springmvc會根據解析器的優先級來返回具體的視圖,默認,FreeMarkerViewResolver的優先級大於InternalResourceViewResolver的優先級,所以同時存在的話,會返回freemarker視圖