2017-04-10 186 views
0

^h所有,春天開機:MVC沒有返回視圖:thymeleaf&春天數據

我試圖做一個簡單的MVC春季啓動應用程序,我有它在我的代碼當控制器爲返回的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); 


    } 

} 

HealthCHeckController.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

enter image description here

的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; 
    } 



} 
+0

你有什麼異常? –

+0

另外我想你的'UserEntity'類'id'應該是'Long'以支持CrudRepository。 [鏈接](HTTP://文檔。spring.io/spring-data/commons/docs/1.5.0.RELEASE/reference/html/repositories.html) –

+0

事情是,我沒有得到任何異常。它的請求沒有到達控制器,當我在第一次看到http:// localhost:8080 – user641887

回答

1

你一定要這樣說的:

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

您正在試圖打一個簡單的HTML頁面,這實際上是一個靜態的內容。 從春天啓動文件:

默認春天啓動的任期將從一個名爲/靜態目錄(或/公共或/資源或/ META-INF /資源)在類路徑或從ServletContext的根靜態內容。

首先, 這意味着你需要確保index.html頁面是靜態的文件夾下,位於在src /主/資源春季啓動靜態文件夾。

, 當試圖服務於春季啓動靜態內容,你需要和希望破滅的高速緩存,這意味着您的瀏覽器緩存您的舊的請求,併爲了看到你的變化正在發生,你需要把它添加到您的application.properties文件:

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

看看這裏也:

Spring boot cannot load css from user folder

+0

我沒有看到他的包結構的任何問題。他的'index.html'在'src/main/resources/templates'下。這就是百里香和春天的正常行爲。 – Patrick

+0

主要問題不在於您指出的索引文件的位置,爲了破壞瀏覽器緩存,您應該將上述兩個語句添加到您的application.properties文件中。當然,在春季啓動時,如果索引文件位於適當的位置,這將有所幫助。請閱讀spring引導文檔的鏈接。 –

+0

但他使用百里香作爲模板引擎。因此index.html的位置在模板下。 – Patrick