当前位置: 首页 > news >正文

Springboot对MVC、tomcat扩展配置

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

Springboot1.x版本如何配置


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

Springboot2.x版本如何配置

通过实现webmvcconfigure接口的方式

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

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

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;/*** 自定义一个登陆拦截器*/
@Configuration //声明这是一个配置
public class LoginInterceptor extends WebMvcConfigurerAdapter {/*用来添加拦截器的方法InterceptorRegistry registry拦截器注册*/@Overridepublic void addInterceptors(InterceptorRegistry registry) {//使用匿名内部类创建要给拦截器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默认使用的就是嵌入式servlet容器即tomcat,对于web项目,如果使用的是外部tomcat,相关配置比如访问端口、资源路径等可以在tomcat的conf文件下配置。但是在boot中,tomcat配置又两种方式

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

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

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

http://www.lryc.cn/news/178532.html

相关文章:

  • 网络子网划分练习
  • Leetcode刷题笔记--Hot51-60
  • 广告牌安全监测系统,用科技护航大型广告牌安全
  • volatile
  • JAVA:实现Excel和PDF上下标
  • AI写稿软件,最新的AI写稿软件有哪些
  • 干货:数据仓库基础知识(全)
  • 二分搜索简介
  • 虚拟车衣VR云展厅平台扩大了展览的触达范围
  • 云部署家里的服务器
  • 【利用冒泡排序的思想模拟实现qsort函数】
  • [plugin:vite:css] [sass] Undefined mixin.
  • 【论文阅读】大语言模型中的文化道德规范知识
  • 51单片机实训项目之产品数量计数器
  • Scala第七章节
  • C语言进程的相关操作
  • 数据结构学习系列之链式栈
  • too many session files in /var/tmp
  • 【7.0】打开未知来源安装应用
  • 安装ipfs-swarm-key-gen
  • BASH shell脚本篇5——文件处理
  • ElementUI之首页导航及左侧菜单(模拟实现)
  • Java开源工具库使用之Lombok
  • uboot启动流程涉及reset函数
  • 端口被占用怎么解决
  • python reportlab 生成多页pdf
  • word 多级目录的问题
  • python使用mitmproxy和mitmdump抓包之拦截和修改包(四)
  • 邓俊辉《数据结构》→ “2.6.5 二分查找(版本A)”之“成功查找长度”递推式推导
  • Linux文件查找,别名,用户组综合练习