在 GET METHOD Spring 注释控制器上添加错误 [英] Add Error on GET METHOD Spring annotated controller

查看:19
本文介绍了在 GET METHOD Spring 注释控制器上添加错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个场景:

  1. 我向用户显示资产列表.
  2. 用户选择一个资产,然后点击添加项目
  3. 在我的请求映射中,在 GET 操作期间.我使用服务类来检查该资产是否确实仍然存在于 DB 中
  4. 如果没有,则应通过消息通知用户.我使用表单:错误标记

我的问题是当我在方法签名中添加错误对象时,我得到了这个错误:

<块引用>

在没有前面的模型属性的情况下声明的错误/BindingResult 参数

代码:

@RequestMapping(value = "/addItemsToAsset.htm", method = RequestMethod.GET)public String setupForm(@RequestParam("assetID") 长资产ID,错误错误,ModelMap 模型){AssetItemVo voAsset = null;如果(资产 ID != null && 资产 ID != 0){//使用assetID从DB获取资产数据列表lstDraft = service.getAssetDraftByLngID(assetID);如果 (lstDraft.size() == 0) {voAsset = 新的 AssetItemVo();//我想在这里添加验证.如果没有找到资产 id 的数据,我想在错误对象中添加一个错误error.reject("123","无法在数据库中找到该资产的信息.");} 别的 {AssetDraftTempVo voDraft = lstDraft.get(0);voAsset = 新的 AssetItemVo();voAsset.setStrPlant(voDraft.getStrPlant());../*其他DTO属性在这里*/.}}model.put("assetItemDetail", voAsset);返回 "additemstoasset";}

我的目标是在表单的显示过程中,我想立即填充错误对象(如果有错误)

为了清楚起见,这是我的表格.

<div id="error_paragraph"><form:errors path="*" cssClass="errors"/>

</form:form>

为了克服错误,我手动更改了方法签名并添加了模型属性,但它仍然无法填充表单:错误标记

@RequestMapping(value = "/addItemsToAsset.htm", method = RequestMethod.GET)public String setupForm(@RequestParam("assetID") 长资产ID,@ModelAttribute("assetItemDetail") AssetItemVo voAssetData,错误错误,模型映射模型)

解决方案

如果要将 BindingResult 与方法签名中不存在的模型属性相关联,可以手动进行:

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

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

这篇关于在 GET METHOD Spring 注释控制器上添加错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