• Springboot对MVC、tomcat扩展配置


    Springboot在web层的开发基本都是采用Springmvc框架技术,但是Springmvc中的某些配置在boot是没有的,我们就应该根据自己的需求进行对mvc扩展配置

    Springboot1.x版本如何配置


    通过注解@Configuration一个类,继承webmvcconfigureradapter,然后根据需求实现里面的方法。

    Springboot2.x版本如何配置

    通过实现webmvcconfigure接口的方式

    上面boot对mvc的扩展配置既保留了mvc的默认配置,也可以使用我们扩展的配置。如何全面接管mvc的配置,所以的webmvc都由我们自己配置?只需要加上注解EnableWebMvc

    扩展配置使用举例,如配置拦截器

    1. import org.springframework.context.annotation.Configuration;
    2. import org.springframework.web.servlet.HandlerInterceptor;
    3. import org.springframework.web.servlet.ModelAndView;
    4. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    5. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    6. import javax.servlet.http.HttpServletRequest;
    7. import javax.servlet.http.HttpServletResponse;
    8. /**
    9. * 自定义一个登陆拦截器
    10. */
    11. @Configuration //声明这是一个配置
    12. public class LoginInterceptor extends WebMvcConfigurerAdapter {
    13. /*
    14. 用来添加拦截器的方法
    15. InterceptorRegistry registry拦截器注册
    16. */
    17. @Override
    18. public void addInterceptors(InterceptorRegistry registry) {
    19. //使用匿名内部类创建要给拦截器
    20. HandlerInterceptor loginInterceptor = new HandlerInterceptor() {
    21. @Override
    22. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
    23. //判断session中是否存在用户
    24. if (request.getSession().getAttribute("user") == null) {
    25. response.sendRedirect("/admin");
    26. return false;
    27. }
    28. return true;
    29. }
    30. @Override
    31. public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    32. }
    33. @Override
    34. public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    35. }
    36. };
    37. registry.addInterceptor(loginInterceptor).addPathPatterns("/admin/**").excludePathPatterns("/admin").excludePathPatterns("/admin/login");
    38. }
    39. }

    tomcat配置


    Springboot默认使用的就是嵌入式servlet容器即tomcat,对于web项目,如果使用的是外部tomcat,相关配置比如访问端口、资源路径等可以在tomcat的conf文件下配置。但是在boot中,tomcat配置又两种方式

    第一种:通过配置文件直接配置(推荐)
     

    1. #如果是tomcat相关的设置用server.tomcat.xx
    2. server.tomcat.uri-encoding=UTF-8
    3. #如果是servlet相关的配置用server.xx
    4. server.port=80

     第二种:通过配置类的方式

  • 相关阅读:
    java枚举中写抽象方法
    MacOS原版镜像iso下载
    Java进阶导图xmind版本
    抓住那头牛——BFS
    快速学会JDBC及获取连接的五种方式
    使用python进行数据分析(二)
    发挥项目“临时性”威力,让项目顺利实施
    绕过WAF(Web应用程序防火墙)--介绍、主要功能、部署模式、分类及注入绕过方式等
    ArcGIS: 第二届全国大学生GIS技能大赛(广西师范学院)详解-下午题
    【BigDecima】不可变的,任意精度的有符号十进制数。
  • 原文地址:https://blog.csdn.net/qq_34491508/article/details/133414036