Jackson ObjectMapper 的替代方案

发布于 2025-01-08 15:26:14 字数 589 浏览 7 评论 0原文

我正在寻找一个与 Jackson ObjectMapper 一起使用或代替 Jackson ObjectMapper 的标签库。我正在升级我们的网站,并且我们的 JSP 文件中不允许内联代码块。因此我需要找到一些可以解决我的问题的标签库。

<jsp:useBean id="mapper" class="org.codehaus.jackson.map.ObjectMapper"  />
<%=mapper.writeValueAsString(pageContext.getAttribute("result"))%>

但我无法将其转换为这样的内容:

 <c:out value="${mapper.writeValueAsString(pageContext.getAttribute('result'))}"></c:out>

这会引发异常:

未指定默认命名空间时,函数 writeValueAsString 必须使用前缀

谢谢。

I am looking for a taglib to be used with or instead of Jackson ObjectMapper. I am working on upgrading our website, and we do not allow inline code blocks in our JSP files. So therefor I need to find some tag library that can solve my problems.

<jsp:useBean id="mapper" class="org.codehaus.jackson.map.ObjectMapper"  />
<%=mapper.writeValueAsString(pageContext.getAttribute("result"))%>

But I can not convert this to something like this:

 <c:out value="${mapper.writeValueAsString(pageContext.getAttribute('result'))}"></c:out>

This throws exception:

The function writeValueAsString must be used with a prefix when a default namespace is not specified

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戴着白色围巾的女孩 2025-01-15 15:26:14

定义自定义 JSTL 函数:

  • 它必须是一个静态方法,
  • 创建一个定义函数的 .tl

:。

<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
    version="2.1">

    <tlib-version>1.0</tlib-version>
    <short-name>functions</short-name>
    <uri>http://foo.com/tags</uri>

    <function>
        <name>getJSON</name>
        <function-class>com.....ObjectMapper</function-class>
        <function-signature>String writeValueAsString(java.lang.String)</function-signature>
    </function>
</taglib>

然后您可以将其与 f:getJSON(..) 一起使用,

但一般来说,在 JSP 中执行此操作可能并不正确 - 在控制器/servlet 中预先计算它,并将其添加为模型属性。

Define a custom JSTL function:

  • it has to be a static method
  • make a .tl that defines the function:

.

<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
    version="2.1">

    <tlib-version>1.0</tlib-version>
    <short-name>functions</short-name>
    <uri>http://foo.com/tags</uri>

    <function>
        <name>getJSON</name>
        <function-class>com.....ObjectMapper</function-class>
        <function-signature>String writeValueAsString(java.lang.String)</function-signature>
    </function>
</taglib>

Then you can use it with f:getJSON(..)

But generally, it might not be right to do this in the JSP - precompute it in the controller/servlet, and add it as model attribute.

迷离° 2025-01-15 15:26:14

我通过在 WEB-INF 文件夹中的 .tag 文件中实现该功能解决了这个问题,在该文件夹中我们允许内联编码。不完全是最好的解决方案,但它确实有效。

I solved this, by implementing the functionality in a .tag file in the WEB-INF folder, where we allow inline coding. Not exactly the best solution, but it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文