views:

203

answers:

2

Hi All,

Please help me with what I want to do.

I have this scenario:

  1. I display a list of assets to users.
  2. User selects an asset and then clicks add item
  3. On my requestmapping, during GET operation. I use the service class to check if indeed this asset still exist in DB
  4. If not, user should be notified by a message. I use the form:error tag

My problem is when I add the error object in the method signature, I got this error Errors/BindingResult argument declared without preceding model attribute

@RequestMapping(value = "/addItemsToAsset.htm", method = RequestMethod.GET)
    public String setupForm(@RequestParam("assetID") Long assetID,
            Errors error, ModelMap model) {
        AssetItemVo voAsset = null;

        if (assetID != null && assetID != 0) {
            //Get data for asset from DB using assetID
            List<AssetDraftTempVo> lstDraft = service.getAssetDraftByLngID(assetID);

            if (lstDraft.size() == 0) {
                voAsset = new AssetItemVo();
                // I wanted to add validation here.  If no data for asset id is found, I would like to add an error to the error object
                error.reject("123","Unable to find info for the asset in the database.");
            } else {
                AssetDraftTempVo voDraft = lstDraft.get(0);
                voAsset = new AssetItemVo();
                voAsset.setStrPlant(voDraft.getStrPlant());
                .
                . /*other DTO property here*/
                .
            }
        }
        model.put("assetItemDetail", voAsset);
        return "additemstoasset";
    }

My goal is that during the display of the form, I wanted to populate the error object right away (if there is an error)

Here's my form for clarity.

<form:form modelAttribute="assetItemDetail"  method="post">
    <div id="error_paragraph">
        <form:errors path="*" cssClass="errors" />
    </div>
</form:form>

To get past the error, I manually change the method signature and added the model attribute but it still cannot populate the form:error tag

@RequestMapping(value = "/addItemsToAsset.htm", method = RequestMethod.GET)
public String setupForm(@RequestParam("assetID") Long assetID,
        @ModelAttribute("assetItemDetail") AssetItemVo voAssetData, Errors error,
        ModelMap model) 

Please help..

+1  A: 

Spring MVC needs to associate a bunch of errors with some objects on the model, so tags such as <form:errors path="..." /> will correspond to model objects according to the path attribute. In this case, it does so by looking for the Errors argument directly after the ModelMap argument in your controller method.

Try swapping the error and model method arguments and see if it clears up the "Errors/BindingResult argument declared without preceding model attribute" error.

Change this:

public String setupForm(@RequestParam("assetID") Long assetID,
        @ModelAttribute("assetItemDetail") AssetItemVo voAssetData, Errors error,
        ModelMap model) 

to this:

public String setupForm(@RequestParam("assetID") Long assetID,
        @ModelAttribute("assetItemDetail") AssetItemVo voAssetData, ModelMap model,
        Errors error) 
James Earl Douglas
+1  A: 

If you want to associate a BindingResult with a model attribute that doesn't present in the method signature, you can do it manually:

BindingResult result = new Errors();
...
model.put(BindingResult.MODEL_KEY_PREFIX + "assetItemDetail", result); 
model.put("assetItemDetail", voAsset); 
axtavt