企業(yè)管理網(wǎng)站開發(fā)論文可以營銷的十大產(chǎn)品
Springboot在web層的開發(fā)基本都是采用Springmvc框架技術(shù),但是Springmvc中的某些配置在boot是沒有的,我們就應(yīng)該根據(jù)自己的需求進(jìn)行對(duì)mvc擴(kuò)展配置
Springboot1.x版本如何配置
通過注解@Configuration一個(gè)類,繼承webmvcconfigureradapter,然后根據(jù)需求實(shí)現(xiàn)里面的方法。
Springboot2.x版本如何配置
通過實(shí)現(xiàn)webmvcconfigure接口的方式
上面boot對(duì)mvc的擴(kuò)展配置既保留了mvc的默認(rèn)配置,也可以使用我們擴(kuò)展的配置。如何全面接管mvc的配置,所以的webmvc都由我們自己配置?只需要加上注解EnableWebMvc
擴(kuò)展配置使用舉例,如配置攔截器
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;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;/*** 自定義一個(gè)登陸攔截器*/
@Configuration //聲明這是一個(gè)配置
public class LoginInterceptor extends WebMvcConfigurerAdapter {/*用來添加攔截器的方法InterceptorRegistry registry攔截器注冊(cè)*/@Overridepublic void addInterceptors(InterceptorRegistry registry) {//使用匿名內(nèi)部類創(chuàng)建要給攔截器HandlerInterceptor loginInterceptor = new HandlerInterceptor() {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {//判斷session中是否存在用戶if (request.getSession().getAttribute("user") == null) {response.sendRedirect("/admin");return false;}return true;}@Overridepublic void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {}@Overridepublic void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {}};registry.addInterceptor(loginInterceptor).addPathPatterns("/admin/**").excludePathPatterns("/admin").excludePathPatterns("/admin/login");}
}
tomcat配置
Springboot默認(rèn)使用的就是嵌入式servlet容器即tomcat,對(duì)于web項(xiàng)目,如果使用的是外部tomcat,相關(guān)配置比如訪問端口、資源路徑等可以在tomcat的conf文件下配置。但是在boot中,tomcat配置又兩種方式
第一種:通過配置文件直接配置(推薦)
?
#如果是tomcat相關(guān)的設(shè)置用server.tomcat.xx
server.tomcat.uri-encoding=UTF-8
#如果是servlet相關(guān)的配置用server.xx
server.port=80
?第二種:通過配置類的方式