You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Johan Compagner <jc...@j-com.nl> on 2001/01/05 16:04:41 UTC

Template layout file with an include (bean or jsp) to a jsp with a form.

Hi,

I will ask the basic question first so that you don't have to read the whole
email:

So is there no way to use the same request over 2 or more jsp files that are
nested in
each other?

Now my test case:

I have a template jsp file that has all the layout of my site.
in the right left corner will al the dynamic data.

like this:

// tag libs includes
<form:html locale="true">
<head>
<title><bean:message key="index.title"/></title>
</head>
<body bgcolor="#fce8a0" topmargin="0" leftmargin="0">

<table width="100%" border="0" height="123" cellpadding="0" cellspacing="0"
bgcolor="#0066ff">
   <tr>
    <td>
	// standard layout of my site
	// jadajadajadajada
    </td>
	// The include logic.
	<logic:present name="<%= JSP_INCLUDE_KEY %>" scope="request">
	<bean:include id="subpage" name="<%=
(String)request.getAttribute(Constants.JSP_INCLUDE_KEY) %>"/>
	<%= subpage %>
	</logic:present>
	<logic:notPresent name="<%= Constants.JSP_INCLUDE_KEY %>" scope="request">
	<bean:include id="subpage" name="/index.jsp"/>
	<%= subpage %>
	</logic:notPresent>
  </tr>
</table>
</body>
</form:html>

This template file is always the ActionForward that i return as the next
page to see
after this action, something in the perform method of a Action:

	request.setAttribute(Constants.JSP_INCLUDE_KEY,
mapping.findForward("pensioen3a").getPath());
	return mapping.findForward("template");

The nicest thing about this is that if i want to change something of my site
i only have to edit
one file and every page/view is changed also like the logon page or the
pages with the forms.

But now my problem.

An Action is mostly triggerd by the subpage (where the form is in).
That Action is testing the input (some validation) if that goes wrong that
a ACtionError is made and is attached to the request.
But when i forward it (to the same page) i forward it to the template again
and that
template includes the page with the form.
But the include generates another request for that subpage so my
ActionErrors object
and other objects that i stored in the request aren't there!!

I also do this in te validate() of a Form:

	String fromPage = request.getParameter("frompage");
	mapping.setInput(mapping.findForward("template").getPath());
	request.setAttribute(Constants.JSP_INCLUDE_KEY,
mapping.findForward(fromPage).getPath());

So that when there are errors returned it knows that the input is the
template and the
include is the same page it come's from.

But this also doesn't work.
I can't be the only one with this problem.
Are nobody else using subpages where the form/inputs are in an another one
that is the standard layout page?

I thought to be smart and made my own include tag:

public int doStartTag() throws JspException
{
	try
	{
		RequestDispatcher rd=
pageContext.getServletContext().getRequestDispatcher(_sPage);
		rd.forward(pageContext.getRequest(), pageContext.getResponse());
	} catch(Exception e)
	{
		throw new JspException(e.getMessage());
	}
	return SKIP_BODY;
}

But the problem is then that the subpage (the _sPage string) is displayed
first, above
the main page instead of in the main page. Why is this? Because when i walk
through the code
then i see the main page that writes to the output and then the subpage and
then the main
page again. I think this has something to do with flushing ect.
But if i flush first (right above the RequestDispatcher line i do this:

 pageContext.getOut().flush();

Then i get the main page on top again but an error in the place where the
subpage should be:

	Location: /postbank/template.jsp
	Internal Servlet Error:

	javax.servlet.ServletException: Cannot forward as OutputStream or Writer
has already been 	obtained

So is there no way to use the same request over 2 or more jsp files that are
nested in
each other?

Johan








Re: Template layout file with an include (bean or jsp) to a jsp with a form. (there are many tomcat developer here also i believe)

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Johan Compagner wrote:

> Hi,
>
> I thought i shall be smart, i developed my own:
>  IncludeServletResponse that implements HttpServletResponse)
>

On a servlet 2.2 container, this is not legal -- the request and response
arguments that you pass on to a RequestDispatcher *must* be the ones you
originally received.  Many servlet containers (such as Tomcat 3.2) rely on this
in order to cast the object back to their own internal implementation class.

On a servlet 2.3 container (such as Tomcat 4.0), you can do this, as long as
your class does an "extends HttpServletResponseWrapper".

Craig McClanahan



RE: Template layout file with an include (bean or jsp) to a jsp with a form. (there are many tomcat developer here also i believe)

Posted by Johan Compagner <jc...@j-com.nl>.
Hi,

I thought i shall be smart, i developed my own:
 IncludeServletResponse that implements HttpServletResponse)

That one routes everything to the base servlet response of the pagecontext
except the outputstream ones:

The getOutputStream() returns not the default one of the first Response
but my own implementation but now the bug of Tomcat??:

	IncludeResponse iResponse = new
