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>

    即可

  • 相关阅读:
    MyEclipse使用总结——MyEclipse10安装SVN插件
    SVN的安装和配置
    Git
    myeclipse 怎么安装与激活
    jQuery
    sql server 2008
    原问题:身份证为什么只有15位
    QTP10破解方法及mgn-mqt82.exe下载
    WARNING [Project: :app] To shrink resources you must also enable ProGuard
    your local changes would be overwritten by merge. commit stash or revert them to proceed. view them
  • 原文地址:https://www.cnblogs.com/punkrocker/p/10691310.html
Copyright © 2011-2022 走看看