SpringBoot的web開發(fā)

SpringBoot的web開發(fā)

一露久、自動配置的ViewResolver

1.png

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

2.png

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

2.1 進入規(guī)則為/

如果進入SpringMVC的規(guī)則為/時棺棵,Spring Boot的默認(rèn)靜態(tài)資源的路徑為:

spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

測試:

3.png
4.png

訪問:127.0.0.1:8088/timg.jpg

2.2 進入規(guī)則為.xxx 或者 不指定靜態(tài)文件路徑時*

將靜態(tài)資源放置到webapp下的static目錄中即可通過地址訪問:

5.png

測試:

6.png

三、自定義消息轉(zhuǎn)化器

原來的(springmvc.xml中):

<!-- 注解驅(qū)動 -->
<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <constructor-arg index="0" value="UTF-8" />
        </bean>
        <bean class="com.hcx.common.spring.extend.converter.json.CallbackMappingJackson2HttpMessageConverter">
            <property name="callbackName" value="callback" />
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

自定義消息轉(zhuǎn)化器纹份,只需要在@Configuration的類中添加消息轉(zhuǎn)化器的@bean加入到Spring容器运挫,就會被Spring Boot自動加入到容器中。

@Controller
@SpringBootApplication(exclude={RedisAutoConfiguration.class})  //排除redis的配置
@Configuration //聲明該類為一個配置類
public class HelloApplication { 
//springboot的項目命名中亲配,一般都有一個xxxApplication類尘应,該類作為springboot項目的入口類

    @RequestMapping("hello")
    @ResponseBody
    public String hello(){
        return "hello world";
    }
    
    @Bean
    public StringHttpMessageConverter stringHttpMessageConverter(){
        StringHttpMessageConverter converter  = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        return converter;
    }
    
    public static void main(String[] args) {
        
        SpringApplication app = new SpringApplication(HelloApplication.class);
        app.setBannerMode(Banner.Mode.OFF);//關(guān)閉banner
        app.run(args);
    }

}

默認(rèn)配置:

7.png
8.png

四惶凝、自定義SpringMVC的配置

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

HelloApplication和MySpringMVCConfig在同一包下,所以在MySpringMVCConfig加入了@Configuration注解會被掃描進去玷犹,運行HelloApplication混滔,會走該自定義攔截器。

MySpringMVCConfig:

package com.hcx.springboot.demo;

import java.nio.charset.Charset;
import java.util.List;

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

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
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).addPathPatterns("/**");
    }

    // 自定義消息轉(zhuǎn)化器的第二種方法
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        StringHttpMessageConverter converter  = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        converters.add(converter);
    }

}

HelloApplication:

package com.hcx.springboot.demo;

import java.nio.charset.Charset;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@SpringBootApplication(exclude={RedisAutoConfiguration.class})  //排除redis的配置
@Configuration //聲明該類為一個配置類
public class HelloApplication { 
    //springboot的項目命名中歹颓,一般都有一個xxxApplication類坯屿,該類作為springboot項目的入口類
    
    @RequestMapping("hello")
    @ResponseBody
    public String hello(){
        return "hello world";
    }
    
    @Bean
    public StringHttpMessageConverter stringHttpMessageConverter(){
        StringHttpMessageConverter converter  = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        return converter;
    }
    
    public static void main(String[] args) {
        
        SpringApplication app = new SpringApplication(HelloApplication.class);
        app.setBannerMode(Banner.Mode.OFF);//關(guān)閉banner
        app.run(args);
    }

}

注意:此時,有兩個消息轉(zhuǎn)換器巍扛;如果把兩個消息轉(zhuǎn)換器都去掉领跛,spring中默認(rèn)還有一個。把其中一個打開撤奸,則有一個吠昭,內(nèi)部的是否創(chuàng)建取決于我們自己有沒有創(chuàng)建,我們沒有創(chuàng)建則內(nèi)部的就會創(chuàng)建胧瓜,如果我們有創(chuàng)建怎诫,則內(nèi)部的不會創(chuàng)建。內(nèi)部的是判斷贷痪,容器中是否有幻妓,沒有則創(chuàng)建,有就不創(chuàng)建了劫拢。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末肉津,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子舱沧,更是在濱河造成了極大的恐慌妹沙,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件熟吏,死亡現(xiàn)場離奇詭異距糖,居然都是意外死亡,警方通過查閱死者的電腦和手機牵寺,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進店門悍引,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人帽氓,你說我怎么就攤上這事趣斤。” “怎么了黎休?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵浓领,是天一觀的道長玉凯。 經(jīng)常有香客問我,道長联贩,這世上最難降的妖魔是什么漫仆? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮泪幌,結(jié)果婚禮上歹啼,老公的妹妹穿的比我還像新娘。我一直安慰自己座菠,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布藤树。 她就那樣靜靜地躺著浴滴,像睡著了一般。 火紅的嫁衣襯著肌膚如雪然爆。 梳的紋絲不亂的頭發(fā)上掠械,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天拟糕,我揣著相機與錄音,去河邊找鬼品嚣。 笑死,一個胖子當(dāng)著我的面吹牛钧大,可吹牛的內(nèi)容都是我干的翰撑。 我是一名探鬼主播,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼啊央,長吁一口氣:“原來是場噩夢啊……” “哼眶诈!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起瓜饥,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤逝撬,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后乓土,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體宪潮,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年趣苏,在試婚紗的時候發(fā)現(xiàn)自己被綠了狡相。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,902評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡食磕,死狀恐怖谣光,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情芬为,我是刑警寧澤萄金,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布蟀悦,位于F島的核電站,受9級特大地震影響氧敢,放射性物質(zhì)發(fā)生泄漏日戈。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一孙乖、第九天 我趴在偏房一處隱蔽的房頂上張望浙炼。 院中可真熱鬧,春花似錦唯袄、人聲如沸弯屈。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽资厉。三九已至,卻和暖如春蔬顾,著一層夾襖步出監(jiān)牢的瞬間宴偿,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工诀豁, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留窄刘,地道東北人。 一個月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓舷胜,卻偏偏與公主長得像娩践,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子烹骨,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,843評論 2 354

推薦閱讀更多精彩內(nèi)容