Java Spring MVC无法使用下拉菜单传递表单数据
我正在玩Thymeleaf,并创建了一个下拉菜单,上面有人们的名字。我的控制器类表示单个用户,该用户还具有与该用户相同的同级名称。 在制作表单时,在Thymeleaf中,Java Spring MVC无法使用下拉菜单传递表单数据,java,spring,spring-mvc,spring-boot,thymeleaf,Java,Spring,Spring Mvc,Spring Boot,Thymeleaf,我正在玩Thymeleaf,并创建了一个下拉菜单,上面有人们的名字。我的控制器类表示单个用户,该用户还具有与该用户相同的同级名称。 在制作表单时,在Thymeleaf中,th:field=“*{sibling}”导致了我的问题,我无法解决此问题。 我已经看了这么多链接,例如,但是这并没有帮助我解决我的问题。 下面是错误的堆栈跟踪: java.lang.IllegalStateException: Neither BindingResult nor plain target object for
th:field=“*{sibling}”
导致了我的问题,我无法解决此问题。
我已经看了这么多链接,例如,但是这并没有帮助我解决我的问题。
下面是错误的堆栈跟踪:
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.thymeleaf.spring4.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:401) ~[thymeleaf-spring4-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:328) ~[thymeleaf-spring4-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:294) ~[thymeleaf-spring4-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at
........
我的控制器如下所示:
@Controller
public class UserController {
@Autowired
private UserService userService;
Set<String> allSiblings = new HashSet<>();
@ModelAttribute("user")
public Recipe defaultInstance() {
User user = new User();
return user;
}
@GetMapping("/")
public String index(Model model){
List<User> allUsers = userService.findAll();
createDropDown();
model.addAttribute("users", allUsers);
model.addAttribute("allSiblings", this.allSiblings);
return "index";
}
@GetMapping("/search")
public String search(@Valid @ModelAttribute("user") User user, BindingResult bindingResult){
if(bindingResult.hasErrors()){
System.out.println("There was a error "+bindingResult);
return "index";
}
System.out.println(user.getSibling());
return "result";
}
private void createDropDown(){
allSiblings.add("john");
allSiblings.add("paul");
}
@控制器
公共类用户控制器{
@自动连线
私人用户服务;
Set allsides=new HashSet();
@ModelAttribute(“用户”)
公共配方defaultInstance(){
用户=新用户();
返回用户;
}
@GetMapping(“/”)
公共字符串索引(模型){
List alluser=userService.findAll();
createDropDown();
model.addAttribute(“用户”,诱惑者);
model.addAttribute(“allsibles”,this.allsibles);
返回“索引”;
}
@GetMapping(“/search”)
公共字符串搜索(@Valid@modeldattribute(“user”)用户,BindingResult BindingResult){
if(bindingResult.hasErrors()){
System.out.println(“出现错误”+bindingResult);
返回“索引”;
}
System.out.println(user.getSibling());
返回“结果”;
}
私有void createDropDown(){
所有兄弟姐妹。添加(“约翰”);
所有兄弟姐妹。添加(“保罗”);
}
最后,我有一个index.html页面,该页面具有以下thymeleaf下拉表单:
<form style="display:inline-block" th:action="@{/search}" th:object="${user}">
<select th:field="*{sibling}">
<option value=""> --</option>
<option th:each="ing : ${allSiblings}"
th:value="${ing}"
th:utext="${ing}">
</option>
</select>
<button type="submit">submit</button>
--
提交
您需要将
user
对象添加到控制器中的模型中。您正在HTML页面上使用th:object=“${user}”
引用它。我可能重复了您的意思,并将user对象添加到控制器中的模型中-成功了!谢谢
<form style="display:inline-block" th:action="@{/search}" th:object="${user}">
<select th:field="*{sibling}">
<option value=""> --</option>
<option th:each="ing : ${allSiblings}"
th:value="${ing}"
th:utext="${ing}">
</option>
</select>
<button type="submit">submit</button>