BindingResult既不是bean名称的主要目标对象,也不是所有者'可用作请求属性

时间:2015-12-14 11:35:53

标签: java mysql spring hibernate

我正在尝试使用Spring,Hibernate和MySql将数据插入数据库。我在将数据插入数据库时​​收到以下错误 "无论是BindingResult还是bean名称的主要目标对象'所有者'可用作请求属性" 。我尝试了所有可能的解决方案,但无法清除它。

这是我的控制器

@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;
  }
}

我的观点

<%@ 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>

请帮忙。

提前致谢

1 个答案:

答案 0 :(得分:0)

您的控制器存在一些问题

首先,对于@ModelAttribute类型的方法参数,您不应该使用Model。接下来,您希望在GET请求上调用的方法将在POST请求上调用,因此基本上不会有模型。最后在你应该处理POST请求的方法中,你返回一个ModelAndView但是有一个空模型,所以此时不​​再有模型,也没有Owner对象。)。

我建议以下列方式修复你的控制器

@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";
    }
}