简体   繁体   English

将HTML代码作为属性添加到ModelAndView中

[英]Add html code into ModelAndView as attribute

Normally if I have to add some html code dynamically, I would like to load from the view with JQuery, and then I render a ModelAndView from the controller using @ResponseBody, since I believe that adding static html from Javascript is not readable and is not reusable. 通常,如果我必须动态添加一些html代码,我想使用JQuery从视图中加载,然后使用@ResponseBody从控制器渲染ModelAndView,因为我认为从Javascript添加静态html是不可读的,因此不可行可重复使用的。 Now I have a case where making a Ajax call I need to check some values from the controller and then render back a map with some values, or render an html code to be added into the DOM. 现在,我有一个案例,在进行Ajax调用时,我需要从控制器检查一些值,然后使用一些值呈现回映射,或者呈现要添加到DOM中的html代码。 So I have been thinking about how the ModelAndView works, and how can I add a view to be processed on the viewManagement, but only being render if I decide to append that data. 因此,我一直在考虑ModelAndView的工作方式,以及如何在viewManagement上添加要处理的视图,但是只有在我决定附加该数据时才进行渲染。 So imagine that I would like to have a map with this values 所以想像一下,我想要一张具有此值的地图

    Map<String, Object> map = new HashMap<>
    map.add("status", "1")

And in another scenario 在另一种情况下

        Map<String, Object> map = new HashMap<>
        map.add("status", "2")
        map.add("rowErros", "WEB-INF/row_errors.jsp")

And then in the view 然后在视图中

      success:function(data)
             if(data.status=="2") $("#rowErrorsTable tbody tr:last").after(data.rowErros);

Regards. 问候。

I don't happen to agree that including markup in client-side code is per se less readable or reusable, but regardless, I'm not sure why you would choose to use a ModelAndView for this use case. 我不同意这种说法,即在客户端代码中包含标记本身不易读取或可重用,但是无论如何,我不确定为什么您会选择在此用例中使用ModelAndView。 You could accomplish this on the frontend using success and error callbacks, and by returning appropriate HTTP response codes from your controller method: 您可以在前端使用成功和错误回调以及通过从控制器方法返回适当的HTTP响应代码来完成此操作:

@Request(value="/foo")
public ResponseEntity<String> foo() {
    if (conditionIsMet()) {
        return new ResponseEntity<String>("", HttpStatus.OK); // Or some simple JSON string
    } else {
        return new ResponseEntity<String>(HttpStatus.BAD_REQUEST);
    }
}

And on the frontend: 在前端:

success: doSuccess,
error: function() { $('#error').load('/error.jsp'); } // Optionally sniff for the incoming status code

Assuming your error case is far less frequent than your success case, then you're also: 假设您的错误案例比成功案例少得多,那么您还可以:

  1. saving your users the bandwidth associated with transmitting the markup of error.jsp with each payload; 为您的用户节省与每个有效负载传输error.jsp标记相关的带宽;
  2. using HTTP status codes as appropriate responses to HTTP requests, which cleans up and normalizes your API 使用HTTP状态代码作为对HTTP请求的适当响应,从而清理和规范化API

If you need additional information in your error payload, consider a JSON object that gives the frontend information about the error over and above what the HTTP status code itself provides. 如果您在错误有效负载中需要其他信息,请考虑一个JSON对象,该对象在HTTP状态代码本身所提供的内容之外,还提供有关错误的前端信息。 That's not necessary in many cases, but it can be useful for BAD_REQUEST situations where you wish to surface what incoming request element caused the request to fail. 在许多情况下,这不是必需的,但对于希望了解哪些传入请求元素导致请求失败的BAD_REQUEST情况,它可能会很有用。

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

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