4

ANOTHER EDIT: I just re-read it and the question is very ambiguous: the problem is Tomcat won't show me the views. They ARE in a folder under Tomcat's webapps directory.

EDIT: BOUNTY available. The code is here

I have a spring boot web app that runs fine under eclipse tomcat server when I navigate to http://localhost:8080/swa-boot/. When I try to deploy it as a war under Tomcat 9.0.14 externally I get:

Message /swa-boot/WEB-INF/views/home.jsp

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

home.jsp is in the views folder.

I have tried adding to pom the resource:

        <directory>${basedir}/src/main/webapp</directory>
        <includes>
            <include>**/**</include>
        </includes>

And the dependency

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

I have also tried removing the ‘provided’ scope.

I have in application.properties:

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

I have changed any <a tags in the jsp pages to be of the form: <a href=" <spring:url value="/mappedpath" /> ">TakeMeToTheController</a>.

I have added the views folder to the Deployment Assembly.

The folders under tomcat web apps look like: enter image description here

And under the tomcat server web apps directory: enter image description here

Any ideas?

EDIT: Deployment Assemblies: enter image description here

schoon
  • 2,858
  • 3
  • 46
  • 78
  • Have you seen this? https://stackoverflow.com/questions/43186315/tomcat-404-error-the-origin-server-did-not-find-a-current-representation-for-th – diogenesgg Feb 09 '19 at 01:44
  • I have thanks. That is for problems running in Eclipse. Mine runs fine in Eclipse. – schoon Feb 09 '19 at 11:07
  • I know it's been a while since I've worked on projects that use .jsp files, but isn't it weird to store them in the `WEB-INF` directory in your project? You can even see in the final project that they've been moved to the `WEB-INF/WEB-INF` directory. – Powerlord Feb 12 '19 at 13:49
  • Maybe! It is someone else's project I am just trying to make it BOOT-able. – schoon Feb 12 '19 at 13:51
  • @schoon, I agree with Powerlord it is weird to have jsp files under `WEB-INF`, usually they are all under `webapp` folder like `webapp/views/some.jsp` – nortontgueno Feb 12 '19 at 17:53
  • @schoon, try my answer and if it not work give a try to move your JSP's to `webapp` folder – nortontgueno Feb 12 '19 at 17:54
  • Why do you have `WebContent` in your war plugin ? Try removing it as your web resources are inside /src/main/webapp which is default web resource location. Beside that I dont see any issues. how are you building your war ? eclipse or mvn from command line ? – s7vr Feb 12 '19 at 22:41
  • To run: in Eclipse I do Maven - update project, then Maven install, then I copy the resulting war into the tomcat webapps folder. – schoon Feb 13 '19 at 10:17
  • can you try mvn clean install from command line from the project root ? Just to isolate if eclipse is building your project incorrectly. Check the m2 folder for your generated war. – s7vr Feb 13 '19 at 10:21
  • @Veeram. Tried that no m2 folder but under target folder the war looks ok, same as before anyway, i.e. doesn't work. – schoon Feb 13 '19 at 10:46
  • Can you add the Deployment Assembly screenshot from eclipse ? – s7vr Feb 13 '19 at 13:02
  • Done. I removed the broken one, and that did not help. – schoon Feb 13 '19 at 13:08
  • @schoon Please look at my answer. To resolve your main issue you have to get the war structure right. Once you have it and should resolve your jsps. As next steps you can then change web xml into java annotation config for spring boot. – s7vr Feb 13 '19 at 14:29

3 Answers3

0

Looks like the generated war does not pick up contents of your src/main/webapp folder.

Change line no. 72 in your pom.xml as below <warSourceDirectory>src/main/webapp</warSourceDirectory>

This way the generated war will have contents of src/main/webapp. Test the new war and see. Check this question to see how to correctly specify warSourceDirectory.

Edit In adition you need to remove the web.xml file from src/main/webapp/WEB-INF folder. Either delete it or move it outside src folder.

Next run an mvn clean install and deploy target/swa-boot.war to tomcat.

A working copy is here - https://storage.googleapis.com/schoon/swa-boot.zip

Cyac
  • 447
  • 3
  • 15
  • Thanks. Tried that: same error but `Message /swa-boot` only without the subfolders. – schoon Feb 12 '19 at 16:29
  • Can you write down the exact sequence of steps you follow starting with the way you run the maven job to the deployment? – Cyac Feb 12 '19 at 18:08
  • Also I suspect the tags may not be loading properly. Comment out tag related references from your home.jsp and see. Lines 3,5,6 90 and if you are including copyright tag somewhere in home.jsp then comment out that as well. – Cyac Feb 12 '19 at 18:31
  • To run: in Eclipse I do Maven - update project, then Maven install, then I copy the resulting war into the tomcat webapps folder. – schoon Feb 13 '19 at 09:27
  • Okay. The tags are not the issue. But you need to make 2 more changes in addition to change in pom.xml. 1. Add the below line at the top of src/main/resources/application.properties ````server.contextPath=/swa-boot```` – Cyac Feb 13 '19 at 12:40
  • 2. The presence of web.xml is causing an IllegalStateException resulting in a deployment failure in Tomcat. So remove the web.xml i.e. either delete it or move it outside the src folder. Now recompile and deploy. See what happens. – Cyac Feb 13 '19 at 12:44
  • Yes deleted, no effect. – schoon Feb 13 '19 at 13:04
  • Well, that should not be the case. Because I used your same code base. Made the same changes and I am able to load the home.jsp from a tomcat. Infact, the change 2 (in application.properties) is not needed. Change in pom.xml and deleting the web.xml should be enough to load the home.jsp from within a tomcat version 9.0.16. Make sure you are making only the changes I mentioned. Completely delete the existing deployments from your tomcat and redeploy or may be deploy to a fresh tomcat instance. Because as I said, I made these same changes and I can see it working. – Cyac Feb 13 '19 at 13:10
  • Interesting. I have tried different tomcats and different machines. Are you able to upload your code so I can try that? Maybe I have messed something up as I have made a bout 1 billion changes. – schoon Feb 13 '19 at 13:19
  • Started getting: `13-Feb-2019 13:31:34.963 SEVERE [main] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class [org.springframework.web.context.ContextLoaderListener] java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml!` – schoon Feb 13 '19 at 13:36
  • ````java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml!```` You are getting this because you have not removed the web.xml. You will see that if you unzip the war file. Delete the ````web.xml```` from ````src/main/webapp/WEB-INF/```` and run an ````mvn clean install```` and deploy the ````swa-boot.war```` file that gets generated inside ````target```` directory – Cyac Feb 13 '19 at 14:00
  • 1
    Please confirm if this worked and if you have downloaded the zip file from the url i added in my answer. (I will delete it after you download to avoid storage costs). Also please accept this answer. – Cyac Feb 13 '19 at 14:36
  • Yes it worked, did not need to download it in the end. Thansk again! – schoon Feb 14 '19 at 10:34
0

Looks to me like your config is incorrect:

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

The file referenced - servlet-context.xml - does not exist under WEB-INF but under src/main/resources which means at runtime it will be under the root of the classpath and should therefore be referenced as below:

<param-value>classpath:/servlet-context.xml</param-value>

You can also remove this from your POM and delete the WebContent folder completely as the root of your web app, being a Maven project, is now webapp and not WebContent.

<warSourceDirectory>WebContent</warSourceDirectory> 
Alan Hay
  • 22,665
  • 4
  • 56
  • 110
0

You have to make a couple of changes.

First change is to remove resources configuration. This is packing everything under /src/main/webapp into WEB-INF/classes with same source structure.

<resources>
   <resource>
    <directory>${basedir}/src/main/webapp</directory>
       <includes>
          <include>**/**</include>
       </includes>
   </resource>
   <resource>
    <directory>src/main/webapp</directory>
   </resource>
   <resource>
    <directory>${basedir}/src/main/resources</directory>
      <includes>
         <include>**/**</include>
      </includes>
   </resource>
</resources>

Next change is to remove <warSourceDirectory>WebContent</warSourceDirectory> from the war plugin. This is preventing from processing the web resources.

<plugin>
    <artifactId>maven-war-plugin</artifactId>
</plugin>

Next step is to run maven clean install and You should end up the below structure inside your war and spring would be able to locate your jsps correctly.

WEB-INF
   classes
      org
      META-INF
      static
      templates
   lib
   spring
   tags
   views
resources

Update - Finally was able to checkout your project. To run spring boot application as a traditional war inside a container you need to make few more changes.

Web.xml change to locate the app servlet correctly and remove the SpringBoot ServletIntializer and remove the import resources annotation.

That's all the changes.

Changes available in github - https://github.com/saagar2000/springboot-traditional

enter image description here

enter image description here

s7vr
  • 73,656
  • 11
  • 106
  • 127
  • Thanks Veeram, but it was the `web.xml` that was the problem. – schoon Feb 14 '19 at 10:35
  • You are welcome. How does your war look like now ? Does it still have multiple web inf folder ? Put the resources and war plugin as you have you will again see the same error. It is the package that is the main issue for page not found. – s7vr Feb 14 '19 at 11:20
  • Yes it still has 2 WEB-INF. What should get rid of it? – schoon Feb 15 '19 at 11:01
  • Try my answer. It should get rid of multiple web inf and that is only changes you have to make. Remember the original error /WEB-INF/views/home.jsp not found. So after marking mentioned changes you will see your home jsp under that folder and everything should work as expected. – s7vr Feb 15 '19 at 11:08
  • I have now added the tested code with screenshot and link to repo. – s7vr Feb 15 '19 at 23:13