第二个(重定向)JSP页面上的模型属性不起作用,抛出错误 - Spring MVC

时间:2015-10-13 22:44:05

标签: spring spring-mvc

我的欢迎页面是Login.jsp。在提交时,它会重定向到另一个页面,该页面将登录用户的虚拟交易详细信息。 please view this image link to see the login flow

但是,当我尝试在第二页中添加一个带有模型属性的表单时,会抛出错误。

Login.jsp - 表示代码

  <form:form method="POST" action="login2.html" modelAttribute="user" id="form">  
<table>
    <tr><td>  <form:input path="user_id" placeholder="User ID:" value="" disabled="true"/></td></tr>    
        <tr><td><form:input path="email_id" placeholder="Email ID:" value="" /></td></tr>
    <tr> <td> <form:password path="password" placeholder="Password" value="" /> </td></tr>
     </table> 
 <input type="submit" value="Log-in"/> 
  </form:form>

login2.html的控制器类操作

@RequestMapping(value = "/login2", method = RequestMethod.POST)
 public ModelAndView login( @ModelAttribute("user") UserBean u1, BindingResult result) {
        /*some  code to call service class*/
        /* lst is a list<Users>*/
        model.put("t1", lst);
        return new ModelAndView("save", model);
    }

Save.jsp(重定向页面)

<form:form method="POST" action="saveTransaction.html" modelAttribute="trans" id="form">
            <table>
                <tr>
                    <td><form:label path="transaction_id">Transaction ID:</form:label></td>
                    <td><form:input path="transaction_id" value="${i.transaction_id}" readonly="true"/></td>
                </tr>
                <tr>
                    <td><form:label path="user_id">User ID:</form:label></td>
                    <td><form:input path="user_id" value="${i.user_id}"/></td>
                </tr>
                    <tr>
                  <td colspan="2"><input type="submit" value="Submit"/></td>
              </tr> 
            </table>            
        </form:form>




 <c:if test="${!empty t1}">
<table align="left" border="1">
        <tr>
            <th>transaction ID</th>
            <th>User ID</th>

            </tr>

            <c:forEach var="i" items="${t1}">
            <tr>
                <td><c:out value="${i.transaction_id}"/></td>
                <td><c:out value="${i.user_id}"/></td>
                <td align="center"><a href="edit.html?id=${i.transaction_id}">Edit</a> | <a href="delete.html?id=${i.transaction_id}">Delete</a></td>
            </tr>
        </c:forEach>
    </table>
    </c:if>

操作的控制器类 - saveTransaction.html

 @RequestMapping(value = "/saveTransaction", method = RequestMethod.POST)
    public ModelAndView saveTransaction( @ModelAttribute("trans") TransactionBeans t1, BindingResult result) {
    /* some code */
    /* lst is a list<TransactionDetails>*/
    model.put("t1", lst);
    return new ModelAndView("save", model);
        }

TransactionBeans.java - bean类

public class TransactionBeans implements Serializable{

    int user_id, transaction_id;


    public int getUser_id() {
        return user_id;
    }

    public void setUser_id(int user_id) {
        this.user_id = user_id;
    }


    public int getTransaction_id() {
        return transaction_id;
    }

    public void setTransaction_id(int transaction_id) {
        this.transaction_id = transaction_id;
    }   
}

此代码与表单一起使用 - 它列出了我在上面的屏幕截图中显示的所有数据。但是,它会在表单modelAttribute中引发错误。请注意,两种形式的模型属性都不同,绑定不同的表。 抛出错误:

root cause
    java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'trans' available as request attribute
        org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
        org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:178)
        org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:198)
        org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor(LabelTag.java:129)
        org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:119)
        org.springframework.web.servlet.tags.form.LabelTag.writeTagContent(LabelTag.java:89)
        org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)
        org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)
        org.apache.jsp.WEB_002dINF.views.save_jsp._jspx_meth_form_005flabel_005f0(save_jsp.java:208)
        org.apache.jsp.WEB_002dINF.views.save_jsp._jspx_meth_form_005fform_005f0(save_jsp.java:153)
        org.apache.jsp.WEB_002dINF.views.save_jsp._jspService(save_jsp.java:93)
        org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
        org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)

这是否意味着重定向页面不能与绑定另一组数据的调用页面具有不同的模型?

感谢。

1 个答案:

答案 0 :(得分:0)

我无法在控制器中看到“user”和“trans”属性传递到模型中。您对modelAttribute的观点似乎期望它们,这就是您收到此错误的原因。

同样很高兴看到你的GET方法处理程序,因为你只提供与POST方法相关的处理程序。

我可能忽略了你想要实现的目标,但每个视图都会呈现你给它的模型属性。

如果我为控制器建议以下内容,它会有帮助吗?

@RequestMapping(value = "/login2", method = RequestMethod.GET)
 public ModelAndView login(ModelMap model) {
    model.put("user", new UserBean(0, "", ""));
    return new ModelAndView("Login", model);
 }

@RequestMapping(value = "/login2", method = RequestMethod.POST)
 public ModelAndView loginPost( @ModelAttribute("user") UserBean u1, ModelMap model, BindingResult result) {
    List<TransactionBeans> lst = service.getAll();
    model.put("t1", lst);

    //Display default transaction
    model.put("trans", new TransactionBeans(0, 0));
    return new ModelAndView("save", model);
}

 @RequestMapping(value = "/saveTransaction", method = RequestMethod.POST)
    public ModelAndView saveTransaction(TransactionBeans t1, ModelMap model,BindingResult result) {
        service.save(t1);
        List<TransactionBeans> lst = service.getAll();

        model.put("t1", lst);
        model.put("trans", t1);
        return new ModelAndView("save", model);
  }

 @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public ModelAndView edit(@RequestParam("id") String transactionId, ModelMap model) {
        TransactionBeans trans = service.get(transactionId);
        List<TransactionBeans> lst = service.getAll();

        model.put("t1", lst);
        model.put("trans", trans);
        return new ModelAndView("save", model);
  }
祝你好运