springboot+jsp jar包 上传云服务器 【访问失败】解决办法

1、springboot整合jsp

最近做的一个项目,由于种种原因使用了springboot+jsp。相信许多人也清楚,springboot官方推荐的模板引擎是thymleaf,并不太支持jsp。

springboot要整合jsp的话,需要添加以下依赖:

另外,如果需要用到jstl的话,则需要添加以下依赖:

而且jsp文件必须放在WEB-INF目录下,否则无法编译成class文件(毕竟jsp本质是servlet)所以要在application.yml添加如下配置

jsp中如果需要引用静态资源的话,则可以放在resource/static文件夹下,在jsp中引用的路径直接写“/xxx/xxx.css” ,因为springboot默认的静态资源路径中有resource/static,放在这里不会被拦截


2、使用maven打jar包

我在使用maven将springboot打成jar包时,发现在WEB-INF下的jsp文件不会被放进jar包中。当然,这是正常的,因为是打jar包,不是打war包,当然不会包含WEB-INF目录的东西。

所以需要添加配置,在pom.xml文件中的build标签下添加如下内容,在打包过程中需要将webapp目录(WEB-INF在webapp下)放入jar包中的META-INF/resource目录下。

另外,还需要将springboot的maven插件版本修改为1.4.2.RELEASE,具体如下图,只有使用这个版本打jar包才能解析jsp,这是springboot的bug吧

然后使用maven工具进行打包即可

如果在打包过程中出现以下错误信息:

[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.4.2.RELEASE:repackage (default) on project spring-boot: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.4.2.RELEASE:repackage failed: Unable to find a single main class from the following candidates [com.charmbirds.Application, com.charmbirds.util.redis.JedisUtils] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException

可能存在的原因:

有两个main方法导致的问题

解决办法:

删掉测试main方法

猜你喜欢

转载自blog.csdn.net/qq_36023564/article/details/85018963