【问题标题】:Binding problems using Spring MVC使用 Spring MVC 的绑定问题
【发布时间】:2012-10-06 21:39:36
【问题描述】:

我正在使用 Spring MVC 3.1 开发 Java Web 应用程序。我有一个 JSP,它有两个成对的单选按钮、一个输入字段和一个下拉选择框。我需要通过模型类的字段将这些值提供给我的映射控制器。

正如我之前在调试器中看到的那样,安全性和 URL 映射工作正常。问题是,当我尝试获取填充模型的 JSP 数据值时,出现错误:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'cccForm' available as request attribute

这是我的 JSP 的一部分:

<c:url var="cccUrl" value="/registers/default/ccPreauth/authorize" />
    <div class="mainWrapper">
    <form:form id="cccForm" action="${cccUrl}" method="post" modelAttribute="cccForm">
  ...
<table>
    <tbody>
        <tr>
            <th>Select an option.</th>
        </tr>
        <tr>
            <td>
                <div class="field-input">
                    <form:radiobutton id="paymentOption" path="paymentOption" value="authorizeCC" />
                    Collect Credit Card Information
                </div>
                <div class="field-input">
                    Authorization Amount $
                    <form:input path="authAmount" maxlength="10" size="10" class="extendWidth"/> 
                    <span class="instructions">
                        <spring:message code="label.authorization.note" />
                    </span>
                </div>
                <div class="field-input">
                    <form:radiobutton id="paymentOption" path="paymentOption" value="cancelAuth" />
                    Choose a Reason and Cancel Credit Card Collection
                </div>
                <div class="field-input right">
                    <form:select id="selectedReason" path="selectedReason" >
                        <c:forEach items="${reasonList}" var="reason">
                            <option value=${reason.reasonText}>${reason.reasonText}</option>
                            <br />
                        </c:forEach>
                    </form:select>
                </div></td>
        </tr>
    </tbody>
</table>
</div>
<div class="right">
<button class="btnBlue" id="submitButton" type="submit">

这是我的控制器的一部分:

    @Controller
@RequestMapping(value = "/registers/default/ccPreauth")
@SessionAttributes(ControllerConstants.DEFAULT_REGISTER_ATTR_NM)
public class CCCaptureController {

...
    @RequestMapping(value="/authorize" )
public ModelAndView authorize(
         final Authentication auth,
         final @ModelAttribute("ccCapturePaymentRequest") CCCapturePaymentForm ccCapturePaymentRequest,
         final BindingResult result,
         final HttpServletResponse response) {

      final ModelAndView mav = new ModelAndView(CC_PREAUTH_PAYMENT_VIEW);

      return mav;
}

最后,这是我的模型类:

    public class CCCapturePaymentForm implements Serializable {

    private static final long serialVersionUID = 6839171190322687142L;
    @NumberFormat(style = Style.CURRENCY)
    private BigDecimal  authAmount;
    private String      selectedReason;
    private String      paymentOption;

    public BigDecimal getAuthAmount() {
        return authAmount;
    }

    public void setAuthAmount(BigDecimal authAmount) {
        this.authAmount = authAmount;
    }

    public String getSelectedReason() {
        return selectedReason;
    }

    public void setSelectedReason(String selectedReason) {
        this.selectedReason = selectedReason;
    }

    public String getPaymentOption() {
        return paymentOption;
    }

    public void setPaymentOption(String paymentOption) {
        this.paymentOption = paymentOption;
    }

}

谁能告诉我需要什么才能让它正常工作?请不要仅仅停留在上述异常的原因上——请验证并更正我的代码,因为我的日程安排很紧,而且对 Spring MVC 几乎没有经验。谢谢!

【问题讨论】:

    标签: java data-binding spring-mvc


    【解决方案1】:

    你的表单中有这个:

    modelAttribute="cccForm"
    

    所以你应该在你的控制器中有这个:

    @ModelAttribute("cccForm") CCCapturePaymentForm ccCapturePaymentRequest
    

    这就是将表单支持对象与模型属性绑定的方式。

    【讨论】:

    • 感谢您的帖子。我试着改变这个,但得到了同样的错误。知道有什么问题吗?
    • 抱歉,我不确定,我无法从您的代码中重现,因为缺少一些东西:常量、关闭 标记等。尝试发布一个较小的编译安全版本你的代码。
    • 扩展 Betoverse 答案:使用@ModelAttribute("cccForm") CCCapturePaymentForm cccForm
    • 感谢您的评论,但我遇到了同样的错误。似乎可能是由于映射到支持 bean?单选按钮和选择选项的正确方法是什么?
    【解决方案2】:

    我为那些 Spring MVC 新手找到了答案……

    我必须在控制器搜索方法中将“cccForm”设置为我的下一页表单的新实例(然后尝试向上出现错误的页面)。

    简而言之:我必须在前面的控制器方法中设置空的支持 bean 值,以便后续方法和 JSP 页面可以使用它。

    希望这可以帮助其他人避免我的错误。 标记

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-15
      • 1970-01-01
      • 1970-01-01
      • 2015-07-17
      • 1970-01-01
      • 2014-01-08
      • 1970-01-01
      相关资源
      最近更新 更多