JSP file is not rendering in Spring-Boot after included dependencies - spring-boot

I am creating simple demo Spring boot application. I have included all required dependencies in pom.xml file for jsp and set in application.properties as well but still not working.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- To compile JSP files -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project
controller.java
package com.example.demoController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class controller {
#RequestMapping("/")
String first() {
return "index";
}
}
here is the project directory tree
please suggest something to resolve this problem.

Your JSP file should be in by default "src/main/webapp/WEB-INF/jsp" directory and right now it seems to be in wrong location.
UPDATE:- Looking at your shared code - the reason you are facing this problem is that you have annotated your Controller with #RestController instead of #Controller because of which the String you are returning from your controller is being written directly on the output stream instead of framework first checking the view name against a JSP template and then rendering it. If you change the annotation to #Controller it works fine

Related

When I deploy Spring Boot app on Elastic Beanstalk It cannot find, .jsp files

I have simple Spring Boot Web application which runs in local as it should be. However when I deploy it on elastic beanstalk, I am getting 404 error which says /WEB-INF/jsp/index.jsp not found.
Here is my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>arge</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>arge</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
and application.property files:
server.port=5000
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.mvc.static-path-pattern=/resources/**
and here is my controller class:
package com.example.arge.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
#Controller
public class HelloController {
#GetMapping("/")
public String index() {
return "index";
}
}
the index.jsp file is located under src/main/webapp/WEB-INF/jsp/index.jsp.
All things seems to be ok, I could no figure out what is wrong

Everytime i am getting error- Whitelabel Error Page This application has no explicit mapping for /error

I am new to spring Boot and followed some basic steps while creating a simple web application.
What i did is i created a folder like- src/main/webapp/WEB-INF/jsp and inside jsp i created home.jsp file
Then a controller class where i provided a path via #RequestMapping("/") function. But still i am not able to view my .jsp file. Please help
Structure
Controller class
Spring Boot Application
Application Properties
pom.xml
pom.xml-
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Try to separate your main and your controller packages. I suggest that you add another package : com.example.demo.controller and change your controller class to that package
Also try to change the #Controller annotation to #RestController
Probably isn't relevant anymore but might help other people:
Try extending SpringBootServletInitializer.
public class demoApplication extends SpringBootServletInitializer {

Spring Data Source is an unknown property?

I am trying to configure my data source in application properties resource and getting a yellow warning that spring data source is an unknown property. Error is coming up with all pasted line below.
How to properly configure it so i can get rid of this error?
I have checked the pom.xml and mysql and jpa dependencies are also imported correctly.
This is my first project so i don't know how to solve this error. I think if i dont resolve it here it will turn into more severe error.
spring.datasource.url=jdbc:mysql://localhost:3306/projectdb
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.show-sql=true
Here's the main class code:
package com.bilal.student.dal;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class StudentdalApplication {
public static void main(String[] args) {
SpringApplication.run(StudentdalApplication.class, args);
}
}
Here's the pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bilal.student.dal</groupId>
<artifactId>studentdal</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>studentdal</name>
<description>STUDENT DAL</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
I have resolved the error #Martin Zaragoza. Actually problem was in my project build path. I have deleted the .m2 folder in my C:\Users\hp.m2 and all the error gone. Now i am successfully building my project. Thanks for sticking up with me.

Spring boot app showing 404 for index.html

I have a Spring boot application and a static index.html file located in src/main/resources/static
my controller is straight forward and looks like this
#Controller
#Log4j
public class StartController {
#Autowired
OwnerRepository ownerRepository;
#RequestMapping("/")
public String index() {
return "index";
}
}
my only configuration is the main one and it only has the #SpringBootApplication
Im getting 404 when I hit the URL, as per my understanding no further configuration is needed
by the way in the logs on the startup im seeing the following
URI Template variables for request [/index] are {}
here is my POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.datasol</groupId>
<artifactId>alquifacil</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>AlquiFacil</name>
<description>spring boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
On your pom.xml i didn't see any template engine like Thymeleaf, Velocity ecc but you have spring-boot-starter-web, so basically you resource folder its configurated, but you should put your file index.html on templates folder. Static folder serve to contain your asset files.
If you are working local, try:
localhost:8080/ or localhost:8080/alquifacil/ assuming you are using port 8080 or add a template engine dependency in pom.xml like Thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
html is a static resource
so you can access by "localhost:8080/index.html"
or
use "forward" keyword
#RequestMapping("/")
public String index() {
//has to be without blank spaces
return "forward:index.html";
}
please delete the target file and run again. it can be a cashing problem.

How to create a multi-module maven project with spring boot

I am really new to spring boot and maven. I have tried to create a multi-module spring boot project using maven. When I create that and tried to access my controller endpoint through the url it will display 404 error.
I have tried a lot to resolve this problem and searched in the online documents but I was failed. Can anybody help me to resolve this.
In my project there are two modules called demo and activityops.
the parent pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.multimodule</groupId>
<artifactId>multimodule</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<modules>
<module>activityops</module>
<module>demo</module>
</modules>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<start-class>com.example.DemoApplication</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
pom.xml in the activityops module is below
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>multimodule</artifactId>
<groupId>org.multimodule</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>activityops</artifactId>
pom.xml in the demo module is below
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>multimodule</artifactId>
<groupId>org.multimodule</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>demo</artifactId>
</project>
My controller class is below
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.ws.rs.core.MediaType;
#org.springframework.web.bind.annotation.RestController
#RequestMapping("/user")
public class TestController {
private static org.slf4j.Logger LOGGER = LoggerFactory.getLogger(TestController.class);
#RequestMapping(method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON})
public User getSingleUser() {
User user = new User();
user.setId(11l);
user.setFirstName("Shane");
user.setLastName("John");
return user;
}
}
I have typed the following url to access the controller method.
http://localhost:8080/user
This will display and 404 error. Can anybody please help me to resolve this?
Thanks
There is quite a lot going on in your project right now but the biggest problem is that your demo project has no dependency on the other module.
Also, that module defines your controller in the root package (don't do that!). Your Spring Boot application is located in com.example. By default, Spring Boot will scan the package where your Spring Boot application is located and all its sub-packages. TestController is not in com.example so it's not found. That's why you get a 404.
There were other issues, I have submitted a pull request that demonstrates what you can do to make this sample project works. Check the commit message for more details.

Resources