Not Able to Resolve View Using Spring boot - spring

I have put all jsp on classpath in views folder.
Folder structure is :
src/main/resources/
src/main/resources/static/
src/main/resources/static/views/
src/main/resources/static/views/*.jsp
src/main/resources/static/views/*.png
I am able to retrieve images file from static folder. But When It comes to jsp I am getting following error.
While hitting url:http://localhost:9001/login
There was an unexpected error (type=Not Found, status=404).
/views/login/login.jsp
However login.jsp exists on given location.

You can place your JSP files in "src/main/resources/META-INF/resources" folder location.
I have created a "WEB-INF/jsp" folder under "src/main/resources/META-INF/resources" and placed the JSP file there.It works. Check the screenshot.
https://i.stack.imgur.com/P3jUI.png
Alternatively, You can create "WEB-INF/jsp" folder under "src/main/webapp" and placed the JSP file there.But for this you have to package it as ".war" extension.

Springboot default tomcat container does not handle JSP. so we need to use below dependency to handle it :
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
And if you are using JSTL tags in JSP then add below jar as well :
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

A template such as a JSP is the exact opposite of static content such as an image file. Put your template in src/main/resources/templates.

Put them in folder :
src/main/resources/templates
2.These three dependencies could help you:
<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>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

I believe by default Spring Boot looks for html files. To change that behavior you have to change your application.properties as following to use jsp-s: spring.view.suffix: .jsp
Maybe even change spring.view.prefix as well to point to static resources.
I would personally suggest to use html-s with Thymeleaf though.

I was supposed to use war as packaging. Jar packaging does not work in case of jsp.

Related

Spring Boot 3 JSP Issue

I've been having issues with JSP since moving to Java 17 and Spring Boot 3. I know that we need to use jakarta.* instead of javax.*, but is there something I'm missing? I am using Spring Tools 4 and just running a basic web app using JSP. When using the following dependencies
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
</dependency>
The project runs, but I get the following error
The superclass "javax.servlet.http.HttpServlet", determined from the Dynamic Web Module facet version (2.5), was not found on the Java Build Path
I can get rid of it by adding the javax servlet dependency
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
But that goes against using javax.* dependencies with Spring Boot 3.
I have read these articles and tried adding the jakarta.servlet.jsp dependency with no luck.
https://debugah.com/solved-tomcat10-error-jakarta-servlet-servletexception-class-com-kuang-servlet-helloservlet-is-not-a-servlet-22749/
https://howtodoinjava.com/java/exception-handling/solved-the-superclass-javax-servlet-http-httpservlet-was-not-found-on-the-java-build-path-in-eclipse/
Solved!
All I had to do was go into Project Properties and under Project Faces, change my Dynamic Web Module from 2.5 to 5.0
The JSTL warning can be suppressed under Properties->Web->JSP Files->Validation->Custom actions->Other problems with TagExtraInfo class (Just change from 'Warning' to 'Ignore')

Micronaut Swagger multimodule

I am trying to visualize the API definitions from a Multimodule project. First I want to start from simple, and I want to visualize the yaml from the parent module. Then I will add other APIs in my Swagger Controller to call the specific url as is done in the example https://github.com/frehov/micronaut-swagger-server
I have my Swagger Controller and my SwaggerConfiguration with my index.hbs inside resource > views.swagger I took the example from the repo https://github.com/frehov/micronaut-swagger-server
This is my Result:
Instead of something like:
When I compared the two project, somehow my view.swagger package from my target folder gets generated into a different way, compared with the example:
What I am missing?
Could you please look at my repo:
https://github.com/amhg/swagger
Thank you
The folder containing the swagger-ui-wrapper (index.hbs) in your project is called 'views.swagger', while the sample has defined 'views/swagger' (so swagger is a folder inside of views). Change that and you get the views/swagger result in your target.
You also need to enable the micronaut handlebars-views (i.e. serving the .hbs files from a controller); add these to the pom.xml of startup (not at the root-pom!):
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-views</artifactId>
</dependency>
<dependency>
<groupId>com.github.jknack</groupId>
<artifactId>handlebars</artifactId>
<version>4.1.0</version>
<scope>runtime</scope>
</dependency>
These will perform the magic of rendering the configuration you return from the controller into an html-page.
Depending on where you're going to put the swagger-annotated controllers, you might also want to add
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger.version}</version>
<scope>compile</scope>
</dependency>

Unable to serve JSP in Spring Boot applications

I have tried several tutorials to serve JSP pages using Spring Boot. They all return a 404 page not found error.
To overcome the known limitations, I'm using a WAR packaging, with the following dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
I have defined the path where JSP pages are in application.properties:
spring.mvc.view.prefix= /WEB-INF/jsp/
spring.mvc.view.suffix= .jsp
When requesting a JSP page, the following WARN is displayed:
WARN 10251 --- [io-8080-exec-11] o.s.w.s.r.ResourceHttpRequestHandler : Path with "WEB-INF" or "META-INF": [WEB-INF/jsp/hello.jsp]
Have JSP been deprecated in Spring Boot 2? Do you have any Spring Boot 2 working example with JSP ?
can you please try adding scope in your dependency just like this
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
I'm using Intellij IDEA. I found out that we cannot just run the SpringBootApplication main class directly. We need to let the maven do the work.
Short solution: maven run you application by the command below from your module root directory
mvn clean package spring-boot:run
You can also add a run configuration using Maven so that you don't have to type the command every time.
Some said JSPs folder should be put under src/main/resources/META-INF/resources/WEB-INF/jsp. This indeed solves the spring boot application run problem though, it will fail when you run the application using tomcat. So we still need to keep the structure if we are going to deploy the application to Tomcat.
Unfortunately, I couldn't find any Spring Boot 2 example able to serve JSP pages as they all return a 404 error. As a workaround I have configured the application to be deployed on WildFly, as described in this tutorial and run my application with JSP on WildFly.
If you want example here it is.
This also help you.

Springboot 2.0.2 not working with thymeleaf template, throwing 404 error

I am using Springboot 1.5.7 for my rest api application, and I am using thymeleaf template to send emails from my api. But when I updated the version of spring boot to 2.0.2 its throwing 404 error i.e "Error resolving template "error", template might not exist or might not be accessible by any of the configured Template Resolvers".
Below is the config I have in application.yml
spring:
thymeleaf:
cache: false
enabled: true
mode: HTML5
prefix: /templates/
suffix: .html
thymeleaf version in pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
Below is the template structure i am using,
My app release is very near and I am badly stuck with this problem, if someone can provide me workaround then it would be a great help.
Remove prefix: /templates/ from application.yml
If still does not work, add thymeleaf-layout-dialect dependency (See: Thymeleaf stopped resolving layout templates after migrating to Thymeleaf 3)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>
FYI, I put my views in WEB-INF/webapp/views, so I am using prefix: /WEB-INF/webapp/views/ (spring boot war deploy to tomcat)
As stated in the 2.0 migration guide.
The Thymeleaf starter included the thymeleaf-layout-dialect dependency
previously. Since Thymeleaf 3.0 now offers a native way to implement
layouts,
we removed that mandatory dependency and leave this choice up to you.
If your application is relying on the layout-dialect project, please
add it explicitly as a dependency.
Adding the following dependencies should work
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>
SOURCE : https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide#template-engines

Adding spring library for usage of JSP Taglibs for security in Freemarker

I am using spring with freemarker as the template engine. Freemarker allows to use the Jsp Taglibs, for security for example, by adding
<#assign security=JspTaglibs["http://www.springframework.org/security/tags"] />
to the templates, what allows me to use for example
<#security.authorize ifNotGranted="ROLE_ADMIN">
whatever
</#security.authorize>
But, Spring/Freemarker cannot find the taglibs, unless they are added to the classpath, so I added
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>${spring.version}</version>
</dependency>
to my pom.xml in my project.
But anyway, the tags couldn't be found! I had to add the spring-security-taglibs.jar into WEB-INF/lib folder for the tags to be found.
Does someone know why the jar has to be added explicitly into the lib folder?? Why aren't they found by tomcat, in my case?
EDIT #ddekany
Thank you. The stacktrace is the following, if the spring-security-taglibs.jar is not copied into the WEB-INF/lib directory
No mapping defined for http://www.springframework.org/security/tags
The problematic instruction: ---------- ==> assignment:
security=JspTaglibs["http://www.springframework.org/security/tags"]
[on line 12, column 1 in home.ftl] in user-directive content.main
[on line 8, column 9 in home.ftl] in user-directive layout.global
[on line 2, column 1 in home.ftl]
---------- Java backtrace for programmers: ----------
freemarker.template.TemplateModelException:
No mapping defined for http://www.springframework.org/security/tags at
freemarker.ext.jsp.TaglibFactory.get(TaglibFactory.java:180) at
...
In case anyone else runs into this...
You need to add the spring support files, as outlined here (just some cut & paste) http://static.springsource.org/spring-webflow/docs/2.2.x/reference/html/ch13s09.html.
And then add some dependencies:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-faces</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>
Assuming you have everything else working, you should now be able to add the taglib to your pages. For example:
xmlns:sec="http://www.springframework.org/security/tags"
< sec:authorize ifAllGranted="USER_ROLE">
Hello user
< /sec:authorize>
*had to add a space b/f 'sec' to post it
Use this Maven dependency:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
org.springframework and org.springframework.security are different frameworks with different version numbers.
Did you include the JspSupportServlet as stated here and here
[EDIT]
After reading your post a bit more careful I advise you the read section "JSP.7.3.2" (and onwards) from the JSP specification.

Resources