如何实现自定义ViewProcessor(jax-rs)?
查看文章此处,其中有一个如何使用 TemplateProcessor 解析 JSP 的示例使用 Jersey 的视图。显然这个类现在已被弃用并被 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想插入对新型模板的支持,则应该实现 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,您还需要做两件事:
如果您仍然认为出于某种原因必须实现自定义 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:
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