如何实现自定义ViewProcessor< T> (JAX-RS)? [英] How to implement a custom ViewProcessor<T> (jax-rs)?

查看:276
本文介绍了如何实现自定义ViewProcessor< T> (JAX-RS)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

其他 ViewProcessor文章就在这里。

谢谢。

推荐答案

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

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.

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

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".

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

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. 将Jersey servlet注册为过滤器一个servlet(只需在web.xml中用filter替换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 /.*\")

  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/.*")

如果您仍然认为必须出于某种原因实施自定义ViewProcessor,那么您可以看一下w实现了freemarker视图处理器并从中受到启发 - 参见 http://java.net/projects/jersey/sources/svn/content/trunk/jersey/contribs/jersey -freemarker / src / main / java / com / sun / jersey / freemarker / FreemarkerViewProcessor.java?rev = 5453

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

这篇关于如何实现自定义ViewProcessor&lt; T&gt; (JAX-RS)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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