【问题标题】:Spring boot : MVC not returning view : thymeleaf & Spring dataSpring boot:MVC 未返回视图:thymeleaf 和 Spring 数据
【发布时间】:2017-09-04 22:16:20
【问题描述】:

全部,

我正在尝试制作一个简单的 mvc spring boot 应用程序,当控制器接收到对“/”的请求时,我将它放在我的代码中以返回 index.html。

我不确定,但这不起作用。

SpringDataWebApplication.java

    package com.demo.main;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@SpringBootApplication
@EnableAutoConfiguration
@EnableJpaRepositories("com.demo.repositories.*")
@EntityScan("com.demo.entities.*")
@ComponentScan(basePackages = {"com.demo.controller.*","com.demo.service.*"})
public class SpringDataWebApplication {

    //TODO : Logging


    public static void main(String[] args) {

        SpringApplication.run(SpringDataWebApplication.class, args);


    }

}

HealthCCheckController.java

package com.demo.controller;

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

@Controller
public class HealthCheckController {

    @RequestMapping("/")
    public String getSystemHealth() {
        return "index";
    }

}

UserRepository.java

package com.demo.repositories;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.demo.entities.UserEntity;

@Repository
public interface UserRepository extends CrudRepository<UserEntity, String> {

}

index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>BlogPost URL check</title>
</head>
<body>
<h1>HealthCHeck Working as desired</h1>
</body>
</html>

Index.html 根据 thymeleaf 位于 /templates 目录中

pom.xml

<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>org.demo</groupId>
    <artifactId>Example1</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>0.7.4</version>
        </dependency>


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

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

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>



</project>

UserEntity.java:这还不是必需的,因为我正在一步一步地进行,我只希望对请求“/”做出响应,但我无法得到响应。

package com.demo.entities;

import java.io.Serializable;
import java.util.Calendar;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

@Entity
@Table(name="user")
public class UserEntity implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String Id;
    private String firstName;
    private String lastName;
    private String address1;
    private String address2;
    private String city;
    private String state;
    private String zipcode;
    private Calendar birthDate;
    private String gender;
    private String email;
    private String verifyEmail;
    private String password;
    private String verifyPassword;
    private String createdBy;
    private String modifiedBy;
    private Calendar createdAt;
    private Calendar modifiedAt;

    public UserEntity() {

    }

    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    @NotNull
    public String getId() {
        return Id;
    }

    public void setId(String id) {
        Id = id;
    }


    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getAddress1() {
        return address1;
    }

    public void setAddress1(String address1) {
        this.address1 = address1;
    }

    public String getAddress2() {
        return address2;
    }

    public void setAddress2(String address2) {
        this.address2 = address2;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getZipcode() {
        return zipcode;
    }

    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }

    public Calendar getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(Calendar birthDate) {
        this.birthDate = birthDate;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @javax.persistence.Id
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getVerifyEmail() {
        return verifyEmail;
    }

    public void setVerifyEmail(String verifyEmail) {
        this.verifyEmail = verifyEmail;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getVerifyPassword() {
        return verifyPassword;
    }

    public void setVerifyPassword(String verifyPassword) {
        this.verifyPassword = verifyPassword;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public String getModifiedBy() {
        return modifiedBy;
    }

    public void setModifiedBy(String modifiedBy) {
        this.modifiedBy = modifiedBy;
    }

    public Calendar getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Calendar createdAt) {
        this.createdAt = createdAt;
    }

    public Calendar getModifiedAt() {
        return modifiedAt;
    }

    public void setModifiedAt(Calendar modifiedAt) {
        this.modifiedAt = modifiedAt;
    }



}

【问题讨论】:

  • 你遇到了什么异常?
  • 另外我想你的 UserEntity 类 id 应该是 Long 以支持 CrudRepository。 link
  • 问题是我没有得到任何异常。它只是请求没有到达控制器,当我执行 localhost:8080 时,我得到一个 Whitelabel 错误页面
  • 乍一看,我没有看到错误。你能显示你的Whitelabel Error Page 异常测试和堆栈跟踪

标签: java spring-mvc spring-boot


【解决方案1】:

您绝对应该阅读以下内容:

http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-static-content

您正在尝试访问一个实际上是静态内容的简单 HTML 页面。 来自 Spring Boot 文档:

默认情况下,Spring Boot 将从类路径中名为 /static(或 /public 或 /resources 或 /META-INF/resources)的目录或 ServletContext 的根目录中提供静态内容。

首先, 这意味着您需要确保 index.html 页面位于 static 文件夹下,Spring boot 中的 static 文件夹位于 src/main/resources 下。

第二, 当您尝试在 Spring Boot 中提供静态内容时,您需要并且想要破坏缓存,这意味着您的浏览器正在缓存您的旧请求,并且为了看到您的更改正在发生,您需要添加将此添加到您的 application.properties 文件中:

spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

也请看这里:

Spring boot cannot load css from user folder

【讨论】:

  • 我没有看到他的包结构有任何问题。他的index.html 在src/main/resources/templates 之下。这是百里香和春天的正常行为。
  • 主要问题不在于您指出的索引文件的位置,为了破坏浏览器缓存,您应该将上述两个语句添加到您的 application.properties 文件中。当然,在 Spring Boot 中,如果索引文件位于适当的位置会有所帮助。请阅读 Spring Boot 文档的链接。
  • 但他正在使用 thymeleaf 作为模板引擎。因此 index.html 的位置在模板下。
  • 您对 thymeleaf 文件位置的看法是正确的,但请阅读此内容以了解如何提供静态内容:docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/…
  • 他需要将这个添加到他的 application.properties 文件中:spring.resources.chain.strategy.content.enabled=true spring.resources.chain.strategy.content.paths=/**跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 2010-11-15
  • 2016-01-08
  • 1970-01-01
相关资源
最近更新 更多