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>

    即可

  • 相关阅读:
    非洲出现新蓝海,他们抓住机遇将产品加工反向送入国内市场
    npm ERR! code ERR_STREAM_WRITE_AFTER_END
    程序默认不自动重连oracle
    一份超详细的MySQL高性能优化实战总结
    佩奇送福利:Eygle系列书籍免费下载
    解决waiting for target deviceto come online的做法
    Android真机运行闪退问题
    他是如何将小店铺打造成餐饮品牌的?
    用两张图告诉你,为什么你的App会卡顿?
    用两张图告诉你,为什么你的App会卡顿?
  • 原文地址:https://www.cnblogs.com/punkrocker/p/10691310.html
Copyright © 2011-2022 走看看