【问题标题】:java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attributejava.lang.IllegalStateException:Bean 名称“用户”的 BindingResult 和普通目标对象都不能用作请求属性
【发布时间】:2016-02-16 04:46:10
【问题描述】:

我正在尝试通过 Spring Form 从 Bootstrap Modal 发送请求,只要正确输入了当前密码,用户就可以更改密码。我在理解如何处理这件事的后勤方面有点麻烦,目前我有一个表格:

<!-- Modal -->
<form:form modalAttribute="user" action="changepass" method="POST">
 <div id="changePassModal" class="modal fade modal-password" role="dialog">
  <div class="modal-dialog modal-sm">
<!-- Modal content-->
  <div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal">&times;</button>
    <h3 class="modal-title"><b>Change Password </b></h3>
  </div>
    <table>
        <tr>
            <td>Current Password :</td>
            <td><form:password path="currentpassword" />
            </td>
            <td><form:errors path="currentpassword" />
            </td>
        </tr>
        <tr>
            <td>Password :</td>
            <td><form:password path="newpassword" />
            </td>
            <td><form:errors path="newpassword"/>
            </td>
        </tr>
        <tr>
            <td>Confirm Password :</td>
            <td><form:password path="newpasswordconfirmation" />
            </td>
            <td><form:errors path="newpasswordconfirmation"/>
            </td>
        </tr>
    </table>
  <div class="modal-footer">
   <button  class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
   <input type="submit" class="btn btn-primary" value="Save Changes" />
</div>
</div>
</div>
</div>
</form:form> 

编辑:添加主控制器类

package controller;
import java.security.Principal;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import model.User;

@Controller
public class MainController {   

/** Constructors **/
@ModelAttribute("user")
public User constructUser(){
    return new User();
}

/** Mapping URI **/

@RequestMapping(value ={"/", "/login"}, method = RequestMethod.GET)
public String loginPage(Model model) {
   return ("loginPage");
}

@RequestMapping(value = "/logoutSuccessful", method = RequestMethod.GET)
public String logoutSuccessfulPage(Model model) {
   model.addAttribute("title", "Logout");
   return "logoutSuccessfulPage";
}

 @RequestMapping(value = "/home", method = RequestMethod.GET)
 public String loginPage(Model model, Principal principal) {
   model.addAttribute("title", "Home");

   String userName = principal.getName();

   model.addAttribute("message",
           "Welcome Home " + userName);

   return "homePage";
}

@RequestMapping(value = "/useradmin", method = RequestMethod.GET)
public String adminPage(Model model) {
   model.addAttribute("title", "Admin");
   model.addAttribute("message", "Admin Page - This is protected!");
   return "userAdmin";
}

@RequestMapping(value = "/403", method = RequestMethod.GET)
public String accessDenied(Model model, Principal principal) {
   model.addAttribute("title", "Access Denied!");

   if (principal != null) {
       model.addAttribute("message", "<br>Error 403"
               + "<br> You do not have permission to access this page!");
   } else {
       model.addAttribute("msg",
               "You do not have permission to access this page!");
   }
   return "403Page";
}

 /** Form Handling  **/
 @RequestMapping(value = "/changepass", method = RequestMethod.POST)
 public String changePassword(@ModelAttribute ("user") User user, Principal     principal) {
   // for testing purpose:
   String name = principal.getName();
   System.out.println("entered changePass");
   return "redirect:/home.jsp";
   }
}  

我最终得到了错误(加载屏幕时):

SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: Neither BindingResult nor plain target     object for bean name 'user' available as request attribute
at org.springframework.web.servlet.support.BindStatus.<init>    (BindStatus.java:144)

编辑:添加用户类

package model;

public class User {

private String username;
private String currentpassword;
private String newpassword;
private String newpasswordconfirmation;

public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getCurrentpassword() {
    return currentpassword;
}
public void setCurrentpassword(String currentpassword) {
    this.currentpassword = currentpassword;
}
public String getNewpassword() {
    return newpassword;
}
public void setNewpassword(String newpassword) {
    this.newpassword = newpassword;
}
public String getNewpasswordconfirmation() {
    return newpasswordconfirmation;
}
public void setNewpasswordconfirmation(String newpasswordconfirmation) {
    this.newpasswordconfirmation = newpasswordconfirmation;
}
}

mvc-dispatcher-servlet

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="
   http://www.springframework.org/schema/beans    
   http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-4.1.xsd">

 <context:component-scan base-package="controller" />
 <context:annotation-config />
 <mvc:default-servlet-handler/>

 <bean
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="prefix">
   <value>/WEB-INF/pages/</value>
 </property>
 <property name="suffix">
   <value>.jsp</value>
 </property>
 </bean>
 <mvc:resources mapping="/resources/**" location="/resources/css/,   /resources/js/, /resources/images/, /resources/fonts/" cache-period="31556926"/>
 <mvc:annotation-driven />
</beans>

我是否需要在调度程序中映射用户 bean?我已经尝试了许多不同的方法,包括简单形式(已弃用)。我也认为应该使用 modelAttribute 而不是 command。

任何建议都会受到好评。

【问题讨论】:

  • 您是否尝试过搜索您遇到的异常? google.com/…
  • 是的,我这两天一直在找。我发现的资源似乎使用欢迎文件,或者它们使用命令而不是 modelAttribute。我试图关注 [link] (stackoverflow.com/questions/21790656/…) 但这似乎也没有多大帮助
  • 请发布 User 类代码和 MainController 类代码的其余部分。
  • 嗨@lordhazra,感谢您对此进行调查。我已经添加了 MainController 和 User 类,在我今天大部分时间都在努力让它工作时进行了一些编辑。有什么不好的做法吗?不知道这个错误是从哪里来的。

标签: spring twitter-bootstrap spring-mvc binding illegalstateexception


【解决方案1】:

在 GET 请求中将模型属性“user”添加到模型中,该请求将在表单提交之前调用。

例子

获取加载页面和表单的请求

@RequestMapping(value ={"/", "/login"}, method = RequestMethod.GET)
    public String loginPage(Model model) {
    model.addAttribute("user", new User()); // Here it is added to the model
    return ("loginPage");
}

表单提交

@RequestMapping(value = "/changepass", method = RequestMethod.POST)
public String changePassword(@ModelAttribute ("user") User user, Principal principal) {
    // for testing purpose:
    String name = principal.getName();
    System.out.println("entered changePass");
    return "redirect:/home.jsp";
}

【讨论】:

  • 感谢您的回复,但是表单位于引导模式和每个页面上。从您上面的回答中我可以看到的唯一变化是包含 model.addAttribute("user", new User());在 //login 映射中。你有空聊天吗?
  • 我已将表单操作更改为 /changepass 而不是 changepass 并且应用程序加载但是在单击按钮时它现在推送到错误 404 /changepass not found,有什么想法吗?
  • 在 GET 请求中将“user”对象添加到模型中。让我知道这是否有效。
  • 在jsp中将其保存为“changepass”。
  • 好的,我现在将它作为 jsp 中的 changepass 和请求映射处理程序中的 /changepass。单击保存按钮时,我找不到 404(在 url innov8/changepass 上)。即在 GET 中有或没有 model.addattribute。
猜你喜欢
  • 2016-02-17
  • 2020-09-26
  • 2018-01-07
  • 2020-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多