Spring-Boot does not find JSP page in WAR file

When starting the spring-boot project (java -jar / path / to / war.war) .jsp files were not found.

Methods annotated with @ResponseBody work fine. The view resolver approaches the correct path to the JSP pages, but they were not found. This project has one configuration class and not web.xml.

Configuration class:

@Configuration @EnableAutoConfiguration @EnableWebMvc @ComponentScan (basePackages = "org.ghc.security.web") class ScMain extends WebMvcConfigurerAdapter { // SpringBoot BootStrap... static void main (String[] args) { ApplicationContext ctx = SpringApplication.run (ScMain, args) System.out.println("Let inspect the beans provided by Spring Boot:"); String[] beanNames = ctx.getBeanDefinitionNames(); Arrays.sort(beanNames); beanNames.each { beanName -> System.out.println(beanName); } } @Bean InternalResourceViewResolver internalResourceViewResolver () { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver() viewResolver.setPrefix("/WEB-INF/jsp/") viewResolver.setSuffix(".jsp") viewResolver } } 

controller

 @Controller class Running { @RequestMapping ("/alive") // This works fine @ResponseBody String amIAlive () { "ALIVE!" } @RequestMapping ("/alive/page") // Path to page resolved, but file not found! ModelAndView amIAlivePage () { new ModelAndView("alivepage") } } 

Error log

2013-11-25 09: 08: 28.714 ERROR 1549 --- [tp1945397783-20] org.apache.jasper.servlet.JspServlet: PWC6117: File "% 2FUsers% 2Fnode42% 2FDevelopment% 2Fmock-security-ui% 2Fbuild% 2Flibs % 2Fmock-security-ui-2.06-SNAPSHOT.war% 2FWEB-INF% 2Fjsp% 2Falivepage.jsp "not found

The path to the .war file in the log entry is correct, and the path in the military file (WEB-INF / jsp / alivepage.jsp) is correct. The answer is the same, whether it be Jetty or Tomcat (the above log was from Jetty). I also tried not to use the view recognizer by specifying one of them, as indicated above, or set the resolution of the view through the properties. I am completely confused because everything looks as if it works, except for this small detail. And the annotated @ResponseBody method in the controller works fine.

If anyone has any suggestions, I would certainly be grateful for the entry!

+6
source share
5 answers

I had the same problem, and in my case it happened because I was not in the library on the class path.

Spring Download does not include Jasper by default, so JSP rendering does not work unless you explicitly include the library:

For Gradle:

 compile("org.apache.tomcat.embed:tomcat-embed-jasper") 

For Maven:

 <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> 
+17
source

I do not think that JSPs in executable archives are fully supported in Spring Boot (it is listed), so I would try to get it to work first with a) a deployed WAR and / or b) an exploded archive (or launch from the IDE) or launch from source. Once this works, you still have to wait for full JSP support (contributions are welcome), but at least you'll know that it works. The error you see in the expanded WAR (lack of matching) indicates that something else is happening. Please note that there is a JSP sample in Spring Boot , if you want to compare something - it works to the point (JSP is enabled and displayed).

Edit: Spring taglibs, JSTL and EL support, seem to work in the example above. I just updated it to add JSTL and test it from the IDE and as an executable WAR.

+3
source

I had a similar problem caused by the default servlet not being mapped. I had to do this in the extends DelegatingWebMvcConfiguration class:

 @Override protected void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } 
+2
source

I solved this problem with a minor correction in the definition of Bean.

 @Bean InternalResourceViewResolver internalResourceViewResolver () { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver() viewResolver.setPrefix("WEB-INF/jsp/") viewResolver.setSuffix(".jsp") return viewResolver; } 
+2
source

I don't know your pom.xml, but it looks like this thread

JSP file not showing up in Spring boot web application

Try adding a dependency list to your pom, please.

0
source

Source: https://habr.com/ru/post/958787/


All Articles