2016-04-18 8 views
13

मैं वसंत के लिए बहुत नया हूं। मैं स्प्रिंग बूट का उपयोग कर एक एमवीसी अनुप्रयोग बनाने की कोशिश कर रहा हूं जो उत्पादों की एक सूची दिखाता है। लेकिन मैं नीचे त्रुटि हो रही है:परिपत्र दृश्य पथ त्रुटि वसंत बूट

javax.servlet.ServletException: Circular view path [products]: would dispatch back to the current handler URL [/products] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

यहाँ नियंत्रक है:

package com.springframeworkguru.controllers; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 

import com.springframeworkguru.services.ProductService; 


    @Controller 
    public class ProductController { 

     private ProductService productService; 

     @Autowired 
     public void setProductService(ProductService productService) { 
      this.productService = productService; 
     } 

     @RequestMapping("/products") 
     public String listProducts(Model model){ 

      model.addAttribute("products", productService.listAllProducts()); 

      return "products"; 
     } 

    } 

यह मुख्य वर्ग है:

package com.springframeworkguru; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.builder.SpringApplicationBuilder; 
import org.springframework.boot.context.web.SpringBootServletInitializer; 

import com.springframeworkguru.controllers.ProductController; 

@SpringBootApplication 
public class SpringmvcApplication extends SpringBootServletInitializer{ 

    public static void main(String[] args) { 
     SpringApplication.run(SpringmvcApplication.class, args); 
    } 
} 

और products.html:

<!DOCTYPE html> 
<html lang="en" xmlns:th="http://www.thymeleaf.org"> 
<head> 
    <title>Spring Core Online Tutorial - List Products</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 

    <link href="http://cdn.jsdelivr.net/webjars/bootstrap/3.3.4/css/bootstrap.min.css" 
      th:href="@{/webjars/bootstrap/3.3.5/css/bootstrap.min.css}" 
      rel="stylesheet" media="screen"/> 

    <script src="http://cdn.jsdelivr.net/webjars/jquery/2.1.4/jquery.min.js" 
      th:src="@{/webjars/jquery/2.1.4/jquery.min.js}"></script> 

    <link href="../css/spring-core.css" 
      th:href="@{css/spring-core.css}" rel="stylesheet" media="screen"/> 
</head> 
<body> 
<div class="container"> 
    <div th:if="${not #lists.isEmpty(products)}"> 
     <h2>Product List</h2> 
     <table class="table table-striped"> 
      <tr> 
       <th>Id</th> 
       <th>Description</th> 
       <th>Price</th> 
       <th>Image URL</th> 
       <th>List</th> 
      </tr> 
      <tr th:each="product : ${products}"> 
       <td th:text="${product.id}"></td> 
       <td th:text="${product.description}"></td> 
       <td th:text="${product.price}"></td> 
       <td th:text="${product.imageUrl}"></td> 
       <td><a th:href="${'/product/' + product.id}">View</a> </td> 
      </tr> 
     </table> 
    </div> 
</div> 

</body> 
</html> 

products.html/static फ़ोल्डर में है। इसके अलावा, मैं ग्रहण केप्लर का उपयोग कर रहा हूं।

उत्तर

10

The products.html is /static folder

डिफ़ॉल्ट रूप से, स्प्रिंग बूट classpath पर templates निर्देशिका में Thymeleaf टेम्पलेट्स के लिए दिखेगा। तो अपने products.html से src/main/resources/templates निर्देशिका को ले जाएं। आप Spring Boot Documentation पर टेम्पलेट इंजन और स्प्रिंग बूट के बारे में अधिक पढ़ सकते हैं:

When you’re using thymeleaf templating engine with the default configuration, your templates will be picked up automatically from src/main/resources/templates

इसके अलावा, static निर्देशिका है जहाँ आप अपने स्टेटिक सामग्री, नहीं अपने टेम्पलेट रखना चाहिए।

+0

मैं कि किया है निम्नलिखित निर्भरता जोड़ें। लेकिन एक ही त्रुटि - javax.servlet.ServletException: परिपत्र दृश्य पथ [उत्पाद]: वर्तमान हैंडलर यूआरएल [/ उत्पादों] पर फिर से भेज देगा। अपने ViewResolver सेटअप की जांच करें! (संकेत: डिफ़ॉल्ट दृश्य नाम जनरेशन के कारण, यह अनिर्दिष्ट दृश्य का परिणाम हो सकता है।)। 2016-04-19 00: 01: 35.129 त्रुटि 3200 --- [nio-8080-exec-5] oaccc [। [। [/]। [DispatcherServlet]: Servlet.service() servlet dispatcher के लिए सर्वलेट अपवाद फेंक दिया – sohan

+13

क्या आपने 'स्प्रिंग-बूट-स्टार्टर-थाइमेलीफ' निर्भरता जोड़ें? –

+1

बहुत बहुत धन्यवाद। यह वसंत-बूट स्टार्टर-थाइमेलीफ, वसंत-बूट-स्टार्टर-वेब निर्भरता जोड़ने के बाद काम करता था। – sohan

37

spring-boot-starter-thymeleaf निर्भरता जोड़ने से समस्या हल हो गई।

तो अपने pom.xml फ़ाइल से जोड़ें:

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

में pom.xml

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf --> 
<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-thymeleaf</artifactId> 
    <version>1.4.0.RELEASE</version> 
</dependency>