java.lang.IllegalStateException:BindingResult 和 Bean 名称的纯目标对象'user'可用作请求属性



我正在尝试通过弹簧表单从引导模式发送请求,只要正确输入当前密码,用户就可以更改其密码。我在理解如何处理这个问题的物流时遇到了一些麻烦,目前我有一个表格:

<!-- 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而不是命令。

任何建议都广受欢迎。

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

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

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

相关内容

最新更新