Reputation: 101
I downloaded a sample demo project from spring initializr with web
dependency. And then I created this class.
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class TestController {
@GetMapping("/test")
public String test() {
return "test";
}
}
Its in the same package as the file that starts Spring Boot's run method.
Now I have a test.jsp
file.
Where should I place it? I tried creating a src/main/webapp/WEB-INF/views
folder and placed the file inside it and added prefix and suffix properties in application.properties
as follows
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
I am getting a whitelabel error. How can I just get my controller working? Now if I change @Controller
annotation to @RestController
annotation, I am able to get string test
displayed in the browser as opposed to contents of test.jsp
which works fine because it is supposed to be a RestController. But @Controller
annotation doesn't work at all.
Note: This is the url that I am going to:
http://localhost:8080/test
Update: Here are the logs when I hit that above mentioned url:
2018-06-05 02:21:34.659 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing servlet 'dispatcherServlet'
2018-06-05 02:21:34.659 INFO 9224 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-06-05 02:21:34.659 INFO 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2018-06-05 02:21:34.659 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Using MultipartResolver [org.springframework.web.multipart.support.StandardServletMultipartResolver@27b737cf]
2018-06-05 02:21:34.662 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Unable to locate LocaleResolver with name 'localeResolver': using default [org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver@41be462c]
2018-06-05 02:21:34.665 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Unable to locate ThemeResolver with name 'themeResolver': using default [org.springframework.web.servlet.theme.FixedThemeResolver@7c946fc0]
2018-06-05 02:21:34.669 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Unable to locate RequestToViewNameTranslator with name 'viewNameTranslator': using default [org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator@4f05a3a6]
2018-06-05 02:21:34.673 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Unable to locate FlashMapManager with name 'flashMapManager': using default [org.springframework.web.servlet.support.SessionFlashMapManager@4dc34295]
2018-06-05 02:21:34.674 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Published WebApplicationContext of servlet 'dispatcherServlet' as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcherServlet]
2018-06-05 02:21:34.674 INFO 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 15 ms
2018-06-05 02:21:34.674 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Servlet 'dispatcherServlet' configured successfully
2018-06-05 02:21:34.681 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/test]
2018-06-05 02:21:34.682 DEBUG 9224 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /test
2018-06-05 02:21:34.684 DEBUG 9224 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public java.lang.String com.example.demo.TestController.test()]
2018-06-05 02:21:34.684 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/test] is: -1
2018-06-05 02:21:34.695 DEBUG 9224 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8] based on Accept header types and producible media types [*/*])
2018-06-05 02:21:34.695 DEBUG 9224 --- [nio-8080-exec-1] o.s.w.servlet.view.BeanNameViewResolver : No matching bean found for view name 'test'
2018-06-05 02:21:34.697 DEBUG 9224 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.web.servlet.view.InternalResourceView: name 'test'; URL [/WEB-INF/views/test.jsp]] based on requested media type 'text/html'
2018-06-05 02:21:34.697 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'test'; URL [/WEB-INF/views/test.jsp]] in DispatcherServlet with name 'dispatcherServlet'
2018-06-05 02:21:34.700 DEBUG 9224 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView : Forwarding to resource [/WEB-INF/views/test.jsp] in InternalResourceView 'test'
2018-06-05 02:21:34.702 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/WEB-INF/views/test.jsp]
2018-06-05 02:21:34.703 DEBUG 9224 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /WEB-INF/views/test.jsp
2018-06-05 02:21:34.704 DEBUG 9224 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/WEB-INF/views/test.jsp]
2018-06-05 02:21:34.704 DEBUG 9224 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Matching patterns for request [/WEB-INF/views/test.jsp] are [/**]
2018-06-05 02:21:34.704 DEBUG 9224 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : URI Template variables for request [/WEB-INF/views/test.jsp] are {}
2018-06-05 02:21:34.704 DEBUG 9224 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapping [/WEB-INF/views/test.jsp] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], ServletContext resource [/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@62f87c44]]] and 1 interceptor
2018-06-05 02:21:34.705 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/WEB-INF/views/test.jsp] is: -1
2018-06-05 02:21:34.705 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2018-06-05 02:21:34.705 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Successfully completed request
2018-06-05 02:21:34.706 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Successfully completed request
2018-06-05 02:21:34.707 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
2018-06-05 02:21:34.707 DEBUG 9224 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
2018-06-05 02:21:34.707 DEBUG 9224 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)]
2018-06-05 02:21:34.707 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/error] is: -1
2018-06-05 02:21:34.712 DEBUG 9224 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, text/html;q=0.8] based on Accept header types and producible media types [text/html])
2018-06-05 02:21:34.713 DEBUG 9224 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$SpelView@317e671e] based on requested media type 'text/html'
2018-06-05 02:21:34.713 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Rendering view [org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$SpelView@317e671e] in DispatcherServlet with name 'dispatcherServlet'
2018-06-05 02:21:34.723 DEBUG 9224 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Successfully completed request
Update:
Here is the attached screenshot for folder structure:
Update:
Here is 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.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.2.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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Upvotes: 0
Views: 2764
Reputation: 3086
Please add these dependency in your pom.xml
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
Let me know if it works !
Upvotes: 2