我有一个Spring 4 Simple WebApp,由于Ajax get调用,我将页面(.jsp)附加到div中。该页面包含将ModelAttribute定义为显示页面的控制器的形式。例如:
index.jsp
<html>
<jsp:forward page="home.html"/>
</html>
HomeController.java
@RequestMapping("/home")
public ModelAndView showUsers(@ModelAttribute("User")User u) {
ModelAndView model = new ModelAndView();
model.setViewName("/home/home");
return model;
}
home.jsp
<html>
<head>
<script type="text/javascript">
function getData() {
$.ajax({
type: "GET",
url: "searchUser.html",
success: function (result) {
$('#form').append(result);
},
error: function (result) {
}
});
}
}
</script>
<title></title>
</head>
<body>
<a href="javascript:getData()">click me</a>
<div class="container">
<div id="form" />
</div>
</body>
</html>
UserController.java
@RequestMapping("/searchUser")
public String searchUser(Model model) {
List<User> uList = userDao.getAllUsers();
model.addAttribute("u", uList);
return "formU";
}
formU.jsp
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<form:form method="post" action="modUser.html" modelAttribute="User">
<form:select path="idUser" items="${u}" itemLabel="name" itemValue="idUser"/>
</form:form>
结果是:
GRAVE: Servlet.service() for servlet spring threw exception
java.lang.IllegalStateException: Neither BindingResult nor plain target
object for bean name 'Categoria' available as request attribute
如果我在div内对表单进行硬编码,则页面将正确加载。你知道为什么吗?我能解决这个问题吗? 谢谢大家