I have a Spring Boot web application that reads index.jsp and uses tomcat jasper to support JSP. However, the page is showing up as "There was an unexpected error (type=Not Found, status=404)." I suspect either the issue lies with the dependency.
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.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.Alex</groupId>
<artifactId>WebApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>WebApp</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>14</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.35</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Hello this shit works
</body>
</html>
The method runs as the print statement executes
#Controller
public class indexController {
// Tomcat Jasper to return jsp files
#RequestMapping("index")
public String getIndex() {
System.out.println("test1");
return "index.jsp";
}
File structure
Add this dependency with other ( like web ) in your pom file.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
In application.properties file add this config properties for file location (Your .jsp file location in class path)
Properties config:
spring.mvc.view.prefix = /WEB-INF/views/
spring.mvc.view.suffix =.jsp
spring.mvc.static-path-pattern=/resources/**
OR
Class level config:
#Configuration
#EnableWebMvc
#ComponentScan(basePackages= {"com.imran"})
public class WebConfig implements WebMvcConfigurer {
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp().prefix("/WEB-INF/views/").suffix(".jsp");
}
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
and your controller will be like this (with out .jsp extension, just file name):
#Controller
public class indexController {
#RequestMapping(value = "/index", method = RequestMethod.GET)
public String getIndex() {
System.out.println("test1");
return "index";
}
}
You can get some help from here if you need.
Related
I'm trying to deploy a very basic Spring Boot web app to AWS Elastic Beanstalk, It works fine locally, but each time I try to run it on elastic beanstalk I get a 404 error.
Here is my Controller:
package com.example.springhelloworld;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class HelloWorldController {
#RequestMapping("/")
public String returnHello(){
return "helloworld";
}
}
Here is my main class:
package com.example.springhelloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
#SpringBootApplication
public class SpringHelloWorldApplication extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(SpringHelloWorldApplication.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SpringHelloWorldApplication.class);
}
}
Here is my html template:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Spring App</title>
</head>
<body>
<h3>Welcome to Spring Hello World!</h3>
</body>
</html>
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 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>3.0.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>SpringHelloWorld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringHelloWorld</name>
<description>SpringHelloWorld</description>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</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-tomcat</artifactId>
<scope>provided</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>
</project>
This is my project structure:
I also set server port to 5000 in application.properties file.
I generated the war file using maven and uploaded to Elastic Beantalk using a Tomcat 8.5 platform with Corretto 11. However when I try to access my application I get a 404 error. I'm trying to figure out why this is happening.
All i am trying to do is print a simple Hello world message , using a JSP page but when i try to do it,i get a 404 not found error as shown below
this is my project structure, shown in the above diagram
package com.example.MailSender;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication(scanBasePackages = {"com.example.MailSender","com.example.MailSender.Controller"})
public class MailSenderApplication {
public static void main(String[] args) {
SpringApplication.run(MailSenderApplication.class, args);
}
}
the main application class is as shown above
package com.example.MailSender.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
#RequestMapping("/hello")
public class SimpleController {
#GetMapping("/world")
public String sample()
{
return "sample";
}
}
A very simple controller that i coded is shown above
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Hello world</title>
</head>
<body>
<h1>Basic config successful!!!</h1>
<h2>Hello World</h2>
</body>
</html>
A very simple JSP page to show the message
server.port=9090
spring.mvc.view.prefix=/src/main/resources/static
spring.mvc.view.suffix=.jsp
this is the application.properties 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 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.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>MailSender</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MailSender</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-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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>
the pom.xml file
i am unable to understand where i am going wrong, please help me out, why am i getting a 404 error ??
i belive http://localhost:9090/hello/world is a valid url
I see two issues.
You need to add this dependency to your pom file
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
/src/main/resources/static is meant to hold static files such as html and css
Create directory src/webapp/WEB-INF/jsp
Move your jsp into the above directory
Update prefix spring.mvc.view.prefix=/WEB-INF/jsp/
https://hellokoding.com/spring-boot-hello-world-example-with-jsp/
Put samples.jsp in "resources/templates"
You muste enable the mvc configuration. Create a class config :
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
#EnableWebMvc
#Configuration
public class Config {
}
https://www.logicbig.com/tutorials/spring-framework/spring-web-mvc/spring-enablewebmvc-annotation.html
I'm new to Spring. I am currently following a tutorial to the T and ran into the "Whitelabel error page". I've done my research, checking Spring boot - Whitelabel Error Page and Spring Boot Remove Whitelabel Error Page with no luck.
I am trying to reach the page by simply using : http://localhost:8080/hello
Obviously Spring cannot find the page, but I cannot figure out why that is.
My html file:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Thymeleaf demo</title>
</head>
<body>
<p th:text="'time on the server is: ' + ${theDate}" />
</body>
</html>
Pom 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 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.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cat.otsGrief</groupId>
<artifactId>GriefReconcile</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>GriefReconcile</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Java class:
package com.cat.ots.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
#Controller
public class DemoController {
#GetMapping("/hello")
public String sayHello(Model theModel) {
theModel.addAttribute("theDate", new java.util.Date());
return "helloworld";
}
}
File Structure:
Any help is appreciated
EDIT:
answer I was looking is found here - Issue With Spring: There was an unexpected error (type=Not Found, status=404)
Add component scanning for packages that are not in the same package, or in sub-packages, of the main class.
you have to configure viewResolver and internalResourceView Resolvers to serve jsp/html files find the below code snippet go through the link
package com.howtodoinjava.app.controller;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
#Configuration
#EnableWebMvc
#ComponentScan
public class MvcConfiguration extends WebMvcConfigurerAdapter
{
#Override
public void configureViewResolvers(ViewResolverRegistry registry) {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/view/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
registry.viewResolver(resolver);
}
}
My request can get into my method of my Welcomecontroller,but it semms that this method can not return a freemarker page to me or spring boot didn't distinguish freemarker(I have put .ftl files into /resources/templates/)
When I input the url http://localhost:8080/index
I got this from my chrome and don't report any errors in my IDEA's console:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Jan 04 22:52:53 CST 2020
There was an unexpected error (type=Not Found, status=404).
No message available
My code is as follow:
#Controller
public class WelcomeController {
#Value("${application.message}")
private String message;
#GetMapping("/index")
public String welcome(Map<String, Object> model) {
model.put("time", new Date());
model.put("message", this.message);
return "welcome";
}
}
#SpringBootApplication
public class QuestionsiteApplication {
public static void main(String[] args) {
SpringApplication.run(QuestionsiteApplication.class, args);
}
}
welcome.ftl:
<!DOCTYPE html>
<html lang="en">
<body>
Date: ${time?date}
<br>
Time: ${time?time}
<br>
Message: ${message}
</body>
</html>
application.properties:
application.message: Hello, Andy
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.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.UU</groupId>
<artifactId>questionsite</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>questionsite</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<commons-lang3-version>3.1</commons-lang3-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-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
As of Spring Boot 2.2 the suffix for Freemarker templates is ftlh not ftl.
This is documented in the Release Notes.
Rename your welcome.ftl to welcome.ftlh and it will work with the default configuration.
Add this to application.yml
server:
servlet:
context-path: /cx
and then try http://localhost:8080/cx/index
Make sure you put .ftl files into /resources/templates/
P.S.
You don't need this dependency, as it is already included in spring-boot-starter-web:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
For springboot 2.6.0
Controller
WelcomeController.java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
...
#Controller
public class WelcomeController {
#GetMapping("/index")
public String welcome(Model model) {
Map<String, Object> mydataMap = new HashMap<>();
LocalDate localDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd LLLL yyyy");
String formattedDateString = localDate.format(formatter);
mydataMap.put("Message", "hello");
mydataMap.put("Date", formattedDateString);
model.addAttribute("mydata", mydataMap); // the attrbuteName should same with the corresponding variable in the template file
return "welcome"; // same with the template file name
}
}
welcome.ftl
<!DOCTYPE html>
<html lang="en">
<body>
<#list mydata as mykey, myvalue>
<br> ${mykey}: ${myvalue}
</#list>
</body>
</html>
application.properties
#
# Default value true for springboot 2.6.0
spring.freemarker.enabled=true
# Default value /templates
spring.freemarker.template-loader-path=classpath:/templates
# Default value ftlh
spring.freemarker.suffix=.ftl
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
Hi I am using Spring boot with maven jar packaging. My project structure looks like this
I am starting my project from command promt with this command:
mvn package && java -jar target/ProjekatNekretnine-0.0.1.jar, and it starts perfect. Than I go to http://127.0.0.1:8080/api/users/home and I do not get my welcome.jsp page. My controller class looks like this:
UserController class:
package com.projekat.nekretnine.controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.projekat.nekretnine.Model.Korisnik;
#RestController
#RequestMapping(value="api/users")
public class UserController {
#RequestMapping(value = "/home")
public String welcome(Model model) {
return "welcome";
}
}
I think that my welcome method should return my welcome.jsp file, but instead it writes "welcome" to the page. Why is that so? What am I doing wrong?
My welcome.jsp file looks like this:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>This is working.</h1>
</body>
</html>
So you see that it shouldn't write welcome.
Here is my pom.xml for you to take a look:
<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.projekat.nekretnine</groupId>
<artifactId>ProjekatNekretnine</artifactId>
<version>0.0.1</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<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.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!-- <version>5.1.30</version> -->
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
One more thing, when I created maven project, I selected that the packaging should be JAR, not WAR. I don't know if I need now web.xml or other xml files, I think not. Maybe the mistake is where I put my static files, if that's the problem, can you tell me where to put tham?
And I tried to use #Controller, instead of #RestController in my UserController class, and still the same problem.
Thank you in advance.
The behaviour your are experiencing is normal.
This happens because you instructed your controller( specifically your rest-controller ) to return a String object when it hits /api/users/home endpoint.
If you however want to expose a page(view) in this endpoint you should configure your spring-boot to use a InternalResourceViewResolver.
You could take a look at a similar question Configure ViewResolver with Spring Boot and annotations gives No mapping found for HTTP request with URI error. and apply the changes for your controller to return the welcome page.