Enable JSP Templating on Spring 4 with Maven - java

I'm trying to create an mvc web application using Spring.
I followed this guide http://spring.io/guides/gs/serving-web-content/
this works fine bur I don't understand how to use templates I'd like to have a header, footer to not repeat the code on each pages.
I'd like to use JSP but I don't understand how, there is no WEB-INF folder, no web.xml.
I don't know how to add .tag
Thanks for your help

Add file src/main/resources/application.properties with content
spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp
And dependency:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
Example project: spring-boot-web-jsp-example

Related

swagger-ui.html shows a blank page in a Vaadin + Spring application

In my Spring+Vaadin (18) application I configured this:
vaadin.url-mapping=/ui/*
All Vaadin views are indeed accessible with a "localhost:8080/ui/something" address.
In the pom I have the required dependency for the Swagger UI:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>${springdoc.version}</version>
</dependency>
But the swagger-ui page returns full blank with the following console output by browsing to http://localhost:8080/swagger-ui.html which automatically redirects to http://localhost:8080/swagger-ui/index.html/?configUrl=/v3/api-docs/swagger-config.
The OpenAPI JSON page is accessible on http://localhost:8080/v3/api-docs/
Any idea why the Swagger UI page is not shown?

Cannot resolve method getParameter() in JSP

I'm building a small project in JSP. I want to take data from a HTML sign up form and save them to a database. But my IDE (intellij) won't allow me to do so because of the error in the title. Does anyone know a fix to this? Internet research didn't really helped me.
Thanks in advance!
EDIT
<%
String name = request.getParameter("realName");
%>
Error: Cannot resolve method 'getParameter(java.lang.String)'.
I'm assuming that your JSP file looks like this:
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%
String name = request.getParameter("realName");
%>
Here's the param "realName": <%=name%>
</body>
</html>
And that it looks like this, in your IntelliJ:
If that's the case, I'm almost sure you're missing the servlet-api.jar file in your classpath.
Here's one of the ways to add it on IntelliJ:
Right-click on your project and select Open Module Settings:
Make sure that you're on the Modules section, Dependencies tab, click on the "+" button at the bottom, and select 1 JARs or directories...:
Select the file servlet-api.jar from the folder lib at (THIS IS IMPORTANT:) the container where you're deploying your application (in my case, apache-tomcat-8.5.31):
Then click on the "Ok" button. Your program now should look like this:
You're good to go!
I hope it helps.
Note: I know that sometimes you cannot avoid to use scriptlets, especially when you're working on legacy codes, as I did for a while. Even though, please also pay attention to the other answers here about using scriptlets. There are several other options available.
As a complement to the answer, try this on your intellij ide :
add support framework
check maven
go to pom.xml
add servlet-api dependency :
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>

Spring resources folder

I have a Maven project which use Spring . In this project have a folder called res(in src/main/webapp), which contain resources(css, images, js, etc.). This resources are called in JSP files.
My problem is this: I try to put an image logo.png on index.jsp:
<img src="/res/images/logo.png"/>
In page appear this: Page Result
Here is how my project look: Tree project
How can I access logo.png?
Unless you deploy your webapp as the root webapp in your server, the path of the image is wrong. It should start with the context path of your application:
<img src="${pageContext.request.contextPath}/res/images/logo.png"/>
If you mapped the spring servlet to /, you also need to make these resources available, either by enabling the default servlet, or by mapping the resources as explained in the documentation
Try this:
<mvc:resources mapping="/resources/**" location="/resources/folder_name/"/>
and also i recommend use jstl taglib
For example add dependency :
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
and in your jsp you can use this approach instead you default:
<c:url value="src="images/logo.png"/>
good luck

Spring MVC with Boot: "There was an unexpected error (type=Not Found, status=404)"

So I'm new to Spring and I've so far gotten a simple web API running connected to a MongoDB database, but I'm having trouble generating just plain old views using .jsp or .html files. I've tried a variety of different approaches: InternalResourceViewResolver, XmlViewResolver, returning Strings instead of ModelAndView objects, nothing seems to be working for me. I have the following code:
Edit: here is a git repo with my project: https://github.com/jwallp/Spring-Test
As the above project is, I am getting a white label error upon going to /index which says:
There was an unexpected error (type=Internal Server Error, status=500).
Circular view path [index]: would dispatch back to the current handler URL [/index] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
Edit: So I managed to get the view to finally load by using spring.view.prefix and spring.view.suffix instead of spring.mvc.view.prefix and such, and by moving my WEB-INF directory from my project root to inside <project root>/src/main/webapp/. I just wanted to know, if my WEB-INF directory is contained within another directory, will it still function as intended (making its contents not directly visible)?
Spring Boot has limited support for JSP, because of its use of an embedded servlet container. From the Spring Boot reference 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, i.e. 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. Jetty
does not currently work as an embedded container with JSPs.
Here is a basic example of using jsp in spring boot application.
Hope you have the JSP libraries in your classpath. If you are using maven, including the following dependencies in pom.xml will have those:
<!-- For using JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- If you want to use JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
Also, you may need to put this line at the top of the JSP file:
<%# page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
Update
Doing the following changes to your project at GitHub worked in my environment:
Move the WEB-INF folder into src/main/webapp. That's the place for it.
In application.properties replace
spring.mvc.view.prefix=/WEB-INF/pages/
spring.mvc.view.suffix=.jsp
with
spring.view.prefix: /WEB-INF/pages/
spring.view.suffix: .jsp
Seems the former will work with Spring Boot 1.3, but not not with the current stable release.
Try the Following:
package hello;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class IndexController
{
#RequestMapping("/")
public String index()
{
return "index";
}
}
We ran into this problem at work while upgrading an older application to Spring Boot. What we did:
The prefix and suffix mappings as given above for application.properties (though our prefix was just /WEB-INF/).
Moved our CSS, JavaScript, HTML files to a resources\static folder. We had subdirectories under that for each type.
Places where window.open("somefile.jsp") was used, was changed to window.open("somevalue.do") where somevalue mapped to a #RequestMapping value and the setViewName for the ModelAndView of that method mapped to the previous jsp. Where there was a window.open("somefile.html") we changed it to map to window.open("includes/somefile.html") where includes is a subdirectory in our resources/static tree.
I ran into this problem few times. It was because I put /WEB-INF/ in /src/main/java folder. Latest I created separate path for the INF file in /src/main/webapp and I was able to run my application correctly and displayed the text in the browser.
Following jsp files relocation can solve the problem. I solved my problem this way:
Move the .jsp files to:
"src/main/resources/META-INF/resources/WEB-INF/jsp".
Make sure the application.properties file contains:
spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp
Your controller class should be like:
#Controller
public class IndexController
{
#RequestMapping("/")
public String index()
{
return "index";
}
}
If you are using maven, include the following dependencies in pom.xml
<!-- For using JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- If you want to use JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
It should then work.
I added one line to my application.properties file.
server.tomcat.additional-tld-skip-patterns=hk2-utils.jar,javax.annotation-api.jar,javax.inject.jar,hk2-api.jar,config-types.jar,hk2-core.jar,hk2-config.jar,tiger-types.jar,validation-api.jar,jboss-logging.jar,classmate.jar,hk2-locator.jar,javassist.jar,hk2-runlevel.jar,class-model.jar,asm-all-repackaged.jar
It should then run.
I have used mvn spring-boot:run
It's worked for me
Eventhough I have tried many ways to fix this issue, couldn't find perfect solution.
If you are using Intellij IDEA : do not try to run Spring Boot application(with dynamic .jsp views) with the IDE's run ▶︎ button.
$ cd {PROJECT_FOLDER}
$ ls //Make sure that you are in the same folder where pom.xml resides
and then run below command
$ mvn spring-boot:run
Now your application should be served at localhost:8080.
i have faced the same problem and my solution was replacing the
spring.view.prefix: /WEB-INF/pages/
spring.view.suffix: .jsp
with a configuration class like this. it worked for me!
package com.rbc.sample.user.login.sample;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
#Configuration
public class WebConfig {
public static final String RESOLVER_PREFIX= "/WEB_INF/jsp/";
public static final String RESOLVER_SUFIX=".jsp";
#Bean
public ViewResolver viewResolver(){
UrlBasedViewResolver viewResolver=new InternalResourceViewResolver();
viewResolver.setPrefix(RESOLVER_PREFIX);
viewResolver.setSuffix(RESOLVER_SUFIX);
return viewResolver;
}
}

Quartz.properties

I developed an EAR using Quartz API. I have put my quartz.properties file in the classpath(WEB-INF/classes in war). Added following lines to web.xml file
<context-param>
<param-name>config-file</param-name>
<param-value>/WEB-INF/classes/quartz.properties</param-value>
</context-param>
But Quartz still loads the default properties file from the quartz.jar
I believe that config-file should be a servlet parameter, not a context parameter, like the example given at http://www.openscope.net/2010/02/05/quartz-scheduled-jobs/. See also quartz docs on QuartzInitializationServlet here.
Try with /quartz.properties instead of /WEB-INF/classes/quartz.properties.
The param-value you had inserted is interpreted as a string, not a path.
I believe that upon initialization, the config file is being searched in "WEB-INF\classes" folder... whereas you meant something like "\webapps\\WEB-INF\classes.
(would help to add logs,at least for the initialization part...)
since the resource "WEB-INF\classes\quartz.properties" isn't found, the default quartz.properties is loaded.
You have an error between maven dependencies
erase from your pom.xml this dependencie
<!-- Quartz spring-support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-support</artifactId>
<version>2.0.6</version>
</dependency>
And relaunch...

Categories