Prateek Kumar
Prateek Kumar

Reputation: 53

Internal resource view resolver not working in spring boot

I have created my "fancy-login.jsp" in src/main/webapp/WEB-INF/view/fancy-login.jsp but my jsp page is not shown when I access localhost:8080/book/login its shown just the page name fancy-login means the string is printed as it is rather than my jsp page. Please help me resolve it.

Bean

 @Bean
    public ViewResolver viewResolver() {
        
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        
        viewResolver.setPrefix("/WEB-INF/view/");
        viewResolver.setSuffix(".jsp");
        
        return viewResolver;
    }

Controller file

@RestController
@RequestMapping("/book")
public class BookController {

    @GetMapping("/login")
    public String getLoggedIn() {
        
        return "fancy-login";
    }
}

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 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.demo</groupId>
    <artifactId>book</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>book</name>
    <description>Demo project for Spring Boot</description>
    <packaging>war</packaging>
    
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <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-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.5</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>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-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>

folder structure

https://i.sstatic.net/D0Wv0.png

Upvotes: 0

Views: 3436

Answers (1)

Ananthapadmanabhan
Ananthapadmanabhan

Reputation: 6216

Replace @RestController with @Controller as a rest controller will not resolve a view , it will only return the data.

@Controller
@RequestMapping("/book")
public class BookController {

    @GetMapping("/login")
    public String getLoggedIn() {
        
        return "fancy-login";
    }
}

Since you are using jsp with springboot make sure that you are using a war packaging instead of the default jar . Jsp does not work well with the jar as packaging.

You could also configure the jsp view resolver by just providing the values in application.properties like :

#ViwResolver
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp

In your case if you are using maven then find pom.xml in your ide and then add the following under version tag :

<packaging>war</packaging>

Also if you are using embedded tomcat add the following under dependency secton of your pom :

  <dependency>
     <groupId>org.apache.tomcat.embed</groupId>
     <artifactId>tomcat-embed-jasper</artifactId>
     <scope>provided</scope>
  </dependency>

And also add JSP Servlet and JSTL support if used in your JSP page then only

Upvotes: 3

Related Questions