users@jersey.java.net

Re: [Jersey] make jsp:include work

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 26 Nov 2009 13:49:33 +0100

Hi,

Jersey does have support for includes based on resolving to the "it"
object (it is just not documented :-( ) but there is an example of it
in the Bookstore sample.

Attached is a com/sun/jersey/samples/bookstore/resources/CD/index.jsp
notice the following:

   <rbt:include page="footer.jsp"/>

Because the CD class inherits from Item the com/sun/jersey/samples/
bookstore/resources/Item/footer.jsp will be included.

Is that the sort of functionality you require?

Otherwise would it be possible to log an issue and attache a test case
as that will help me better understand what you require.

Note that we really do need to improve this area. I believe it has a a
lot of potential. As usual it just requires some focused time and
resources on it.

Paul.



<%_at_page contentType="text/html"%>
<%_at_page pageEncoding="UTF-8"%>
<%--
The taglib directive below imports the JSTL library. If you uncomment
it,
you must also add the JSTL library to the project. The Add Library...
action
on Libraries node in Projects view can be used to add the JSTL 1.1
library.
--%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%_at_taglib prefix="rbt" uri="urn:com:sun:jersey:api:view" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
     <head>
         <meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
         <title>Book</title>
     </head>
     <body>

         <h1>${it.title}</h1>

         Book from ${it.author}

         <rbt:include page="footer.jsp"/>

     </body>
</html>



On Nov 26, 2009, at 1:31 PM, Yoryos wrote:

> These days I'm trying to make jersey work right with the
> jsp:include. I mean given a jsp page to be able to do a <jsp:include
> page="/some/path/to/our/jaxrs" />. I managed to have a working
> solution after extending the Jersey filter, and the
> JSPTemplateProcessor. Shouldn't something like that be part of
> default imlementation of jersey. The only thing that has to be
> changed is a check if the request is "imported" and if so proceed
> based on the url informations that can be extracted from the
> imported request uri and query srting. The above is a working
> implementation of that
>
> public class ExtendedJerseyServletContainer extends ServletContainer {
> private static final long serialVersionUID =
> -9034703985328011311L;
> private static final Logger logger =
> Logger.getLogger(ExtendedJerseyServletContainer.class.getName());
>
> private static final String INCLUDE_REQUEST_URI =
> "javax.servlet.include.request_uri";
> private static final String INCLUDE_QUERYSTRING =
> "javax.servlet.include.query_string";
> private static final String INCLUDE_SERVLETPATH =
> "javax.servlet.include.servlet_path";
>
> @Override
> public void doFilter(HttpServletRequest request,
> HttpServletResponse response, FilterChain chain) throws IOException,
> ServletException {
> String includeRequestURI = (String)
> request.getAttribute(INCLUDE_REQUEST_URI);
> if (includeRequestURI == null) {
> super.doFilter(request, response, chain);
> return;
> }
>
> String requestURI = request.getRequestURI();
> if (includeRequestURI.equalsIgnoreCase(requestURI)) {
> super.doFilter(request, response, chain);
> return;
> }
>
> // if we match the static content regular expression lets
> delegate to the filter chain
> // to use the default container servlets & handlers
> Pattern p = getStaticContentPattern();
> if (p != null &&
> p
> .matcher
> ((String)request.getAttribute(INCLUDE_SERVLETPATH)).matches()) {
> chain.doFilter(request, response);
> return;
> }
>
> String includeQueryString = (String)
> request.getAttribute(INCLUDE_QUERYSTRING);
> final UriBuilder absoluteUriBuilder =
> UriBuilder.fromUri(request.getRequestURL().toString());
> final URI baseUri =
> absoluteUriBuilder
> .replacePath(request.getContextPath()).path("/").build();
> final URI requestUri =
> absoluteUriBuilder
> .replacePath
> (includeRequestURI).replaceQuery(includeQueryString).build();
>
> if (logger.isLoggable(Level.FINE)) logger.log(Level.FINE,
> String.format("Seems like we have an included request: '%s?%s' when
> the default requese was '%s?%s'", includeRequestURI,
> includeQueryString, requestURI, request.getQueryString()));
>
> service(baseUri, requestUri, request, response);
> }
> }
>
> The problem is with the returning of Viewables as they bind by
> default the model to the "it" key, so I should also create a Custom
> template processor that would bind the model to a key based on the
> class name of the jaxrs resource object.
>
> It could be much easier to have Viewable be able to constructed with
> an additional String parameter witch would be the key that will be
> used to bind the model to the request scope. I couldn't find out how
> Viewable exactly works to overcome this. But as I've seen the
> actually binding get placed in the JspTemplateProcessor and more
> precisely to the custom RequestDispatcher that is used. I couldn't
> find there any class/field/something that had to do with Viewable.
> Could someone guide me on this one?
>
> Thanks in advanced
> Yoryos
>