IncludeResponse((HttpServletResponse)pageContext.getResponse());
	RequestDispatcher rd=
pageContext.getServletContext().getRequestDispatcher(_sPage);
	rd.forward(pageContext.getRequest(), iResponse);

iResponse is my own implentation.
The RequestDispatcher interface wants only a ServletResponse (and a
ServletRequest)
It compiles fine so how is it possible that the RequestDispatcher throws a
ClassCastException???? inside itself?
What kind of ServletResponse does it wants?

Johan Compagner





> -----Original Message-----
> From: Johan Compagner [mailto:jcompagner@j-com.nl]
> Sent: Friday, January 05, 2001 4:05 PM
> To: Struts-User
> Subject: Template layout file with an include (bean or jsp) to a jsp
> with a form.
>
>
> Hi,
>
> I will ask the basic question first so that you don't have to
> read the whole
> email:
>
> So is there no way to use the same request over 2 or more jsp
> files that are
> nested in
> each other?
>
> Now my test case:
>
> I have a template jsp file that has all the layout of my site.
> in the right left corner will al the dynamic data.
>
> like this:
>
> // tag libs includes
> <form:html locale="true">
> <head>
> <title><bean:message key="index.title"/></title>
> </head>
> <body bgcolor="#fce8a0" topmargin="0" leftmargin="0">
>
> <table width="100%" border="0" height="123" cellpadding="0"
> cellspacing="0"
> bgcolor="#0066ff">
>    <tr>
>     <td>
> 	// standard layout of my site
> 	// jadajadajadajada
>     </td>
> 	// The include logic.
> 	<logic:present name="<%= JSP_INCLUDE_KEY %>" scope="request">
> 	<bean:include id="subpage" name="<%=
> (String)request.getAttribute(Constants.JSP_INCLUDE_KEY) %>"/>
> 	<%= subpage %>
> 	</logic:present>
> 	<logic:notPresent name="<%= Constants.JSP_INCLUDE_KEY %>"
> scope="request">
> 	<bean:include id="subpage" name="/index.jsp"/>
> 	<%= subpage %>
> 	</logic:notPresent>
>   </tr>
> </table>
> </body>
> </form:html>
>
> This template file is always the ActionForward that i return as the next
> page to see
> after this action, something in the perform method of a Action:
>
> 	request.setAttribute(Constants.JSP_INCLUDE_KEY,
> mapping.findForward("pensioen3a").getPath());
> 	return mapping.findForward("template");
>
> The nicest thing about this is that if i want to change something
> of my site
> i only have to edit
> one file and every page/view is changed also like the logon page or the
> pages with the forms.
>
> But now my problem.
>
> An Action is mostly triggerd by the subpage (where the form is in).
> That Action is testing the input (some validation) if that goes wrong that
> a ACtionError is made and is attached to the request.
> But when i forward it (to the same page) i forward it to the
> template again
> and that
> template includes the page with the form.
> But the include generates another request for that subpage so my
> ActionErrors object
> and other objects that i stored in the request aren't there!!
>
> I also do this in te validate() of a Form:
>
> 	String fromPage = request.getParameter("frompage");
> 	mapping.setInput(mapping.findForward("template").getPath());
> 	request.setAttribute(Constants.JSP_INCLUDE_KEY,
> mapping.findForward(fromPage).getPath());
>
> So that when there are errors returned it knows that the input is the
> template and the
> include is the same page it come's from.
>
> But this also doesn't work.
> I can't be the only one with this problem.
> Are nobody else using subpages where the form/inputs are in an another one
> that is the standard layout page?
>
> I thought to be smart and made my own include tag:
>
> public int doStartTag() throws JspException
> {
> 	try
> 	{
> 		RequestDispatcher rd=
> pageContext.getServletContext().getRequestDispatcher(_sPage);
> 		rd.forward(pageContext.getRequest(),
> pageContext.getResponse());
> 	} catch(Exception e)
> 	{
> 		throw new JspException(e.getMessage());
> 	}
> 	return SKIP_BODY;
> }
>
> But the problem is then that the subpage (the _sPage string) is displayed
> first, above
> the main page instead of in the main page. Why is this? Because
> when i walk
> through the code
> then i see the main page that writes to the output and then the
> subpage and
> then the main
> page again. I think this has something to do with flushing ect.
> But if i flush first (right above the RequestDispatcher line i do this:
>
>  pageContext.getOut().flush();
>
> Then i get the main page on top again but an error in the place where the
> subpage should be:
>
> 	Location: /postbank/template.jsp
> 	Internal Servlet Error:
>
> 	javax.servlet.ServletException: Cannot forward as
> OutputStream or Writer
> has already been 	obtained
>
> So is there no way to use the same request over 2 or more jsp
> files that are
> nested in
> each other?
>
> Johan
>
>
>
>
>
>
>
>