带有 JSP 的 Spring Boot 应用程序不能作为独立的 jar 工作,但可以在 IntelliJ 中工作
Posted
技术标签:
【中文标题】带有 JSP 的 Spring Boot 应用程序不能作为独立的 jar 工作,但可以在 IntelliJ 中工作【英文标题】:Spring Boot application with JSP do not work as a standalone jar but works from within IntelliJ 【发布时间】:2021-08-08 06:09:02 【问题描述】:我有一个简单的应用程序,它应该根据提供的 URL 加载一些简单的 JSP 页面
http://localhost:9191/mypath/ --> 应该加载page1.jsp http://localhost:9191/mypath/first.do --> 应该加载page1.jsp http://localhost:9191/mypath/second.do --> 应该加载page2.jsp在 IntelliJ 中使用应用程序时,上述 URL 可以按预期工作。
但是,在启动应用程序时 - java -jar app.jar --spring.config.location=file:./application.properties 它为上述所有 URL 返回 404。
无法弄清楚原因,如果有人能指出我可能遗漏的任何东西,我将不胜感激。
我的项目详情如下。没有提供 JSP 的详细信息,因为它们是简单的页面 一行文字。
Java version : 11.9
Spring Boot version : 2.3.4.RELEASE
Spring Version : 5.2.9.RELEASE
Maven 项目结构
├── src
│ └── main
│ ├── java
│ │ └── com.springjsp
│ │ └── config
| | └── Config.java
│ │ └── controller
| | └── TestController.java
│ │ └── App.java
│ ├── resources
│ │ └── application.properties
│ └── webapp
| └── WEB-INF
│ └── jsp
│ └── ui
│ └── page1.jsp
│ └── page2.jsp
│ └── error.jsp
App.java
@SpringBootApplication
public class App
public static void main(String[] args)SpringApplication.run(App.class, args);
Config.java
@Configuration
public class Config
@Bean
TestController page1()
return new TestController("/ui/page1","/ui/error");
@Bean
TestController page2()
return new TestController("/ui/page2","/ui/error");
@Bean
SimpleUrlHandlerMapping simpleUrlHandlerMapping()
SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
simpleUrlHandlerMapping.setOrder(1);
Map<String, Object> mappings = new HashMap<>();
mappings.put("", page1());
mappings.put("first.do", page1());
mappings.put("second.do", page2());
simpleUrlHandlerMapping.setUrlMap(mappings);
return simpleUrlHandlerMapping;
TestController.java
public class TestController implements Controller
private String formView;
private String errorView;
public TestController(String formView, String errorView)
this.formView = formView;
this.errorView = errorView;
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception
try
return new ModelAndView(formView);
catch (Exception ex)
return new ModelAndView(errorView);
application.properties
server.port=9191
server.servlet.context-path=/mypath
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
</dependencies>
<resources>
<resource>
<directory>$basedir/src/main/resources/</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>$basedir/src/main/webapp</directory>
</resource>
</resources>
来自控制台的错误
2021-05-19T11:48:34.387 [http-nio-11970-exec-2] INFO o.a.c.c.C.[.[localhost].[/mypath] - Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-05-19T11:48:34.388 [http-nio-11970-exec-2] INFO o.s.web.servlet.DispatcherServlet - Initializing Servlet 'dispatcherServlet'
2021-05-19T11:48:34.388 [http-nio-11970-exec-2] DEBUG o.s.web.servlet.DispatcherServlet - Detected StandardServletMultipartResolver
2021-05-19T11:48:34.399 [http-nio-11970-exec-2] DEBUG o.s.web.servlet.DispatcherServlet - enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data
2021-05-19T11:48:34.399 [http-nio-11970-exec-2] INFO o.s.web.servlet.DispatcherServlet - Completed initialization in 11 ms
2021-05-19T11:48:34.412 [http-nio-11970-exec-2] DEBUG o.s.web.servlet.DispatcherServlet - GET "/mypath/", parameters=
2021-05-19T11:48:34.420 [http-nio-11970-exec-2] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping - Mapped to com.springjsp.controllers.TestController@e638281
2021-05-19T11:48:34.424 [http-nio-11970-exec-2] DEBUG o.s.w.s.v.ContentNegotiatingViewResolver - Selected 'text/html' given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8]
2021-05-19T11:48:34.425 [http-nio-11970-exec-2] DEBUG o.s.web.servlet.view.JstlView - View name '/ui/page1', model
2021-05-19T11:48:34.432 [http-nio-11970-exec-2] DEBUG o.s.web.servlet.view.JstlView - Forwarding to [/WEB-INF/jsp//ui/page1.jsp]
2021-05-19T11:48:34.436 [http-nio-11970-exec-2] DEBUG o.s.web.servlet.DispatcherServlet - Completed 404 NOT_FOUND
2021-05-19T11:48:34.437 [http-nio-11970-exec-2] DEBUG o.s.web.servlet.DispatcherServlet - "ERROR" dispatch for GET "/mypath/error", parameters=
2021-05-19T11:48:34.440 [http-nio-11970-exec-2] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2021-05-19T11:48:34.489 [http-nio-11970-exec-2] DEBUG o.s.w.s.v.ContentNegotiatingViewResolver - Selected 'text/html' given [text/html, text/html;q=0.8]
2021-05-19T11:48:34.494 [http-nio-11970-exec-2] DEBUG o.s.web.servlet.DispatcherServlet - Exiting from "ERROR" dispatch, status 404
【问题讨论】:
当你做java -jar ...
时,你的终端里会有一些spring日志,你能显示那些吗?
application.properties
真的外化了吗?
@Eugene 添加了控制台详细信息
@ScaryWombat 是的。通过命令行提供
你可以试试return new TestController("ui/page1","ui/error")
,注意少了一个斜杠。
【参考方案1】:
JSPs are not supported when using jar packaging。您应该将您的应用程序打包为一个 war 文件,而不是在其 pom.xml 中设置<packaging>war</packaging>
。打包为war时,仍然可以使用java -jar your-app.war
运行。
【讨论】:
谢谢。包装改变为我做到了。以上是关于带有 JSP 的 Spring Boot 应用程序不能作为独立的 jar 工作,但可以在 IntelliJ 中工作的主要内容,如果未能解决你的问题,请参考以下文章
带有 JSP 的 Spring Boot 应用程序不能作为独立的 jar 工作,但可以在 IntelliJ 中工作
gradle + spring MVC + spring boot + jsp = 404错误?
如何使用 Spring MVC & Security、Jasig CAS 和 JSP 视图在 Spring Boot 2 中配置 UTF-8?