从视图中省略ModelAttribute

编程入门 行业动态 更新时间:2024-10-24 04:47:12
本文介绍了从视图中省略ModelAttribute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个rest应用程序,它返回json/xml.我使用杰克逊和jaxb进行转换.一些方法需要接受query_string.我已经使用@ModelAttribute将query_string映射到一个对象中,但这将对象强制进入了我的视图.我不希望该对象出现在视图中.

I have a rest application that returns json/xml. I use jackson and jaxb for conversion. Some methods need to accept a query_string. I've used @ModelAttribute to map the query_string into an object, but this forces the object into my view. I do not want the object to appear in the view.

我想我需要使用@ModelAttribute以外的其他东西,但是我无法弄清楚如何进行绑定,但是不能修改视图.如果我省略@ModelAttribute批注,则该对象将在视图中显示为变量名称(例如:"sourceBundleRequest").

I think I need to use something other than @ModelAttribute, but I can't figure out how to do the binding, but not modify the view. If I omit @ModelAttribute annotation, the object appears in the view as the variable name (eg: "sourceBundleRequest").

示例网址:

localhost:8080/rest/sourcebundles/?updateDate=20100501&label=urgent

控制器方法:

@RequestMapping(value = {"", "/"}, method = RequestMethod.GET) public String getAll(@ModelAttribute("form") SourceBundleRequest sourceBundleRequest, BindingResult result, ModelMap model) throws ApiException { // Detect and report errors. if (result.hasErrors()) { // (omitted for clarity) } // Fetch matching data. PaginatedResponse<SourceBundle> sourceBundleResponse = null; try { int clientId = getRequestClientId(); sourceBundleResponse = sourceBundleService.get(clientId, sourceBundleRequest); } catch (ApiServiceException e) { throw new ApiException(ApiErrorType.INTERNAL_ERROR, "sourceBundle fetch failed"); } // Return the response. RestResponse<PaginatedResponse> restResponse = new RestResponse<PaginatedResponse>(200, "OK"); restResponse.setData(sourceBundleResponse); model.addAttribute("resp", restResponse); // XXX - how do I prevent "form" from appearing in the view? return "restResponse"; }

示例输出:

"form": { "label": "urgent", "updateDate": 1272697200000, "sort": null, "results": 5, "skip": 0 }, "resp": { "code": 200, "status": "OK", "data": { "totalAvailable": 0, "resultList": [ ] } }

所需的输出:

"resp": { "code": 200, "status": "OK", "data": { "totalAvailable": 0, "resultList": [ ] } }

省略@ModelAttribute("form")

如果我只是省略了@ModelAttribute("form"),我仍然会收到不期望的响应,但是传入的表单是由对象名称命名的.响应如下所示:

If I simply omit @ModelAttribute("form"), I still get the undesirable response but the incoming form is named by the object name. The response looks like this:

"resp": { "code": 200, "status": "OK", "data": { "totalAvailable": 0, "resultList": [ ] } }, "sourceBundleRequest": { "label": "urgent", "updateDate": 1272697200000, "sort": null, "results": 5, "skip": 0 }

推荐答案

我不知何故错过了解决此问题的最明显方法.我专注于属性,却忘记了我只能修改基础Map.

Somehow I missed the most obvious way to fix this. I was focused on the Attributes and forgot that I can just modify the underlying Map.

// Remove the form object from the model map. model.remove("form");

按照Biju的建议省略@ModelAttribute,然后删除sourceBundleRequest对象,可能会更有效率.我怀疑@ModelAttribute有一些额外的开销.

It might be a little more efficient to omit @ModelAttribute as Biju suggested and then remove the sourceBundleRequest object. I suspect @ModelAttribute has some additional overhead.

更多推荐

从视图中省略ModelAttribute

本文发布于:2023-11-03 14:57:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555410.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   ModelAttribute

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!