[英]Neither BindingResult nor plain target object for bean name 'owner' available as request attribute
I am trying to insert data's into database using Spring,Hibernate and MySql. 我正在尝试使用Spring,Hibernate和MySql将数据插入数据库。 I am getting the following error while inserting data into database "Neither BindingResult nor plain target object for bean name 'owner' available as request attribute" .
在将数据插入数据库“ Bean名称'owner'的BindingResult或普通目标对象都不能用作请求属性”时,出现以下错误。 I tried all possible solutions available but couldn't clear it.
我尝试了所有可能的解决方案,但无法清除。
This is my controller 这是我的控制器
@Controller
public class DataOwnerRegController {
@Autowired(required = true)
DataService dataService;
@RequestMapping(value="/DataOwnerReg",method = RequestMethod.POST)
public String getForm(@ModelAttribute("owner") Model model )
{
model.addAttribute("owner", new Owner());
return "DataOwnerReg";
}
@RequestMapping(value = "/DataOwnerReg")
public ModelAndView registeruser(@ModelAttribute("owner") Owner owner, BindingResult result){
ModelAndView modelAndView = new ModelAndView("DataOwnerReg");
dataService.insertRow(owner);
return modelAndView;
}
}
My View 我的观点
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>DATA OWNER REGISTRATION</title>
<link href="<c:url value="/resources/theme/css/bootstrap.css"/>" rel="stylesheet" >
<link href="<c:url value="/resources/theme/css/style.css"/>" rel="stylesheet">
<script src="<c:url value="/resources/theme/js/jquery-2.1.4.min.js"/>"> </script>
<script src="<c:url value="/resources/theme/js/bootstrap.mi.js"/>"></script>
<link href='https://fonts.googleapis.com/css?family=Raleway:300' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Bangers' rel='stylesheet' type='text/css'>
</head>
<body class="body-image">
<div>
<nav style="background-color:transparent;border:none" class="navbar navbar-default">
<div class="container">
<ul class="nav navbar-nav">
<li><a style="font-family: Raleway,sans-serif;color:white;font-size:15px;font-weight:600" href="<c:url value="/"/>">HOME</a></li>
<li><a style="font-family: Raleway,sans-serif;color:white;font-size:15px;font-weight:600" href="<c:url value="/DataOwner"/>">DATAOWNER</a></li>
<li><a style="font-family: Raleway,sans-serif;color:white;font-size:15px;font-weight:600" href="<c:url value="/DataOwner"/>">DATA CENTER</a></li>
<li><a style="font-family: Raleway,sans-serif;color:white;font-size:15px;font-weight:600" href="<c:url value="/DataOwner"/>">KEY DISTRIBUTION</a></li>
<li><a style="font-family: Raleway,sans-serif;color:white;font-size:15px;font-weight:600" href="<c:url value="/DataOwner"/>">ADMINISTRATOR</a> </li>
</ul>
</div>
</nav>
</div>
<div class="container">
<div class="border">
<form:form modelAttribute="owner" method="Post" action="/DataOwnerReg">
<h3 style="text-align:center">Data Owner Registration</h3>
<h2 style="text-align:center">${message}</h2>
<hr style="width:25%"/>
<div class="formation">
<h4>Username:</h4>
<div class="input-group">
<form:input type="text" class="form-control" style="font-family:Raleway,sans-serif;height:40px" placeholder="Enter the Username" required="" path="Username" /><div class="input-group-addon"><span class="glyphicon glyphicon-user" aria-hidden="true"></span></div>
</div>
<h4>Password:</h4>
<div class="input-group">
<form:input type="password" class="form-control" style="font-family:Raleway,sans-serif;height:40px" placeholder="Enter the Password" required="" path="Password"/><div class="input-group-addon"><span class="glyphicon glyphicon-lock" aria-hidden="true"></span></div>
</div>
<h4>Confirm Password:</h4>
<div class="input-group">
<form:input type="password" class="form-control" style="font-family:Raleway,sans-serif;height:40px" placeholder="Repeat Password" required="" path="CPassword" /><div class="input-group-addon"><span class="glyphicon glyphicon-lock" aria-hidden="true"></span></div>
</div>
<h4>Email Address:</h4>
<div class="input-group">
<form:input type="email" class="form-control" style="font-family:Raleway,sans-serif;height:40px" placeholder="me@example.com" required="" path="Email" /><div class="input-group-addon"><span class="glyphicon glyphicon-envelope" aria-hidden="true"></span></div>
</div>
<h4>Mobile Number:</h4>
<div class="input-group">
<form:input type="text" class="form-control" style="font-family:Raleway,sans-serif;height:40px" placeholder="10 Digit Mobile Number" required="" path="Email" /><div class="input-group-addon"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></div>
</div>
<div>
<input type="submit" style="width:150px;height:45px;margin-top:30px;font-family:Raleway,sans-serif" class="btn btn-success" value="Register"/>
</div>
<br/>
</div>
</form:form>
</div>
</div>
</body>
</html>
Please Help in this. 请帮忙。
Thanks in advance 提前致谢
There are a couple things wrong with your controller 您的控制器有几处错误
First you have the @ModelAttribute
on a method argument of the type Model
which you shouldn't do. 首先,在不应该使用的
Model
类型的方法参数上具有@ModelAttribute
。 Next the method you expect to be invoked on a GET request is going to be invoked on a POST request so basically there will be no model. 接下来,您希望在GET请求上调用的方法将在POST请求上调用,因此基本上没有模型。 Finally in your method that should handle the POST request you are returning a
ModelAndView
but with an empty model, so at that point there is no model anymore and no Owner
object.). 最后,在应该处理POST请求的方法中,您返回的是
ModelAndView
但模型为空,因此,此时不再有模型,也没有Owner
对象。)
I would suggest to fix your controller in the following way 我建议以以下方式修复您的控制器
@Controller
@RequestMapping("/DataOwnerReg")
public class DataOwnerRegController {
private final DataService dataService;
@Autowired
public DataOwnerRegController(DataService DataService) {
this.dataService=dataService;
}
@ModelAttribute
public Owner owner() {
return new Owner();
}
@RequestMapping(method = RequestMethod.GET)
public String get() {
return "DataOwnerReg";
}
@RequestMapping(method = RequestMethod.POST)
public String registeruser(@ModelAttribute("owner") Owner owner, BindingResult result){
dataService.insertRow(owner);
return "DataOwnerReg";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.