ASP.NET Master Pages equivalent in Java - java

What would be the Master Pages equivalent in the Java web development world? I've heard of Tiles, Tapestry and Velocity but don't know anything about them. Are they as easy to use as Master Pages?
I want something as easy as set up one template and subsequent pages derive from the template and override content regions, similar to Master Pages.
Any examples would be great!!

You should also check out Facelets; there is a good introductory article on DeveloperWorks.
The Facelets <ui:insert/> tag is comparable to the ASP.NET <asp:ContentPlaceHolder/> tag used in master pages; it lets you provide default content for that area of the page, but this can be overridden.
To fill the Facelets template in another page, you start with a <ui:composition/> element that points to the template file. This is roughly equivalent to declaring the MasterPageFile attribute in an ASP.NET page.
Inside the <ui:composition/> element, you use <ui:define/> elements to override the template defaults, similar to the way an <asp:Content/> tag is used. These elements can contain any kind of content - from simple strings to JSF elements.
So, to bring it all together...
In master.xhtml:
<!-- HTML header content here -->
<ui:insert name="AreaOne">Default content for AreaOne</ui:insert>
<ui:insert name="AreaTwo">Default content for AreaTwo</ui:insert>
<!-- HTML footer content here -->
In page.xhtml:
<ui:composition template="/WEB-INF/templates/master.xhtml">
<ui:define name="AreaOne">Here is some new content</ui:define>
<ui:define name="AreaTwo">
<p>Some new content here too</p>
</ui:define>
</ui:composition>
And this will render as:
<!-- HTML header content here -->
Here is some new content
<p>Some new content here too</p>
<!-- HTML footer content here -->
You also get some other benefits with Facelets, such as the ability to reuse page components with different data.
(Edited to provide more information.)

First, the equivalent of ASP.Net in Java is going to be a web framework, such as the ones you mention (Tiles, Tapestry and Velocity).
Master pages give the ability to define pages in terms of content slotted into a master template.
Master pages are a feature of ASP.Net (the .Net web framework), so you are looking for a feature similar to master pages in a Java web framework.
http://tiles.apache.org/framework/tutorial/basic/pages.html gives some basic examples using Tiles and JSP to implement something similar with Struts, a Java web framework. In this case, the Master Pages functionality is a plugin on top of Struts.
Velocity is a generic templating engine, not specialized for web pages and definitely more complicated than you need. (I've seen it used for code generation.)
Tapestry is more of a full featured web stack than Tile, and is probably good for your purposes. Its templating functionality involves creating a component and putting all common markup in that. An example is at http://www.infoq.com/articles/tapestry5-intro.
The specifics of this differ based on which Java web framework you choose.

I've used sitemesh in previous projects and it's pretty easy to set up. Essentially, you create decorators which are equivalents of master pages. You then define which child pages use which decorators. See introduction to sitemesh for more information.

Related

How to add image along with title name for all pages in that project in one code in Struts 2

I wrote the mentioned code
<link rel="Shortcut icon" href="img/favicon.ico"/>
in head section of JSP page and it is able to display the image along with the title name of the page
but what I want is it should be reflected on all my pages of project. So what should be done ?
Note: Made a favicn.ico and environment is Java Struts 2
Create a main page called for example baseLayout.jsp, which has a section for header, body, footer, toolbar, etc.
Each section you should include using Struts include tag or similar. In this page you have a head where you can put your link tag. As well as this page will be returned by every action, with exception to those actions that return redirect like results, you will have a link on every page.
This is the same concept that used in Tiles framework, where you can define a layout in the tiles config file and use tiles result type to return by the actions.
Another approach is to create a JSP file with only the link and include it on every page, but it's less productive as above solution.
If you are not familiar with Tiles framework you can read
this answer to get started.
I'm thinking of using simple features such as a side bar for navigation and banner across the top with the website name etc, and would like this to be carries across every page.
you are definitely needed tiles and tiles integration with Struts 2 via the tiles-2 plugin or tiles-3 plugin
there's plenty tutorials with examples on the web on these theme, for example
Struts 2 Tiles Integration Tutorial
Struts 2 Tutorial Series – How to insert Tiles to your project
Struts 2 Tiles Plugin Tutorial with Example in Eclipse
Struts 2 Tiles Framework Integration Tutorial Example
Struts2 Tiles Example
Working With Struts 2 Tiles, Struts 2 Tiles Example
I have read that to do this in JSP I have to use tag files (stored in the WEB-INF folder) then link to these on each JSP page?
Tags is a bit outdated, however in the JSP you should use struts tags
Also I have seen people creating these banners and sidebars etc in Photoshop, is this what normally goes on in web site development?
Yes, that banners, images, designed with it.
To sum it up: Creating a decent looking web page whilst using JSPs and the Struts 2 framework.

Can Jodd provide functionality like apache tiles framework?

I am using jodd in my project. I am using lots of functionality jodd like madvoc , petite. I am looking functionality like apache tiles framework I have seen and read the post http://jodd.org/doc/decora/ but I don't get to much. Can any one provide working example of decora.
Ok, this is not short question - so please be a bit more concrete here on StackOverflow :)
Decora is quite simple and somewhat different then Apache Tiles. It reminds on old, good SiteMesh. Now, let me explain how it works:
with your business logic, create an design-free html page. Generate the content right in the <body>.
Now, create a template with design, colors, images... Choose where you want your content to be injected; like inside certain div.
After you apply Decora, the body content from the page will be decorated with the template, by inserting it into this div.
This is how it works in a nutshell. You have working example in Uphea but just note that is a bit older version of Jodd used. Also, there is quickstart project, that has Decora configured.
Decora is more powerful then above - you may have more regions on one page, set the header tags like title and so on. You may use html tags for marking the regions, or JSP tags, etc.

