[英]javax.servlet.ServletException: Circular view path [products]
I am new in Java Spring 4.0. 我是Java Spring 4.0的新手。 Here is my project set up.
这是我的项目设置。
ProductController ProductController的
@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";
}
@RequestMapping("product/{id}")
public String getProduct(@PathVariable Integer id, Model model){
model.addAttribute("product", productService.getProductById(id));
return "product";
}
@RequestMapping("product/edit/{id}")
public String edit(@PathVariable Integer id, Model model){
model.addAttribute("product",productService.getProductById(id));
return "productform";
}
@RequestMapping("/product/new")
public String newProduct(Model model){
model.addAttribute("product",new Product());
return "productform";
}
@RequestMapping(value = "/product", method = RequestMethod.POST)
public String saveOrUpdateProduct(Product product){
Product saveProduct = productService.saveOrUpdateProduct(product);
return "redirect:/product/" + saveProduct.getId();
}
@RequestMapping("/product/delete/{id}")
public String delete(@PathVariable Integer id){
productService.deleteProduct(id);
return "redirect:/products";
}
}
ProductServiceImpl ProductServiceImpl
@Service
public class ProductServiceImpl implements ProductService {
Map<Integer, Product> products;
public ProductServiceImpl(){
loadProducts();
}
@Override
public List<Product> listAllProducts() {
return new ArrayList<>(products.values());
}
@Override
public Product getProductById(Integer id) {
return products.get(id);
}
@Override
public Product saveOrUpdateProduct(Product product) {
if (product != null){
if (product.getId() == null){
product.setId(getNextKey());
}
products.put(product.getId(), product);
return product;
} else {
throw new RuntimeException("Product Can't be nill");
}
}
@Override
public void deleteProduct(Integer id) {
products.remove(id);
}
private Integer getNextKey(){
return Collections.max(products.keySet()) + 1;
}
private void loadProducts(){
products = new HashMap<>();
Product product1 = new Product();
product1.setId(1);
product1.setDescription("Product 1");
product1.setPrice(new BigDecimal("12.99"));
product1.setImageUrl("http://example.com/product1");
products.put(1,product1);
Product product2 = new Product();
product2.setId(2);
product2.setDescription("Product 2");
product2.setPrice(new BigDecimal("14.99"));
product2.setImageUrl("http://example.com/product2");
products.put(2, product2);
Product product3 = new Product();
product3.setId(3);
product3.setDescription("Product 3");
product3.setPrice(new BigDecimal("34.99"));
product3.setImageUrl("http://example.com/product3");
products.put(3, product3);
Product product4 = new Product();
product4.setId(4);
product4.setDescription("Product 4");
product4.setPrice(new BigDecimal("44.99"));
product4.setImageUrl("http://example.com/product4");
products.put(4, product4);
Product product5 = new Product();
product5.setId(5);
product5.setDescription("Product 5");
product5.setPrice(new BigDecimal("25.99"));
product5.setImageUrl("http://example.com/product5");
products.put(5, product5);
}
}
So, I am injecting a CRUD service to the controller and works good. 因此,我正在向控制器注入CRUD服务,并且运行良好。 I can post, delete, display and update data.
我可以发布,删除,显示和更新数据。 Now I want to make mockito test.
现在我要进行模拟测试。 So.
所以。
ProductControllerTest ProductControllerTest
public class ProductControllerTest {
@Mock
private ProductService productService;
@InjectMocks
private ProductController productController;
private MockMvc mockMvc;
@Before
public void setup(){
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(productController).build();
}
@Test
public void testList() throws Exception{
List<Product> products = new ArrayList<>();
products.add(new Product());
products.add(new Product());
when(productService.listAllProducts()).thenReturn((List)products);
mockMvc.perform(get("/products"))
.andExpect(status().isOk())
.andExpect(view().name("products"))
.andExpect(model().attribute("products",hasSize(2)));
}
}
Unfortunately, the test doesn't pass. 不幸的是,测试没有通过。
javax.servlet.ServletException: Circular view path [products]: would dispatch back to the current handler URL [/products] again.
javax.servlet.ServletException:循环视图路径[products]:将再次分派回当前的处理程序URL [/ products]。 Check your ViewResolver setup!
检查您的ViewResolver设置! (Hint: This may be the result of an unspecified view, due to default view name generation.)
(提示:由于默认视图名称的生成,这可能是未指定视图的结果。)
Why is this happening? 为什么会这样呢? I mean I can display the products in my browswer, but the unit test fails.
我的意思是我可以在浏览器中显示产品,但是单元测试失败。
I am using spring boot and the thymeleaf template. 我正在使用弹簧靴和百里香模板。
pom.xml 的pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>theo.tziomakas</groupId>
<artifactId>springmvc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springmvc</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.5</version>
</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>
</project>
Please try to add @ResponseBody annotation on methods of ProductController with @RequestMapping as shown below. 请尝试使用@RequestMapping在ProductController的方法上添加@ResponseBody批注,如下所示。
@RequestMapping("/products")
@ResponseBody
public String listProducts(Model model){
model.addAttribute("products",productService.listAllProducts());
return "products";
}
Hope it would help. 希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.