Spring MVC-JSTL标签JSON转换可以吗? [英] Spring MVC - Is JSTL Tag JSON Conversion OK?

查看:314
本文介绍了Spring MVC-JSTL标签JSON转换可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伪控制器方法

@RequestMapping("/foo")
public String getFoo(Model model) {
   model.add("foo", repo.findFoo());
   model.add("bar", repo.findBar());
   model.add("barOptions", repo.findBarOptions(bar));
   return "fooView";
}

比方说,客户端使用表达式语言来呈现foobar;但是我们使用JavaScript渲染barOptions.

Let's say the client uses Expression language to render foo and bar; but we use JavaScript to render barOptions.

<html>
    <script>
    var options = <mytag:toJSON object="${barOptions}"/>;
    $("#options").renderOptions( options );
    </script>
    <body>
        <mytag:renderFoo foo="${foo}"/>
        <mytag:renderBar foo="${bar}"/>
        <ul id="options"></ul>
    </body>
</html>

通用约定告诉我,这很糟糕.但是,MVC的本质(控制器发送数据并由视图确定如何使用它)告诉我,这很好.有没有更好的方法来做同样的事情?有什么原因不能做到这一点?我可以使用一个单独的调用来请求JSON,但是随后我必须对页面进行更多的请求加载,并且在控制器方法getFoo()中可能有基于其他输入的逻辑来确定barOptions.页面加载.

Common conventions tells me this is bad. But the essence of MVC, where the controller sends data and the view determines how to use it, tells me this is good. Is there a better way to do the same thing? Is there any reason why this isn't commonly done? I could request the JSON using a separate call, but then I have to make more requests for the page to load, and there may be logic to determine barOptions in the controller method getFoo() based on other input at the time of the page load.

推荐答案

乍一看,我不能说我看到任何明显的方法错误.最初使我措手不及的唯一方面是您需要将模型对象中的数据转换为json.

At first glance I can not say that I see anything blatantly wrong with approach. The only aspect that initially took me off guard was your need to convert data in the model object to json.

对我来说,JSON通常意味着需要转换某种服务器端对象,以便客户端javascript可以以javascript方式访问或操纵其结构.我猜想在不了解选项列表的目的的情况下,我看不到为什么需要json序列化的原因.

For me, JSON usually implies that there is some sort of server side object that needs to be converted so that a client side javascript can access or manipulate its structure in a javascript way. I guess without knowing more of the purpose of the options list, I can't see a reason why json serialization is required here.

通过使用Tag将模型对象转换为JSON,我们避免了 客户提出的其他要求

By using a Tag to convert a model object to JSON, we avoid an additional request made by the client

但是,如果我们假设JSON是必需的(也许是某些第三方jquery插件),那么我绝对不会发现方法有什么问题.

But if we assume that JSON is a requirement (perhaps for some third party jquery plugin), then I absolutely do not see anything wrong with approach.

barOptions无序列表有什么特别之处或不同之处,为什么它用json呈现?为什么不仅仅使用for循环来构建列表项呢?或者,您可以使用一个自定义标签来完全构建ul.

What is special or different about the barOptions unordered list, why does it have be rendered with json? Why not just use a for loop to build the list items? Or you can have a custom tag that builds out the ul entirely.

除此之外,我还没有意识到人们可能认为这是错误代码的意思.

Aside from that, I missing the point as how one may perceive this as being bad code.

这篇关于Spring MVC-JSTL标签JSON转换可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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