how to create a template with jsp and servlet - java

let's say I have a site with 10 pages. Every page is called by a servlet. For example, I have the servlet "index" that makes a forward() to "index.jsp".
In my index.jsp i have 2 includes for the header and the footer
...
<jsp:include page="header.jsp" >
home page text
<jsp:include page="footer.jsp" >
...
Now I have 10 pages similar to the index page, I mean I have 10 pages that include a header and a footer.
Let's say I decide to delete the footer: I should edit 10 pages.
What im wondering is if there's something that allow me to use just one page, and show, dinamically, just the "content" of the page (home page, contacts, ecc), keeping in mind that i use a servlet to get every page content (with a forward()).

what you need is Apache Tiles Framework,
Tiles allows authors to define page fragments which can be assembled into a complete pages at runtime. These fragments, or tiles, can be used as simple includes in order to reduce the duplication of common page elements or embedded within other tiles to develop a series of reusable templates. These templates streamline the development of a consistent look and feel across an entire application.
It includes configuration file so you can edit in one file alone , so that the changes will be reflected in common for all jsp files
A nice Startup tutorial here

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.

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.

Regular HTML pages on Struts 2 Spring Framework

I personally do not have much experience working in jsp and struts2 tomcat server but I have a small task that I have to deal with a server in production. I looked at info on the internet for two nights but seems like i can't get the answer.
What I want to do is to open some static simple html pages pop-up from the jsp pages that already exist. Those jsp pages are currently located at /webapps/WEB-INF/tld/mainPage/indexMainPage.jsp
what I did was:
make some javascript functions so that when they click a button, i open a pop-up
function btn_openPopUp()
{
window.open("../popUp/test.html");
}
and put the test.html page at /webapps/WEB-INF/tld/popUp/test.html
The pop-up part works fine as it is just simplye javascript, but test.html is not displayed.
Struts Problem Report
Struts has detected an unhandled exception:
Messages:
There is no Action mapped for namespace / and action name test.
I have been looking at info online, and everything about struts is action based. Is there anyway I can open a page like on a regular server?
Pages under WEB-INF are not directly accessible to clients; put such pages under the root somewhere.
They can still be in sub-directories if it makes sense, just not under WEB-INF. If they must be under WEB-INF for some reason (which makes no sense for non-JSP pages) they can be streamed through an action like any other file--bit that'd be weird for HTML pages.

CSS design templates

I'm about to develop a j2ee web application . I need to know , how can I have the different designs (layout of CSS) for jsp pages . Say If I send the same data always but I want to present that data in different web designs ( web page designs) .
So that I can navigate through the designs more flexibly and choose the best one for my applications.
My need is , with out changing the content related to design in jsp page , (like classname's , id 's related to CSS for different textboxes and lables.. etc) , instead I'll change only one attribute in my application so that whole design would change.
Can any one suggest where can I find these sets of web layouts (CSS layouts).
What you're looking for then is the 960.gs grid system. :)
It provides.. "..a streamlined web development workflow by providing commonly used dimensions..". which is what you have asked for in your question.
I agree with another poster about having separate CSS Style Sheets (external style sheets).
Have you checked out CSS Zen Garden? There's probably more than a hundred different web pages that all use the same HTML, but changed the CSS & image files only. That's what opened my eyes to what CSS can do.
If i understand your question correctly, one way to accomplish changing layout without changing classes and ids is to have separate stylesheets for each layout.
You can then select the desired stylesheet in the header of the html file being served.
i.e
layout 1:
<link rel="stylesheet" type="text/css" href="layoutOne.css" />
layout 2:
<link rel="stylesheet" type="text/css" href="layoutTwo.css" />
Finally I reached below link . It sounds great
http://www.oswd.org - You can download from this site many number of designs.
I agree the answer on zengarden , here is the URL http://www.mezzoblue.com/zengarden/alldesigns/

ASP.NET Master Pages equivalent in 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.

Categories

Resources