简体   繁体   中英

Binding problems using Spring MVC

I am using Spring MVC 3.1 to develop a Java web app. I have a JSP that has two paired radio buttons, an entry field, and a dropdown select box. I need these values to be available to my mapped controller, via a model class' fields.

The security and URL mapping works fine, as I've seen in debugger before. The issue is that when I tried to get the JSP data values populating my model, I get an error:

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

Here is part of my 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">

Here is part of my controller:

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

and finally, here is my model class:

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

}

Can anyone tell me what I need to get this to work correctly? Please don't stop at just the reason for the exception above - please verify and correct my code as I am on a tight schedule as usual and have little experience with Spring MVC. Thanks!

You have this in your form:

modelAttribute="cccForm"

So you should have this in your controller:

@ModelAttribute("cccForm") CCCapturePaymentForm ccCapturePaymentRequest

That's how you bind the form backing object with the model attribute.

I found the answer for those Spring MVC newbies...

I had to set the "cccForm" to a new instance of my next page's form inside the controller search method (that then tries to being up the page that was getting the error).

In a nutshell: I had to set the empty backing bean value in the preceding controller method so that the follow-on method and JSP page have it to work with.

Hope this helps someone else avoid my mistake. Mark

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM