在spring-boot+spring mvc 的項(xiàng)目中,有些時(shí)候我們需要自己配置一些項(xiàng)目的設(shè)置,就會(huì)涉及到這三個(gè),那么,他們之間有什么關(guān)系呢?
首先,@EnableWebMvc=WebMvcConfigurationSupport,使用了@EnableWebMvc注解等于擴(kuò)展了WebMvcConfigurationSupport但是沒(méi)有重寫(xiě)任何方法。
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專(zhuān)注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、微信小程序開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了石首免費(fèi)建站歡迎大家使用!
所以有以下幾種使用方式:
具體哪種方法適合,看個(gè)人對(duì)于項(xiàng)目的需求和要把控的程度
在WebMvcConfigurationSupport(@EnableWebMvc)和@EnableAutoConfiguration這兩種方式都有一些默認(rèn)的設(shè)定
而WebMvcConfigurationAdapter則是一個(gè)abstract class
具體如何類(lèi)內(nèi)如何進(jìn)行個(gè)性化的設(shè)置,可以參考以下文章:
Spring Boot:定制HTTP消息轉(zhuǎn)換器
EnableWebMvc官方文檔
/**
* Web MVC 配置適配器
* @ClassName: WebAppConfigurer
* @Description:
* @author OnlyMate
* @Date 2018年8月28日 下午2:39:31
*
* WebAppConfigurer extends WebMvcConfigurerAdapter 在Spring Boot2.0版本已過(guò)時(shí)了,用官網(wǎng)說(shuō)的新的類(lèi)替換
*
*/
@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); // 這個(gè)屬性通常并不需要手動(dòng)配置,高版本的Spring會(huì)自動(dòng)檢測(cè)
return viewResolver;
}
/**
* SpringBoot設(shè)置首頁(yè)
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
WebMvcConfigurer.super.addViewControllers(registry);
registry.addViewController("/").setViewName("index");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
}
視圖
## 視圖
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的視圖解析器方面就默認(rèn)集成了ContentNegotiatingViewResolver和BeanNameViewResolver,在視圖引擎上就已經(jīng)集成自動(dòng)配置的模版引擎,如下:
JSP技術(shù)spring boot 官方是不推薦的,原因有三:
而其他的模版引擎spring boot 都支持,并默認(rèn)會(huì)到classpath的templates里面查找模版引擎,這里假如我們使用freemarker模版引擎
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
定義一個(gè)模版后綴是ftp,注意是在classpath的templates目錄下
@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,默認(rèn)就會(huì)在每個(gè)方法上加上@Responsebody,方法返回值會(huì)直接被httpmessageconverter轉(zhuǎn)化,如果想直接返回視圖,需要直接指定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");
}
}
/**
* Web MVC 配置適配器
* @ClassName: WebAppConfigurer
* @Description:
* @author OnlyMate
* @Date 2018年8月28日 下午2:39:31
*
* WebAppConfigurer extends WebMvcConfigurerAdapter 在Spring Boot2.0版本已過(guò)時(shí)了,用官網(wǎng)說(shuō)的新的類(lèi)替換
*
*/
@Configuration
public class WebAppConfigurer implements WebMvcConfigurer {/**
* SpringBoot設(shè)置首頁(yè)
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
WebMvcConfigurer.super.addViewControllers(registry);
registry.addViewController("/").setViewName("index");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
}
在controller中返回視圖
同freemaker的第三步
配置首頁(yè)
同freemaker的第四步
如果出現(xiàn)freemarker模版引擎和jsp技術(shù)同時(shí)存在的話,springmvc會(huì)根據(jù)解析器的優(yōu)先級(jí)來(lái)返回具體的視圖,默認(rèn),F(xiàn)reeMarkerViewResolver的優(yōu)先級(jí)大于InternalResourceViewResolver的優(yōu)先級(jí),所以同時(shí)存在的話,會(huì)返回freemarker視圖
網(wǎng)頁(yè)題目:SpringBoot實(shí)踐——SpringMVC視圖解析
文章URL:http://sd-ha.com/article22/jjchjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、網(wǎng)站收錄、Google、網(wǎng)站內(nèi)鏈、網(wǎng)站設(shè)計(jì)、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)