07_SpringBoot的web開發(fā)

Spring Boot的web開發(fā)

spring-boot-autoconfigure-1.5.2.RELEASE.jar中,
Web開發(fā)的自動配置類:org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration

自動配置的ViewResolver


視圖的配置mvcProperties對象中:
org.springframework.boot.autoconfigure.web.WebMvcProperties.View

  • application.prrperties中自定義配置prefix
"NoHandlerFoundException" should be thrown if no Handler was found to process a request.
spring.mvc.view.prefix= # Spring MVC view prefix.
spring.mvc.view.suffix= # Spring MVC view suffix.

自動配置靜態(tài)資源

  • 進入規(guī)則為 /
    如果進入SpringMVC的規(guī)則為/時,Spring Boot的默認靜態(tài)資源的路徑為:
    spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/


    靜態(tài)資源路徑

    瀏覽器訪問路徑

  • 進入規(guī)則為*.xxx 或者 不指定靜態(tài)文件路徑時
    將靜態(tài)資源放置到webapp下的static目錄中即可通過地址訪問:


自定義消息轉化器

自定義消息轉化器,只需要在@Configuration的類中添加消息轉化器的@bean加入到Spring容器,就會被Spring Boot自動加入到容器中。

    @Bean
    public StringHttpMessageConverter stringHttpMessageConverter(){
        StringHttpMessageConverter converter  = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        return converter;
    }

默認配置:



自定義SpringMVC的配置

有些時候我們需要自已配置SpringMVC而不是采用默認,比如說增加一個攔截器,這個時候就得通過繼承WebMvcConfigurerAdapter然后重寫父類中的方法進行擴展。

package cn.huachao.springboot.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class MySpringMVCConfig extends WebMvcConfigurerAdapter{
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        HandlerInterceptor handlerInterceptor = new 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 {
                
            }
            @Override
            public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
                    Exception ex)throws Exception {
                
            }
        };
        registry.addInterceptor(handlerInterceptor);
    }

  // 自定義消息轉化器的第二種方法
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        StringHttpMessageConverter converter  = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        converters.add(converter);
    }
    
}
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容