Does Spring MVC 3.x support partial views (html/jsp) like that of ASP.NET MVC3/4?

Q1: Does Spring or any opensource java UI framework support partial views like that in asp.net mvc?
For example in my main index.html (or _layout.cshtm per asp.net mvc3 spec)
I would have the folllowing code:
<span id="logindisplay">#Html.Partial("_LogOnPartial")</span>
where #Html is a helper to display a partial view for _LogonPartial.cshtml which just injected it's html view contents into the page?
Q2: If this is supposed If I want to display a bunch of partial views it would be helpful to display them concurrently in parallel to improve performance. Very similar to what linkedin is doing using dust and fizzy? http://engineering.linkedin.com/profile/engineering-new-linkedin-profile
Q3: Is fizzy available as open source like that of dust?
If you want to include content of a page into another page, by adding some code on the page itself, you should compare asp with jsp, not ASP.NET MVC* with JEE - Spring MVC
So, an equivalent to <span id="logindisplay">#Html.Partial("_LogOnPartial")</span> on a jsp would be one / all of the following
On your jsp, include content from another jsp using <%# include file="../includes/inner-content.jsp" %>. This is what is called a static include. The source of the included jsp is included and made part of the parent jsp, before the jsp is compiled. If you use an IDE, it will check to ensure the included jsp does infact exist at the path specified, relative to the location of the jsp in which you are adding the include. Technically this is a JSP Directive. The jsp being included could just be a fragment, and not addressable from the outside world (could be hidden inside WEB-INF)
You can also use what's called a Dynamic include <jsp:include page="someJSP.jsp" />. In this case, the included JSP should be addressable from the browser and should be capable of being rendered independently. When the server is executing the servlet to render the parent JSP, it stops when this tag is seen, and starts executing the servlet for the included jsp, the output obtained from the inner jsp execution is then merged to the output of the parent jsp, and processing of the parent jsp is resumed.
A third option would be to use Core JSTL taglib's <c:import url=""/>. This works just like option 2 above, except it also allows you to import a page / content from a url that lives outside your application. Basically you can mention a path to a jsp, or relative URI to a servlet mapping in your application, or a URL to an external page.
Now, I suspect this is not really what you want to do, if you are comparing with what Linkedin is doing. You want to mashup content from sources in your own application, and compose your page. You also want to do this in an asynch manner so as to keep load times in check. In this case, you HAVE to use JavaScript and Ajax. All the mechanisms described above are for server rendered pages (All HTML is created before the page is rendered in the browser). Just like #HTML. You need to create a simple framework / use an existing one, where once a page loads, it fires asynch ajax calls to the server to get content for specific areas on the page and renders the returned HTML in those specific areas.
Hope this helps.
Do let me know if I've misunderstood your question.

Spring MVC. Rendering layout

I'am new to Java Spring MVC. Official documentation is very poor and i've got a lot of questions about rendering pages in Spring.
I have page and top block with menu. This menu must be dynamic and must be displayed on all pages. In PHP frameworks this is very common and very easy task. But Spring makes my brain explode.. How can i separate rendering of this menu in some peace of logic and tell spring to render it all the time? For example in JSP. Shoud I use beans or other stuff?
Thanks for your answers!
Official documentation is very poor
No it isn't. Its the one place I always refer back to.
I use a jsp include and yes pass a java bean to the jsp from the controller. The included jsp will be cached the first time it is included. In other places I connect to a controller using jquery and ajax, which dynamically updates the page.
I'd suggest the official tutorials, and krams as good starting points.
You won't find much information about this in the Spring documentation because it's out of the scope of Spring.
You can use JSP includes just as you would do PHP includes. This will however force you to repeat the page layout and the include in each and every page of the application :
<jsp:include page="/menu.jspf" />
Or use a templating engine, like Sitemesh or Apache Tiles (see http://static.springsource.org/spring/docs/3.1.0.RC1/spring-framework-reference/html/view.html#view-tiles for Tiles integration in Spring).
The easiest way would be to use <jsp:include page="/menu.jsp" /> where "/menu.jsp" is a path to your menu jsp.
There are other solutions like using tiles, but <jsp:include> should do.

Is there something like ASP.NET web user controls in Java/struts/jsp

In java/struts/jsp world, is there something like the ASP.NET custom user control, a piece of UI with back end code that performs a specific logic, and can be pulled and used in another place (for example something like the login control).
The current Java EE API offers JSF 2.0 on Facelets for this. Note that Struts(2) is actually a competitor to JSF and that JSP is a view technology like ASP, not a MVC framework. As per Java EE 6 / JSF 2.0, JSP is been replaced by Facelets as the default view technology.
On top of the basic JSF implementation you can choose from a lot of "rich" component libraries which adds an extra CSS and Ajax sausage, for example PrimeFaces (showcase) and OpenFaces (showcase).
See also:
What is the mainstream Java alternative to ASP.NET?
What is the difference between JSP, Servlet and JSF?
Java EE 6 tutorial - chapters 4-10
Coreservlets.com JSF 2.0 tutorial
What you are after is called tag files in JSP. I found that they are not as easy to learn and use as .NET user controls.
You can use them similarly to user controls .NET.
workflow is like this.
Create a tag file in web-inf/tags/AtillaTagLibrary/DropDownList.tag
reference your tag file in your jsp file like below
<%# taglib prefix="ct" tagdir="/WEB-INF/tags/AtillaTagLibrary"%>
Use your tag file like normal jsp tag.
<ct:DropDownList />
This tag files can get attribute values from outside like user controls.

Categories

Resources