开发者

Add Error on GET METHOD Spring annotated controller

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

Code:

@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) 


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


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) 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