如何实现自定义ViewProcessor(jax-rs)?

发布于 2024-12-12 18:42:45 字数 451 浏览 7 评论 0原文

查看文章此处,其中有一个如何使用 TemplateProcessor 解析 JSP 的示例使用 Jersey 的视图。显然这个类现在已被弃用并被 ViewProcessor取代。我对如何实现其中之一感到有些困惑(最好是较新的,因为它没有被弃用);什么作为模板参数?如何实现一个简单地解析 /WebContent/WEB-INF/views/* 中的 jsps 并评估返回视图的表达式语言?

另一篇 ViewProcessor 文章在这里。

谢谢。

Looking at the article here, there is an example of how to use a TemplateProcessor to resolve JSP views using Jersey. Apparently this class has been deprecated now and replaced by ViewProcessor<T>. I am somewhat confused about how to implement either (preferably the newer one since it isn't deprecated); what goes in as the template argument? How can I implement one to simply resolve jsps in /WebContent/WEB-INF/views/* and also evaluate the expression language of the returned view?

The other ViewProcessor article is here.

Thanks.

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

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

发布评论

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

评论(1

烟─花易冷 2024-12-19 18:42:45

如果您想插入对新型模板的支持,则应该实现 ViewProcessor。如果您需要的只是 JSP 模板,那么它已经内置到 Jersey 中,您不需要实现自己的 ViewProcessor。

如果您的 JSP 文件位于 WEB-INF/views 下,那么您可以设置 JSP_TEMPLATES_BASE_PATH 初始化参数WEB-INF/视图。然后,您只需从资源方法返回一个新的 Viewable 实例(如您所引用的 Paul 博客所示),并将模板名称作为第一个参数和数据(模型)作为第二个参数传递给构造函数。然后在 JSP 中您可以使用名为“it”的属性访问数据。

更新:如果你的 url-pattern 是“/*”,那就有点复杂了。 Jersey 在委托给容器时无法解析模板,因为 Jersey servlet 屏蔽了 JSP。为了使容器能够看到 JSP,您还需要做两件事:

  1. 将 Jersey servlet 注册为过滤器而不是 servlet(只需在 web.xml 中用过滤器替换 servlet 的出现) - 请参阅底部此 javadoc 页面:http://jersey .java.net/nonav/apidocs/latest/jersey/com/sun/jersey/spi/container/servlet/package-summary.html
  2. 添加 PROPERTY_WEB_PAGE_CONTENT_REGEX过滤器的初始化参数并将其设置为jsp模板匹配的正则表达式(例如“/WEB-INF/views/.*”)

如果您仍然认为出于某种原因必须实现自定义 ViewProcessor,您可以查看 freemarker 视图处理器的实现方式并从中获得启发 - 请参阅

ViewProcessor should be implemented if you want to plug in support for a new type of templates. If all you need is JSP templates, then that's already built into Jersey and you don't need to implement your own ViewProcessor.

If your JSP files are located under WEB-INF/views, then you can set the JSP_TEMPLATES_BASE_PATH init parameter to WEB-INF/views. Then you just need to return a new instance of Viewable from your resource method (as illustrated by Paul's blog you are referring to) and pass the template name as the first parameter and data (model) as the second parameter to the constructor. Then in the JSP you can access the data using the attribute named "it".

UPDATE: If your url-pattern is "/*" it is a bit more complicated. Jersey is not able to resolve the templates when delegating to the container, as the Jersey servlet is masking out the JSPs. To make it possible for the container to see the JSPs, you need to do two more things:

  1. register the Jersey servlet as a filter instead of a servlet (simply replace occurrences of servlet by filter in your web.xml) - see the bottom of this javadoc page: http://jersey.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/spi/container/servlet/package-summary.html
  2. add PROPERTY_WEB_PAGE_CONTENT_REGEX init param to the filter and set it to the regular expression that the jsp templates match (e.g. "/WEB-INF/views/.*")

If you still believe you have to implement a custom ViewProcessor for some reason, you can look at how the freemarker view processor is implemented and get inspired by that - see http://java.net/projects/jersey/sources/svn/content/trunk/jersey/contribs/jersey-freemarker/src/main/java/com/sun/jersey/freemarker/FreemarkerViewProcessor.java?rev=5453

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