zoukankan      html  css  js  c++  java
  • SpringMVC Web项目升级为Springboot项目(二)

    一、访问原项目地址,报404错误

    由于原项目地址启动路径为http://localhost:8080/xxx

    Spring boot默认启动路径为http://localhost:8080/

    所以需要添加context-path配置,在application.properties中添加

    server.servlet.context-path=/xxx/

    即可

    二、访问登录地址,报500错误

    访问项目登录地址,报500错误,控制台中报错:

    org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.ThreadContext or as a vm static singleton.  This is an invalid application configuration.

    此错误原因是未加载shiro配置文件

    原项目shiro.xml配置文件在resource文件夹下,通过servlet.xml配置

    由于springboot启动未加载servlet.xml中配置

    所以只需要在Application类中添加

    @ImportResource(locations={"classpath:shiro.xml"})

    即可正确加载配置文件

    三、程序启动后,访问页面无法正确加载css、img、js等静态资源

    由于原先静态资源配置在spring-mvc.xml中

    升级为springboot后,可将静态资源直接放入/resource/static/文件夹中

    然后添加类MvcConfig,代码如下

    package com.gauge.childheart;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    @Configuration
    public class MvcConfig implements WebMvcConfigurer {
    
        /*资源处理器*/
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/img/**").addResourceLocations("/static/img/");
            registry.addResourceHandler("/assets/**").addResourceLocations("/static/assets/");
            registry.addResourceHandler("/css/**").addResourceLocations("/static/css/");
            registry.addResourceHandler("/js/**").addResourceLocations("/static/js/");
        }
    }

    即可正确加载静态资源文件

    PS:如果生成时资源文件没有正确复制到生成目录,在pom中添加

    <build>   
    <resource>
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>**/*.*</include>
                    </includes>
                </resource>
            </resources>
        </build>

    即可

  • 相关阅读:
    python小白-day9 数据库操作与Paramiko模块
    python小白-day8 线程、进程、协程
    python小白-day8 socketserver模块
    python小白-day7 socket初识
    python小白-day7 面向对象高级部分
    python小白-day6 xml处理模块
    python小白-day6 ConfigParser模块
    2020软件定义网络实验二
    软件工程实践第一次个人作业
    2020软件定义网络实验一
  • 原文地址:https://www.cnblogs.com/punkrocker/p/10691310.html
Copyright © 2011-2022 走看看