问题
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.
回答1:
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:
- 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
- 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
来源:https://stackoverflow.com/questions/7931469/how-to-implement-a-custom-viewprocessort-jax-rs