【问题标题】:Neither BindingResult nor plain target object for bean name 'owner' available as request attributeBean 名称“所有者”的 BindingResult 和普通目标对象都不能用作请求属性
【发布时间】:2015-12-14 11:35:53
【问题描述】:

我正在尝试使用 Spring、Hibernate 和 MySql 将数据插入数据库。将数据插入数据库时​​出现以下错误 “Bean 名称‘所有者’的 BindingResult 和普通目标对象都不能用作请求属性”。我尝试了所有可能的解决方案,但无法清除它。

这是我的控制器

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

请帮忙。

提前致谢

【问题讨论】:

  • 您返回的是ModelAndView,但模型为空。返回 String 而不是 ModelAndView 或正确填充模型。
  • 仍然出现同样的错误.. 你能帮帮我吗
  • 您的 @ModelAttribute 方法参数上有一个 @ModelAttribute,请将其删除。我也怀疑这是您的实际控制器,您将如何在 POST 方法上填充模型?恕我直言,应该是 GET,提交是 POST。
  • 我已经删除了@ModelAttribute。我应该将其更改为 GET 方法。
  • 目前该方法将在提交时调用,而不是在您期望它被调用时调用。

标签: java mysql spring hibernate


【解决方案1】:

你的控制器有一些问题

首先,您不应该在 Model 类型的方法参数上使用 @ModelAttribute。接下来,您希望在 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";
    }
}

【讨论】:

  • 我照你说的做了。但我收到以下错误“发现不明确的映射。无法映射'dataOwnerRegController' bean 方法”
  • 那你没有像我写的那样做(或者保留你自己的旧控制器并创建一个新的)......
  • 仍然收到相同的错误 Bean 名称“所有者”的 BindingResult 和普通目标对象都不能用作请求属性
  • 我照你说的做了,但它给我带来了同样的旧错误 BindingResult 和 bean name 'owner' 的普通目标对象都不能用作请求属性
  • 那你真的在做你不应该做的事情。您不是通过任何更改直接访问 JSP 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-23
  • 2015-08-31
  • 2013-04-04
  • 2011-11-25
  • 2019-10-29
  • 2013-08-15
相关资源
最近更新 更多