在spring mvc表单中保留post中的对象
我正在使用SpringMVC。我有一个预构建的对象,作为ModelAttribute传递给视图。此对象用于显示窗体内的某些信息。然后,在填充表单后,通过表单中的path属性设置前一个对象的属性:select标记。当我检索这个对象时,我只看到用post更新的属性,并且该对象之前设置的所有属性现在都具有null值。我想知道我必须做什么才能不丢失对象的这些属性,这样我才能访问它们 这就是目标:在spring mvc表单中保留post中的对象,spring,spring-mvc,Spring,Spring Mvc,我正在使用SpringMVC。我有一个预构建的对象,作为ModelAttribute传递给视图。此对象用于显示窗体内的某些信息。然后,在填充表单后,通过表单中的path属性设置前一个对象的属性:select标记。当我检索这个对象时,我只看到用post更新的属性,并且该对象之前设置的所有属性现在都具有null值。我想知道我必须做什么才能不丢失对象的这些属性,这样我才能访问它们 这就是目标: public class VocabularyRegisterDto { private Lis
public class VocabularyRegisterDto {
private List<String> availableVocabularies;
private String vocabularyToRegister;
... constructors setters and getters ...
表单视图
<form:form action="vocabulary/register" method="post" modelAttribute="vocabularyRegister">
<form:select path="vocabularyToRegister" id="register-vocabulary-dropdown">
<c:forEach items="${vocabularyRegister.availableVocabularies}" var="vocabulary">
<form:option value="${vocabulary}"/>
</c:forEach>
</form:select>
<input id="register-vocabulary-button" type="submit" value="Submit"/>
</form:form>
在post控制器之后执行的方法
@RequestMapping(value = "/vocabulary/register", method = RequestMethod.POST)
public String registerVocabulary(@ModelAttribute VocabularyRegisterDto vocabularyRegisterDto,
HttpServletRequest request) {
String vocabularyRegister = vocabularyRegisterDto.getVocabularyToRegister(); // Here I do get the option selected in the form
List<String> vocabularyRegister = vocabularyRegisterDto.availableVocabularies(); // But here I get null even though this list was already used in the view to display all the options
return "redirect:/profile";
}
@RequestMapping(value=“/词汇表/register”,method=RequestMethod.POST)
公共字符串注册表可撤销性(@ModelAttribute VocabularyRegisterTo VocabularyRegisterTo,
HttpServletRequest(请求){
String-vocabularyRegister=vocabularyRegisterDto.getVocabularyToRegister();//这里我得到了表单中选择的选项
List-vocabularyRegister=vocabularyRegisterDto.availableVocabularies();//但是这里我得到了null,即使该列表已经在视图中用于显示所有选项
返回“重定向:/profile”;
}
@RequestMapping(value = "/vocabulary/register", method = RequestMethod.POST)
public String registerVocabulary(@ModelAttribute VocabularyRegisterDto vocabularyRegisterDto,
HttpServletRequest request) {
String vocabularyRegister = vocabularyRegisterDto.getVocabularyToRegister(); // Here I do get the option selected in the form
List<String> vocabularyRegister = vocabularyRegisterDto.availableVocabularies(); // But here I get null even though this list was already used in the view to display all the options
return "redirect:/profile";
}