gpt4 book ai didi

java - Spring MVC 表单 - 引用对象的模型

转载 作者:行者123 更新时间:2023-11-30 03:37:06 25 4
gpt4 key购买 nike

我创建了一个表单,其中有 2 个字段(产品名称和价格)和类别对象的下拉列表(产品的类别)。当我在 Product 对象中设置了一个 Category 对象时,我不知道如何进行这项工作。

产品:

public class Product {
private String name;
private Category category;
private int price;

public Product() {
}

public Product(String name, Category category, int price) {
this.name = name;
this.category = category;
this.price = price;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Category getCategory() {
return category;
}

public void setCategory(Category category) {
this.category = category;
}

public int getPrice() {
return price;
}

public void setPrice(int price) {
this.price = price;
}

}

产品 Controller :

    @ModelAttribute("categoryList")
public List<Category> categoryList() {
return categoryService.getCategories();
}

@RequestMapping("/products/add")
public ModelAndView addProductForm() {
ModelAndView mv = new ModelAndView("addProduct");

mv.addObject("product", new Product());
return mv;
}
@RequestMapping(value = "/products/add/process", method = RequestMethod.POST)
public ModelAndView addProduct(@ModelAttribute("product") Product product) {
ModelAndView mv = new ModelAndView("products");
System.out.println("added " + product.getName() + " " + product.getPrice());
return mv;
}

形式:

<form class="form-horizontal" action="#"
th:action="@{/products/add/process}" th:object="${product}"
method="post">
<fieldset>

<!-- Form Name -->
<legend>Add product</legend>

<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Product
name</label>
<div class="col-md-4">
<input id="textinput" name="textinput" placeholder="Product name"
class="form-control input-md" required="" type="text"
th:field="*{name}"></input>
</div>
</div>

<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="selectbasic">Category</label>
<div class="col-md-4">
<select th:field="*{category}">
<option th:each="cat : ${categoryList}" th:value="${cat.getId()}"
th:text="${cat.getName()}"></option>
</select>
</div>
</div>

<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Price</label>
<div class="col-md-4">
<input id="textinput" name="textinput" placeholder=""
class="form-control input-md" required="" type="text"
th:field="*{price}"></input>
</div>
</div>

<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="singlebutton"></label>
<div class="col-md-4">
<button id="singlebutton" name="singlebutton"
class="btn btn-success">Add product</button>
</div>
</div>
</fieldset>
</form>

来自评论的附加信息
当我提交它时(请参阅 addProduct 方法 - 它是一个表单处理程序),我得到: java.lang.IllegalStateException: Cannot conversion value of type [java.lang.String] to required type [com.example.shop.Category]对于属性“类别”:找不到匹配的编辑器或转换策略]。我根本无法将来自下拉列表的字符串转换为对象

最佳答案

问题是Spring没有内置的从StringCategory的转换能力。它知道需要一个 Category 才能使用 ProductsetCategory(Category Category) 方法,但无法转换 String 它从您发布的下拉列表中获取。因此,你需要成为一个亲爱的人,通过告诉 Spring 如何进行转换并定义转换器来帮助 Spring 一些,请参阅 Spring docs了解更多信息。

最简单的选择是使用 Converter SPI:

package com.example.shop.converter;

final class StringToCategoryConverter implements Converter<String, Category> {
public Category convert(String source) {
Category category;

// Put your conversion logic here

return category;
}
}

就您而言,我猜您会想要使用:CategoryService.getCategory(int id) 或类似的方法。

然后您需要配置 Spring 来实际使用您的转换器,下面是一个说明如何执行此操作的 XML 示例:

<mvc:annotation-driven conversion-service="conversionService" />

<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.example.shop.converter.StringToCategoryConverter" />
</list>
</property>
</bean>

关于java - Spring MVC 表单 - 引用对象的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27607106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com