添加错误GET方法春天注明控制器 [英] Add Error on GET METHOD Spring annotated controller
问题描述
请帮助我,我想做的事情。
Please help me with what I want to do.
我有这样的场景:
- 显示我的资产给用户列表。
- 用户选择的资产,然后
点击添加项目 - 在我的requestmapping,在GET
操作。我用的是服务类
检查是否确实该资产仍然
存在于DB - 如果不是,用户应该由一个通知
信息。我用以下形式:错误
标签
我的问题是,当我在方法签名中添加错误的对象,我得到这个错误的错误/ BindingResult说法没有宣布preceding模型属性
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)
请帮助..
推荐答案
如果你想用未present在一个模型属性关联一个 BindingResult
方法签名,可以手动做到这一点:
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方法春天注明控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!