久久久精品一区ed2k-女人被男人叉到高潮的视频-中文字幕乱码一区久久麻豆樱花-俄罗斯熟妇真实视频

SpringBoot實(shí)踐——SpringMVC視圖解析

一、注解說(shuō)明

在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)建站歡迎大家使用!

所以有以下幾種使用方式:

  1. @EnableWebMvc+extends WebMvcConfigurationAdapter,在擴(kuò)展的類(lèi)中重寫(xiě)父類(lèi)的方法即可,這種方式會(huì)屏蔽springboot的@EnableAutoConfiguration中的設(shè)置
  2. extends WebMvcConfigurationSupport,在擴(kuò)展的類(lèi)中重寫(xiě)父類(lèi)的方法即可,這種方式會(huì)屏蔽springboot的@EnableAutoConfiguration中的設(shè)置
  3. extends WebMvcConfigurationAdapter,在擴(kuò)展的類(lèi)中重寫(xiě)父類(lèi)的方法即可,這種方式依舊使用springboot的@EnableAutoConfiguration中的設(shè)置

具體哪種方法適合,看個(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官方文檔

  • 使用 @EnableWebMvc 注解,需要以編程的方式指定視圖文件相關(guān)配置
/**
 * 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);
    }

}
  • 使用 @EnableAutoConfiguration 注解,會(huì)讀取 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的視圖解析器方面就默認(rèn)集成了ContentNegotiatingViewResolver和BeanNameViewResolver,在視圖引擎上就已經(jīng)集成自動(dòng)配置的模版引擎,如下:

  1. FreeMarker
  2. Groovy
  3. Thymeleaf
  4. Velocity (deprecated in 1.4)
  5. Mustache

JSP技術(shù)spring boot 官方是不推薦的,原因有三:

  1. 在tomcat上,jsp不能在嵌套的tomcat容器解析即不能在打包成可執(zhí)行的jar的情況下解析
  2. Jetty 嵌套的容器不支持jsp
  3. Undertow

而其他的模版引擎spring boot 都支持,并默認(rèn)會(huì)到classpath的templates里面查找模版引擎,這里假如我們使用freemarker模版引擎

  1. 現(xiàn)在pom.xml啟動(dòng)freemarker模版引擎視圖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
  1. 定義一個(gè)模版后綴是ftp,注意是在classpath的templates目錄下
    Spring Boot實(shí)踐——SpringMVC視圖解析

  2. 在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,默認(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");
    }
}
  1. 配置首頁(yè)
/**
 * 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);
    }

}
  1. 訪問(wèn) http://localhost:8088/springboot
    Spring Boot實(shí)踐——SpringMVC視圖解析
    Spring Boot實(shí)踐——SpringMVC視圖解析

雖然,jsp技術(shù),spring boot 官方不推薦,但考慮到是常用的技術(shù),這里也來(lái)集成下jsp技術(shù)

  1. 首先,需要在你項(xiàng)目上加上webapp標(biāo)準(zhǔn)的web項(xiàng)目文件夾
    Spring Boot實(shí)踐——SpringMVC視圖解析
  2. 配置jsp的前綴和后綴
    參考頭部 ‘注解說(shuō)明’
  3. 在controller中返回視圖
    同freemaker的第三步

  4. 配置首頁(yè)
    同freemaker的第四步

  5. 訪問(wèn) http://localhost:8088/springboot
    Spring Boot實(shí)踐——SpringMVC視圖解析
    Spring Boot實(shí)踐——SpringMVC視圖解析

注意

如果出現(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視圖
Spring Boot實(shí)踐——SpringMVC視圖解析

網(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)

外貿(mào)網(wǎng)站建設(shè)