• 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>

    即可

  • 相关阅读:
    linux   截取时间段的日志
    Linux 通过 shell 脚本修改密码
    Centos7下安装netstat
    正文字体大小:大 中 小 解决configure: error: Cannot find libmysqlclient under /usr
    隐藏index.php
    编译安装php Cannot find MySQL header files under /usr/include/mysql.
    解决问题 “You don't have permission to access /index.html on this server.”
    Linux Samba服务器配置
    PHP curl传输文件的版本兼容性
    消息队列feed程序实现中的问题
  • 原文地址:https://www.cnblogs.com/punkrocker/p/10691310.html
Copyright © 2020-2023  润新知