美文网首页
SpringBoot Jsp 404

SpringBoot Jsp 404

作者: 风吟空城 | 来源:发表于2018-10-31 13:50 被阅读0次

    场景

    仿照SpringBoot官方spring-boot-sample-web-jsp,在IDEA中建立了同样的项目。直接在IDEA中启动项目,出现以下现象:

    1. 数据接口可以正常访问;
    2. 视图不能被解析,即访问页面时,会出现404客户端异常;

    如图所示:


    数据 视图

    相关配置

    application.yml

    mvc:
        view:
          prefix: /WEB-INF/pages/
          suffix: .jsp
    

    HomeController

    @Controller
    public class HomeController {
    
        /**
         * 首页
         */
        @GetMapping("/")
        public String index(){
            return "hello";
        }
    }
    

    解决办法

    1. 使用mvn spring-boot:run命令行启动
    2. 在pom文件中增加内置tomcat:
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    

    原因

    With Jetty and Tomcat, it should work if you use war packaging. An executable war will work when launched with java -jar, and will also be deployable to any standard container. JSPs are not supported when using an executable jar.

    大致意思: 如果将你的程序打包成war包,在jetty、Tomcat和使用java -jar 命令时,JSP会起作用。但是如果是作为一个可执行的jar包的话,JSP不受支持。

    Do not use the src/main/webapp directory if your application is packaged as a jar. Although this directory is a common standard, it works only with war packaging, and it is silently ignored by most build tools if you generate a jar.

    大致意思:如果您的应用程序打包为jar,那么不要使用src/main/webapp目录。虽然这个目录是一个通用标准,但它只与war打包一起工作,并且如果您生成一个jar,它会被大多数构建工具默默忽略。代码如下:

    protected boolean isInvalidPath(String path) {
            if (logger.isTraceEnabled()) {
                logger.trace("Applying \"invalid path\" checks to path: " + path);
            }
    
            if (!path.contains("WEB-INF") && !path.contains("META-INF")) {
                if (path.contains(":/")) {
                    String relativePath = path.charAt(0) == '/' ? path.substring(1) : path;
                    if (ResourceUtils.isUrl(relativePath) || relativePath.startsWith("url:")) {
                        if (logger.isTraceEnabled()) {
                            logger.trace("Path represents URL or has \"url:\" prefix.");
                        }
    
                        return true;
                    }
                }
    
                if (path.contains("..")) {
                    path = StringUtils.cleanPath(path);
                    if (path.contains("../")) {
                        if (logger.isTraceEnabled()) {
                            logger.trace("Path contains \"../\" after call to StringUtils#cleanPath.");
                        }
    
                        return true;
                    }
                }
    
                return false;
            } else {
                if (logger.isTraceEnabled()) {
                    logger.trace("Path contains \"WEB-INF\" or \"META-INF\".");
                }
    
                return true;
            }
        }
    

    代码中显示WEB-INF和META-INF为非法路径

    另外,官方也说了尽量避免使用JSP。

    If possible, JSPs should be avoided. There are several known limitations when using them with embedded servlet containers.

    相关文章

      网友评论

          本文标题:SpringBoot Jsp 404

          本文链接:https://www.haomeiwen.com/subject/kphatqtx.html