>When you are using the jsp document (xml) syntax, is it possible to
>pass in an xml fragment as a parameter to <c:import> and then
>reference the parameter as xml and not escaped text?
...

Whohoo! It worked. It took a lot of spec reading until i realised where the
character conversion occured, but once I found what was wrong, all it took
was one parameter change, and it worked like a charm.

In the document that gets imported (the main layout document), I just had to
specify <c:out value="${param.body}" escapeXml="false"/> to make it not
escape the xml fragment that gets passed in. Simple once I found out.
Difficult when I didn't knew.

If somebodys wondering what I'm up to I've discovered a great way to use jsp
(xml) documents with jstl as a push template technique that makes is
extremely easy to change, or create several different layouts for the whole
page. Quite xslt like in the style I think.

I assume that other people do this as well, but I've never seen any
tutorials or suggestions on this usage before, so I'll explain it briefly:
----------------------------------------------------------------------------
-----------------------------------------

1. The jsp that you call only contains one master c:import statement, where
you specify an optional number of parameters. This page should only contain
content and no html <body> <head> declarations etc.

2. These parameters gets pushed onto the imported page and fills out the
needed places. The imported page can be specified on runtime, thereby
changing the whole layout of the page, but still keeping the content the
same. Quite like how you change a master stylesheet in xslt and feed the
stylesheet with information on which xml it should include and then let it
*pull* the needed information from the xml files. The only difference with
this approach is that you tell the *content* file to include a layout
document that can be specified at runtime and let the content file *push*
parameters onto the template. It's the other way around, sort of.

3. If the jsp document (xml) syntax is used, the validity of the generated
(xml) file can always be guaranteed, since the page won't compile unless the
document is wellformed xml. Therefore only wellformed xml fragments can be
generated.

Example:
----------------------------------------------------------------------------
--------------------------------------------
This is the jsp content file that is requested. Lets call it content.jsp For
the moment this page includes  "/WEB-INF/jspf/main.jsp" but it could as
easily include the other file "/WEB-INF/jspf/print.jsp" instead. This could
be done directly by request parameters, so that the user could switch skins
by himself, or it could be set as a parameter in the session, or any place
else. You can create as many skins as you like, all with the same content.
If you craft your content files well, all css styling can be changed on the
fly too, to change the whole look of the content, for example to provide a
high contrast skin page.

<?xml version="1.0" encoding="UTF-8"?>
<jsp:root
 xmlns:jsp="http://java.sun.com/JSP/Page";
 xmlns:c="http://java.sun.com/jstl/core";
 version="1.2">
<jsp:directive.page contentType="text/html" pageEncoding="UTF-8"/>

<c:import url="/WEB-INF/jspf/main.jsp">
      <c:param name="windowTitle" value="I am a window title"/>

      <c:param name="headExtra">
          <style type="text/css">
               p{
                  font-size:36px;
               }
          </style>
      </c:param>

      <c:param name="pageTitle">
           I am a title
      </c:param>

      <c:param name="body">
          <div>
             <p>I am content</p>
             <p><c:out value="Any type of nested tags can be used in the
parameter"/></p>
          </div>
      </c:param>
</c:import>
</jsp:root>


----------------------------------------------------------------------------
---------------------------------
The default layout page, main.jsp, note that the imported files menu.jsp and
footer.jsp does not exist, you have to create them yourself

<?xml version="1.0" encoding="UTF-8"?>
<jsp:root
 xmlns:jsp="http://java.sun.com/JSP/Page";
 xmlns:c="http://java.sun.com/jstl/core";
 version="1.2">
<jsp:directive.page contentType="text/html" pageEncoding="UTF-8"/>

<html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en">
    <head>
       <title><c:out value="${param.windowTitle}"/></title>

       <c:out value="${param.headExtra}" escapeXml="false"/>
    </head>

   <body style="color:red;">
      <c:import url="/WEB-INF/jspf/menu.jsp"/>

         <fieldset>
            <legend><c:out value="${param.pageTitle}"
escapeXml="false"/></legend><br/>

            <c:out value="${param.body}" escapeXml="false"/>
         </fieldset>

      <c:import url="/WEB-INF/jspf/footer.jsp"/>
   </body>
</html>
</jsp:root>


----------------------------------------------------------------------------
---------------------------------
A stripped down version, print.jsp, that could be imported from content.sp
instead. Note that this page does not use all parameters sent to it

<?xml version="1.0" encoding="UTF-8"?>
<jsp:root
 xmlns:jsp="http://java.sun.com/JSP/Page";
 xmlns:c="http://java.sun.com/jstl/core";
 version="1.2">
 <jsp:directive.page contentType="text/html" pageEncoding="UTF-8"/>

<html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en">
   <head>
      <title><c:out value="${param.windowTitle}"/></title>
   </head>

   <body style="color:blue;">
      <div><c:out value="${param.pageTitle}" escapeXml="false"/></div>

      <div>
          <c:out value="${param.body}" escapeXml="false"/>
      </div>
   </body>
</html>
</jsp:root>



/Erik Beijnoff
Addsystems
[EMAIL PROTECTED]

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".

Some relevant archives, FAQs and Forums on JSPs can be found at:

 http://java.sun.com/products/jsp
 http://archives.java.sun.com/jsp-interest.html
 http://forums.java.sun.com
 http://www.jspinsider.com

Reply via email to