• Post Reply Bookmark Topic Watch Topic
  • New Topic

Your opinion on layout in JSP  RSS feed

 
Hans Klaren
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,

I'd like to have your opinion on what's the best way to accomplish this (without any dirty hacks).

The red part of the layout will stay the same in every page, the yellow part will change every time. (see attachment)

I thought of something like an masterpage.jsp which includes the yellow part and displays it on the page.
But when I do this, the GET and POST request are sent to the index.jsp and not to the included page.
(Im relatively new to JSP so I don't know if this is a stupid mistake from my side..)
I also don't know if this is the correct way to do this.

As this is a new project i'd like to keep it clean and not use dirty stuff.
Therefore my question is: I like to know the "official" way of accomplishing this.

Kind Regards,
Hans.
layout.jpg
[Thumbnail for layout.jpg]
Layout example
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

Do you have your jsp code? How are you including the content (yellow page)?

The GET and POST variables should be available to the content page as well if it has been included correctly.

Sean
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Do it the other way around: Have the actual page include the boilerplate code via an <%@ include %> or an <jsp:include>
 
Hans Klaren
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, thank you for the fast response,


Welcome to JavaRanch.

Do it the other way around: Have the actual page include the boilerplate code via an <%@ include %> or an <jsp:include>


Well actually thats what I don't want, because then you have to alter every page if you want to change something in your layout (correct me if I mis understood you)



Hey,

Do you have your jsp code? How are you including the content (yellow page)?

The GET and POST variables should be available to the content page as well if it has been included correctly.

Sean


This is my include div:



The content attiribute is set on the code behind page:


main.jsp is the included page.

If you want to see more please tell me.


Regards Hans.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well actually thats what I don't want, because then you have to alter every page if you want to change something in your layout (correct me if I mis understood you)

Why would you have to change every page? You'd change the included page, and then each page that includes it would pick up the changes automatically.

You might also want to look into a framework like SiteMesh if you have more involved layout needs.
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf makes a good point, it makes sense including your header/footer in each content page rather than doing the opposite.

<div id="content">
<jsp:include page='<%=((String)request.getAttribute("content"))%>' flush="true" />
</div>


Well firstly if you are looking for your code to be cleaner you should look into Expression Language (EL) for use in JSP rather than scriptlets.

I think from that it should be able to read the parameters.
How are you reading them in the main.jsp?

Sean
 
Hans Klaren
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Clark wrote:Ulf makes a good point, it makes sense including your header/footer in each content page rather than doing the opposite.

<div id="content">
<jsp:include page='<%=((String)request.getAttribute("content"))%>' flush="true" />
</div>


Well firstly if you are looking for your code to be cleaner you should look into Expression Language (EL) for use in JSP rather than scriptlets.

I think from that it should be able to read the parameters.
How are you reading them in the main.jsp?

Sean


Your right, actualy this is the only piece of scriplet i've got in my jsp files (because I didn't know any other way to do so). I read somewhere EL is sort of deprecated so I chose not to look what it was, but i'll give it a try now, thanks!


Ulf Dittmer wrote:Why would you have to change every page? You'd change the included page, and then each page that includes it would pick up the changes automatically.

You might also want to look into a framework like SiteMesh if you have more involved layout needs.

Well what you say is absolutely true, but it still is redundant code in my opinion, and I want to do it properly right from the start.. That's why I prefer 1 page containing the main layout, you get what I mean? thanks for your solution by the way, i'll look up the SiteMesh framework tomorrow.

I Also read about "Struts and Tiles" and "JSP Template" where JSP Templates acts like Ulf said. Anyone have any experience on using struts and tiles?

Thanks again for your response!

Regards Hans.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hans Klaren wrote:I read somewhere EL is sort of deprecated so I chose not to look what it was, but i'll give it a try now, thanks!


That's insane! The EL should be used exclusively in place of scriptlets.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find tag files very useful for creating pages with boiler-plate.
 
Hans Klaren
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Hans Klaren wrote:I read somewhere EL is sort of deprecated so I chose not to look what it was, but i'll give it a try now, thanks!


That's insane! The EL should be used exclusively in place of scriptlets.

oops :P ok didn't mean to hurt your feelings ^^,

I now read a bit about the EL and I all ready used it somewhere else in my page (never knowing it was EL..)
In my school I always learned the use of scriplets weren't bad, I always was against it. And im glad to know I'm not the only one.
(to be honest, every piece of code they provided me with is worth posing on thedailywtf.com...)

I also just downloaded SiteMesh and I'll look what that can do.
Thanks for the help you guys gave me! really appreciate it.

You'll probably hear again from me with different problems ;)

Kind regards Hans.
 
Hans Klaren
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oeh, all ready love sitemesh thanks again

Regards
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!