3

Hi I have create simple spring boot application using spring initializr. I added one controller in same folder of main application class.

package com.demo.CrudOperations;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


  @Controller
  public class RegistrationController {

      @RequestMapping(value = "/welcome", method= RequestMethod.GET )
      public String register(){
        return "registration";
    }

  }

This is giving me following error for url http://localhost:8080/welcome

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sat Dec 19 12:51:44 IST 2020 There was an unexpected error (type=Not Found, status=404).

If I use @restcontroller instead of @Controller it is returning me "registration" . In my WEB-INF under views I have created registration.jsp. Here is application.properties file

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

pom.xml showing unknow error.

<?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.5.0-SNAPSHOT</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.CrudOperations</groupId>
    <artifactId>CrudOperations</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>CrudOperations</name>
    <description>Demo project for Crud Operations</description>

    <properties>
        <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-jersey</artifactId>
        </dependency>
        
        <!-- JSTL for JSP -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</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>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

Here is how directory structure looks like

enter image description here

Can some one tell me where I am making mistake ?

I also tried adding @ResponseBody but it gave me simple string instead of jsp file.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
Pravin Kottawar
  • 71
  • 2
  • 18
  • 1
    Note: If you're not experienced enough with Spring to debug this problem, you shouldn't be messing with pre-release builds. (Your problem is almost certainly to do with JSP paths. JSPs are a headache, and this is one of many reasons why; Thymeleaf is a superior alternative for new projects.) – chrylis -cautiouslyoptimistic- Dec 19 '20 at 09:38
  • @chrylis-cautiouslyoptimistic- could this be a bug, why would JSP behave like this? Am struggling myself with the same issue.Here is my post: https://stackoverflow.com/questions/65366871/could-jsp-file-not-found-be-a-bug-in-spring/65369026#65369026 – Gabriel Rogath Dec 19 '20 at 16:16
  • @GabrielRogath tl;dr JSP is a legacy technology and `webapp`/`WEB-INF` have weird rules that are hard to keep straight. With other template engines, the templates are just data files, and the normal rules apply. – chrylis -cautiouslyoptimistic- Dec 19 '20 at 16:25
  • JSP will only work with a war file due to resource loading. See (amongst others) https://github.com/spring-projects/spring-boot/issues/23829 and read [the documentation](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-jsp-limitations). – M. Deinum Dec 23 '20 at 12:45

3 Answers3

0

Add "/" at the end of property:

spring.mvc.view.prefix=/WEB-INF/views/

Saddam
  • 31
  • 4
0

I had the same problem , I had done many attemps but none has worked because, the real problem was that, theamleaf was disabled from , application.yalm

  server:
  port: 8091
  servlet:
    session:
      timeout: 600s

spring:
  thymeleaf:
    enabled: true
  datasource:
    name: dbname  #数据库名
    url: jdbc:mysql://localhost:3306/
    username: username  #用户名
    password: password  #密码
    driver-class-name: com.mysql.cj.jdbc.Driver  #数据库链接驱动
    type: com.alibaba.druid.pool.DruidDataSource #使用druid数据源
  servlet:
    multipart:
      max-file-size: 10MB
      max-request-size: 10MB
  redis:
    port: 6379
    host: "localhost"

mybatis:
  type-aliases-package: matsk.mszdqabbs.Pojo
  mapperLocations: classpath:mapper/*.xml
  configuration:
    use-generated-keys: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

logging:
  pattern:
    file: "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n"
    console: "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n"
  file:
    max-history: 30
    max-size: 10MB
    path: "./logs"
    name: "./logs/mszdqabbs.log"
  level:
    root: INFO

Tinsae
  • 577
  • 5
  • 10
-1

This dependency means you are internally using Spring MVC for your spring boot app.

 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

So you need to use a ModelAndView object here that must be returned by your method,

ModelAndView is a holder for both Model and View in the web MVC framework. Note that these are entirely distinct. This class merely holds both to make it possible for a controller to return both model and view in a single return value. Represents a model and view returned by a handler, to be resolved by a DispatcherServlet. The view can take the form of a String view name which will need to be resolved by a ViewResolver object; alternatively a View object can be specified directly. The model is a Map, allowing the use of multiple objects keyed by name.

More details on ModelAndView here
Try this

@RequestMapping(value = "/welcome", method= RequestMethod.GET )
  public ModelAndView register(){

    return new ModelAndView("registration");
}
  • The dependency is already there and you don't have to return a `ModelAndView` there are multiple other options. – M. Deinum Dec 23 '20 at 12:42