SpringBoot的web開發(fā)
一露久、自動配置的ViewResolver
視圖的配置mvcProperties對象中:
org.springframework.boot.autoconfigure.web.WebMvcProperties.View
二、自動配置靜態(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/
測試:
訪問:127.0.0.1:8088/timg.jpg
2.2 進入規(guī)則為.xxx 或者 不指定靜態(tài)文件路徑時*
將靜態(tài)資源放置到webapp下的static目錄中即可通過地址訪問:
測試:
三、自定義消息轉(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)配置:
四惶凝、自定義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)建了劫拢。