我根据以下内容在MANY Spring-MVC控制器中使用以下自定义编辑器:
一个控制器
binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, NumberFormat.getNumberInstance(new Locale("pt", "BR"), true));
Run Code Online (Sandbox Code Playgroud)
其他控制器
binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, NumberFormat.getNumberInstance(new Locale("pt", "BR"), true));
Run Code Online (Sandbox Code Playgroud)
另一个控制器
binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, NumberFormat.getNumberInstance(new Locale("pt", "BR"), true));
Run Code Online (Sandbox Code Playgroud)
注意注册了相同的自定义编辑器
问题:如何设置像这样的全局自定义编辑器以避免设置每个控制器?
问候,
我有这样的意见
<input type="date" name="date">
Run Code Online (Sandbox Code Playgroud)
如何从这个输入读取类java.util.Date的java对象?
PS Date date是我的Bean的一个字段,我这样读过:
@RequestMapping("/updateVacancy")
public String updateVacancy(@ModelAttribute("vacancy") Vacancy vacancy){
vacancyService.update(vacancy);
return "VacancyDetails";
}
Run Code Online (Sandbox Code Playgroud) 我在这里阅读了 Spring Data Binder 的全面解释:/sf/answers/259409881/
我的问题需要更多的知识,让我强调一下:
我有具有复杂字段的 Person 对象 - 例如与街道和数字字段的联系。所以我们可以这样描述:
public class Contact {
private String street;
private int number;
// setters & getters
};
public class Person {
private String name;
private int age;
private Contact contact;
// setters & getters
};
Run Code Online (Sandbox Code Playgroud)
我认识到 spring 将其表示为 html 表单,如下输入字段:
姓名、年龄、contact.street、contact.number
我认为,当它返回时,它可以将它放回 Person 对象。
我想“雇用”Spring Binder 来为我提供的文本数据创建对象(不是 HttpRequest 对象)。例如我有文件:
name age street number
John 22 MHDrive 2187
Will 32 MHDrive 3212
Run Code Online (Sandbox Code Playgroud)
我可以提供上面的任何结构并想要获取 Person 对象:)
例如
Person p = springBinderSomething({{"name","John"},{"age","22"},{"street","MHDrive"},{"number","2187"}}, Person.class); …Run Code Online (Sandbox Code Playgroud) 这里我使用的是Java 1.8的LocalDate类,在我的bean中,我将返回类型设为LocalDate。而且我要寄约会甲:07/01/2017。当我尝试保存时,出现了以下异常。
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] (default task-33) Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not instantiate value of type [simple type, class java.time.LocalDate] from String value ('07/01/2017'); no single-String constructor/factory method
at [Source: java.io.PushbackInputStream@39959f38; line: 1, column: 650] (through reference chain: com.pro.bean.ParentBean["Soici"]->com.pro.bean.Soici["fecha_de_solicitud"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class java.time.LocalDate] from String value ('07/01/2017'); no single-String constructor/factory method
at [Source: java.io.PushbackInputStream@39959f38; line: 1, column: 650] (through reference chain: com.pro.bean.ParentBean["Soici"]->com.pro.bean.Soici["fecha_de_solici"])
Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我需要一个文本字段(MNC ID),它是int类型,为空,而它在表单中显示为一个空格,但它的值为"0"作为值.我怎么能这样做?我正在使用,但因为我发现我没有形式的值标签:输入spring标签,我如何在表单中包含一个值标签:输入标签,以便我可以留空""?请帮助我这方面