I have just started Spring boot app development. I'm trying to show simple jsp page with default jsp file created by Netbeans 11.1
. I searched this error on the internet and found many questions and answers on the internet that I have tried so far.
Most of the answers say this error is caused by not having controller file
in the same directory as other java files. But that is not my case. First check out my file structure below.
I have tried to load home.jsp
file directly from webapp
folder than from webapp/WEB-INF
folder and than webapp/WEB-INF/jsp
folder as I see this structure from the demo app from Github.
I have added jasper dependency inside .pom
file which is responsible to loading .jsp content.
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
<!--<version>9.0.24</version>-->
</dependency>
Here is my AppContoller class code
@Controller
public class HomeController {
@RequestMapping("home")
public String home(){
System.out.println("HomeController is running!!!"); //showing this output
return "home.jsp";
}
}
Note: This class show the "HomeController is running!!!" output when I type locathost:8080/home
That means the controller is configured correctly and the error is not because of different paths.
My MyWebAppApplication
class has nothing but the default main method. This is what I get in the browser.
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.Tue Sep 10 15:38:44 PKT 2019
There was an unexpected error (type=Not Found, status=404). /home.jsp
If anything else I have to include in question let me know.
I'm using
You need to add following properties:
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp
Replace return "home.jsp";
with return "home";
Also you should add some config properties and dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>4.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
Update: Use following class to run the app:
@SpringBootApplication
public class MyWebAppApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoMvcApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(MyWebAppApplication.class, args);
}
}
By default Netbeans 11.1
generate jsp file with these two line
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
here the prefix is f
and uri is http://java.sun.com/jsf/core
. when I replaced this taglib to
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> //here prefix is `c`
& uri is from jsp/jstl instead of jsf
And commented out the second taglib from default jsp file, It worked for me :).
I supposed these dependencies should do the work but we have to specify the correct tablib in jsp page too.
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
Also now I'm returning home
in the controller instead of home.jsp
as suggested by i.bondarenko in his answer.
Hope this will help someone.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.