当前代码
我只是假设您正在使用基于您的 html 的以下类,所以让我们以它们为起点。
/* This may be called City in your code */
public class Item {
private Long id;
private String description;
// getter/setter/constructor here...
}
public class ContactForm {
private Long cityId;
// getter/setter/constructor here...
}
现在我们可以做些什么来解决您的问题?
Spring 提供了一种为特定类添加转换器的简单方法,因此您不必担心将 String 转换为 Year 对象。同样的技术也可以用于其他类,比如你的 Item 类!让我们研究一下:
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
@Component
public class StringToItemConverter implements Converter<String, Item> {
@Override
public Item convert(String source) {
if(source != null) {
try {
Long id = Long.parseLong(source);
return /* insert your logic for getting an item by it's id here */;
} catch(Exception e) {
return null;
}
}
return null;
}
}
当 Spring 尝试将 String(来自您的表单输入)转换为类型为 Item 的实际 java 字段时,Spring 会自动执行上述代码。所以如果我们稍微改变一下你的 ContactForm 类,spring 会自动为给定的 id 分配 Item 对象。
public class ContactForm {
/* Note that cityId now has all the information you need (id and description) You should however consider renaming it to city. Don't forget to change the th:field name too! ;) */
private Item cityId;
// getter/setter/constructor here...
}
您正在使用 Spring 存储库吗?
如果您将项目存储在数据库中,您很可能会为此使用 CrudRepository。在这种情况下,代码可能如下所示。假设您的 Repository 类名为 ItemRepository。
@Component
public class StringToItemConverter implements Converter<String, Item> {
private ItemRepository itemRepository;
@Autowired
public void setItemRepository (ItemRepository itemRepository) {
this.itemRepository = itemRepository;
}
@Override
public Item convert(String source) {
if(source != null) {
try {
Long id = Long.parseLong(source);
return itemRepository.findOne(id);
} catch(Exception e) {
return null;
}
}
return null;
}
}
上面的代码会尝试通过在数据库中查找 id 来将表单中的字符串转换为实际的Item-Object。所以我们要么得到Item,要么得到null,如果出现任何问题(例如,如果没有该id的项目或者字符串不能被解析那么长)。