//输入异常报告
message java.lang.IllegalStateException:BindingResult和bean名称'test'的普通目标对象都不可用作请求属性
说明服务器遇到内部错误,导致无法完成此请求。
HomeController.java
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/** Handles requests for the application home page. */
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/** Simply selects the home view to render by returning its name. */
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Model model, Locale locale) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate);
return "home";
}
}
HomeModel.java
package com.test.app;
public class HomeModel {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
HometController.java
package com.test.app;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HometController {
@RequestMapping(value = "/try", method = RequestMethod.POST)
public String trynew(@ModelAttribute("test") HomeModel hm, BindingResult br, Model model) {
model.addAttribute("test", new HomeModel());
System.out.println("in try");
return "tst";
}
}
home.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ page session="false" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
Hello world!
</h1>
<P> The time on the server is ${serverTime}. </P>
</body>
<form:form method="POST" modelAttribute="test" name="test" action="try">
<form:input path="name"/>
<input type="submit" value="test"/>
</form:form>
<!-- onClick="document.test.submit()" -->
</html>
tst.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hello World with Spring 3 MVC</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
</head>
<body>
<h1>Registration Form</h1><br />
<form:form commandName="USER">
<table>
<form:errors path="*" cssStyle="color : red;"/>
<tr><td>Name : </td><td><form:input path="name" /></td></tr>
<tr><td colspan="2"><input type="submit" value="Save Changes" /></td></tr>
</table>
</form:form>
</body>
</html>
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'test' available as request attribute
答案 0 :(得分:0)
删除此参数@ModelAttribute("test") HomeModel hm
以消除您的异常
@Controller
public class HometController {
@RequestMapping(value = "/try", method = RequestMethod.POST)
public String trynew( BindingResult br, Model model) {
model.addAttribute("test", new HomeModel());
System.out.println("in try");
return "tst";
}
}