简体   繁体   中英

Intellij Run Configuration Spring Boot vs Maven Issues

I'm trying to run my Spring Boot project with Intellij IDEA. This project is of type Spring MVC, meaning that it has JSP files in path main/webapp/WEB-INF/jsp .

My application.properties has these settings:

spring.view.prefix=/WEB-INF/jsp/
spring.view.suffix=.jsp

When I'm running the project in Intellij as a "Spring Boot Run Configuration" the server can't identify the path of the JSP files. This is the message i get:

在此输入图像描述 z

If I run the project with "Maven Run Configuration" with the command spring-boot:run everything is working fine.

I have no idea why this is happening. The code and settings of the project are the same with each Run Configuration.

You can try this project (not my project) to understand what I mean https://github.com/mariuszs/spring-boot-web-jsp-example .

My project is behiving the same. I should note that when the project is running with Spring Boot Run Configuration I do see in the console that the controllers are mapped correctly.

2017-06-28 08:29:13.906  INFO 10308 --- [           main] o.s.w.s.h.SimpleUrlHandlerMapping        : Mapped URL path [/login] onto handler of type [class org.springframework.web.servlet.mvc.ParameterizableViewController]

2017-06-28 08:29:13.835  INFO 10308 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/login],methods=[GET]}" onto public java.lang.String com.intuit.mintbills.controllers.generic.LoginController.getLoginPage()

This is a problem with IntelliJ and Spring boot. You should check the following things:

Web facet

Make sure that you have the Web facet enabled. The sample project does not have a web.xml file, and due to that, IntelliJ doesn't pick up src/main/webapp . You can add a dummy web.xml file to src/main/webapp/WEB-INF and reimport the project, or you can manually add the web facet and configure it properly. If the web facet is configured correctly, you should see a blue circle in the webapp folder.

An example of a dummy web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
</web-app>

And this is the "blue dot" in the src/main/webapp folder:

项目结构

Provided dependencies

Another issue with IntelliJ is that the provided dependencies are not properly picked up. You can usually fix this by opening Project Structure... , by opening Modules and selecting the Dependencies tab. In here you can override the scopes of your dependencies, so in your case I would suggest configuring both tomcat-embed-jasper and ecj to Compile .

依赖配置

I haven't done this recently, but it's possible that this will be overriden everytime you change your pom.xml . In that case I would suggest overriding the <scope> in you pom.xml :

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

If you're planning to deploy your application on a web container (eg. an external Tomcat), you may want to work with profiles, otherwise this could lead to conflicts since these dependencies are provided by the web containe.

JSP limitations

It's also recommended to change you packaging to WAR, since there are a few known limitations by using JSPs with JAR files. Quoting the documentation:

When running a Spring Boot application that uses an embedded servlet container (and is packaged as an executable archive), there are some limitations in the JSP support.

  • With Tomcat it should work if you use war packaging, ie an executable war will work, and will also be deployable to a standard container (not limited to, but including Tomcat). An executable jar will not work because of a hard coded file pattern in Tomcat.
  • With Jetty it should work if you use war packaging, ie an executable war will work, and will also be deployable to any standard container.
  • Undertow does not support JSPs.
  • Creating a custom error.jsp page won't override the default view for error handling, custom error pages should be used instead.

我通过在运行配置中将工作目录设置为$ MODULE_DIR $来解决了这个问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM