Я создаю простое приложение Spring Boot с JSP, используя SB 2.2.5.RELEASE. Вот файловая структура
.//src/main/java/com/myapp/controllers/HelloController.java
.//src/main/java/com/myapp/MyApplication.java
.//src/main/resources/META-INF/resources/WEB-INF/jsp/hello.jsp
и другое расположение jsp, чтобы увидеть, какой из них работает
.//src/main/webapp/WEB-INF/jsp/hello.jsp
Главное приложение
@SpringBootApplication
@EnableConfigurationProperties
public class MyApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
Контроллер
@Controller
@RequestMapping(value = "/hello")
public class HelloController {
private static final Logger LOGGER = LoggerFactory.getLogger(HelloController.class);
@GetMapping(value ={"", "/{name}"})
public String hello(Model model, @PathVariable Optional<String> name) {
// Should call a greeting service that takes 'name' and returns a Model rendered by the view
model.addAttribute("name", name.orElse("world"));
return "hello";
}
}
My pom
......
<modelVersion>4.0.0</modelVersion>
<groupId>com.myapp</groupId>
<artifactId>myapp</artifactId>
<version>0.0.14-SNAPSHOT</version>
<packaging>war</packaging>
......
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.31</version>
<scope>compile</scope>
</dependency>
......
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>compile</scope>
</dependency>
my application.yml
spring:
profiles: local
mvc.view:
prefix: /WEB-INF/jsp/
suffix: .jsp
в моем локальном запуске Spring Boot, инициированном плагином IDE или SB maven, JSP являются разрешается плавно
2020-05-06 15:42:13.411 DEBUG 31461 --- [nio-8080-exec-4] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, application/xhtml+xml, image/webp, application/xml;q=0.9, */*;q=0.8]
2020-05-06 15:42:13.411 DEBUG 31461 --- [nio-8080-exec-4] o.s.web.servlet.view.JstlView : View name 'hello', model {userlogin=Ginger, username=Ginger, name=world, org.springframework.validation.BindingResult.username=org.springframework.validation.BeanPropertyBindingResult: 0 errors}
2020-05-06 15:42:13.414 DEBUG 31461 --- [nio-8080-exec-4] o.s.web.servlet.view.JstlView : Forwarding to [/WEB-INF/jsp/hello.jsp]
2020-05-06 15:42:13.858 DEBUG 31461 --- [nio-8080-exec-4] o.s.s.w.header.writers.HstsHeaderWriter : Not injecting HSTS header since it did not match the requestMatcher org.springframework.security.web.header.writers.HstsHeaderWriter$SecureRequestMatcher@39b2d7c
2020-05-06 15:42:13.858 DEBUG 31461 --- [nio-8080-exec-4] w.c.HttpSessionSecurityContextRepository : SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
2020-05-06 15:42:13.858 DEBUG 31461 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Completed 200 OK
, но после того, как я разверну войну в Tomcat 8.5.3, неясно, как разрешить JSP, хотя я дал JSP в любом месте или в обоих
2020-05-06 15:42:46.053 DEBUG 11973 --- [.15-8080-exec-4] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, application/xhtml+xml, image/webp, application/xml;q=0.9, */*;q=0.8]
2020-05-06 15:42:46.054 DEBUG 11973 --- [.15-8080-exec-4] o.s.web.servlet.view.JstlView : View name 'hello', model {userlogin=Ginger, username=Ginger, name=world, org.springframework.validation.BindingResult.username=org.springframework.validation.BeanPropertyBindingResult: 0 errors}
2020-05-06 15:42:46.061 DEBUG 11973 --- [.15-8080-exec-4] o.s.web.servlet.view.JstlView : Forwarding to [/WEB-INF/jsp/hello.jsp]
2020-05-06 15:42:46.073 DEBUG 11973 --- [.15-8080-exec-4] o.s.web.servlet.DispatcherServlet : "FORWARD" dispatch for GET "/WEB-INF/jsp/hello.jsp", parameters={}
2020-05-06 15:42:46.073 DEBUG 11973 --- [.15-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]
2020-05-06 15:42:46.074 WARN 11973 --- [.15-8080-exec-4] o.s.w.s.r.ResourceHttpRequestHandler : Path with "WEB-INF" or "META-INF": [WEB-INF/jsp/hello.jsp]
2020-05-06 15:42:46.074 DEBUG 11973 --- [.15-8080-exec-4] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
2020-05-06 15:42:46.074 DEBUG 11973 --- [.15-8080-exec-4] o.s.s.w.header.writers.HstsHeaderWriter : Not injecting HSTS header since it did not match the requestMatcher org.springframework.security.web.header.writers.HstsHeaderWriter$SecureRequestMatcher@530970e5
2020-05-06 15:42:46.074 DEBUG 11973 --- [.15-8080-exec-4] w.c.HttpSessionSecurityContextRepository : SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
2020-05-06 15:42:46.074 DEBUG 11973 --- [.15-8080-exec-4] o.s.web.servlet.DispatcherServlet : Exiting from "FORWARD" dispatch, status 404
2020-05-06 15:42:46.075 DEBUG 11973 --- [.15-8080-exec-4] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
Но он нашел префикс / суффикс просмотра mvc, как указано в журнале
2020-05-06 15:26:22.034 DEBUG 11973 --- [ost-startStop-1] o.s.c.e.PropertySourcesPropertyResolver : Found key 'spring.mvc.view.prefix' in PropertySource 'configurationProperties' with value of type String
2020-05-06 15:26:22.035 DEBUG 11973 --- [ost-startStop-1] o.s.c.e.PropertySourcesPropertyResolver : Found key 'spring.mvc.view.suffix' in PropertySource 'configurationProperties' with value of type String
Я искал повсюду, например
Spring Boot JSP 404
попробовал все, что они предлагали, у меня ничего не работает :(. Могу я получить помощь / подсказки / подсказки, пожалуйста? Это так расстраивает