简体   繁体   English

在GET METHOD弹簧注释的控制器上添加错误

[英]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. 在我的请求映射中,在GET操作期间。 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 我使用form:error标签

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 声明的错误/ BindingResult参数没有前面的模型属性

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 为了克服该错误,我手动更改了方法签名并添加了model属性,但仍然无法填充form:error标记

@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与方法签名中不存在的模型属性相关联,可以手动进行操作:

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. Spring MVC需要将大量错误与模型上的某些对象相关联,因此<form:errors path="..." />标签将根据path属性对应于模型对象。 In this case, it does so by looking for the Errors argument directly after the ModelMap argument in your controller method. 在这种情况下,它是通过在控制器方法中的ModelMap参数之后直接查找Errors参数来实现的。

Try swapping the error and model method arguments and see if it clears up the "Errors/BindingResult argument declared without preceding model attribute" error. 尝试交换错误和模型方法参数,并查看它是否清除了“声明的Errors / BindingResult参数没有前面的模型属性”错误。

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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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