JSP Pagetemplate layout - java

I noticed that on JSF pages i can include templates in my xhtml page like
<ui:composition template="/WEB-INF/tmp/tmp.xhtml">
is there any alternative to this using JSP Pages+ Hibernate?

With plain JSP you can only reuse content with instructions like:
<%# include file="banner.jspf" %>
JSF facelets allow passing parameters to the facelet to do this with JSP you would need an additional framework like struts.

Related

How should I include other jsp files in a jsp file and why?

So there seems to be a few ways to include jsp files in jsp files, being:
<%# include file="header.jsp" %>
<jsp:include page="header.jsp" />
<c:import url="header.jsp" />
<tagfiles:tagfile />
So which one should I use and why? What advantages / disadvantages do they come with?
The include directive, makes a copy of the included page and copies it into a JSP page (the "including page") during translation. This is known as a static include (or translate-time include) and uses the following syntax:
<%# include file="/jsp/userinfopage.jsp" %>
Two alternatives exist for the dynamic include
The jsp:include tag, described in "Standard Actions: JSP Tags", dynamically includes output from the included page within the output of the including page during execution. This is known as a dynamic include (or runtime include) and uses the following syntax:
<jsp:include page="/jsp/userinfopage.jsp" flush="true" />
<c:import url="header.jsp" />
Unlike jsp:include, the c:import action provides a mechanism to access resources that can be specified through a URL, thus allowing page authors to get access to resources that reside outside the Web application. On the other hand, it lacks the ability to flush the response. Finally, c:import is comparatively more heavyweight and is therefore not appropriate when a lightweight solution is sought.
tagfiles are basically templates, which are like generic and can render some common views, but internally they will themselves use html tags itself.but not much of use while including jsp pages.
In my Project, I have used following approach
<jsp:include page="header.jsp" />
I have used this for loading specific div element instead of refreshing whole page.
This can be done by using JQuery's load method.
Including JSP file allows us to reuse the template in many places. Just write template code in JSP file and use it wherever required.
JSP page directives works at translation time while standerd actions works at run time.
You can tagfiles for calling functions on server side. You can also use tagfiles for creation of templates.

Are .tld necessary to run sitemesh?

I am running simple JSP pages (in a Spring 3.1 web app) decorated with sitemesh, but without including sitemesh-decorator.tld and sitemesh-page.tld explicitely in my project. It works.
Yet, I see sample projects explicitely including these files in a \WEB-INF\tld folder. Is this necessary? If yes what for?
For example, one project has a generic taglibs.jsp page imported in all pages. It includes:
...
<%# taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
...
For the records
There is not point into inserting:
<%# taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
in a JSP page to be processed by sitemesh. This include should only appear in decorator pages.
There is no need to include these .tld(s) into your applicataion \WEB-INF\tld. They can be loaded directly from sitemesh.jar/META-INF directory.

Spring mvc layout without tiles

In Spring Mvc, beside using tiles, is there any way to decide the jsp layout. I mean think of a design with side menu panel and if I want to apeear each jsp for a relavent side menu, how can I do that without using tiles.
Thanks
You could crate sepatare jsp for each part of your page layout: header, footer, menu, etc.
And use:
<%# include file="yourPageSection.jsp"%>
to include common parts in your pages.
You can include the html or jsp design using the below.....
It works same as the master page of asp.Net......
<%# include file="page.jsp"%>
<%# include file="page.html"%>
Sitemesh is another framework for jsp page layout: http://wiki.sitemesh.org/display/sitemesh/Home

Extended JSP taglib

Is there any extended JSP taglib available? Basically, because of project constraints, we're using a home-grown framework and working with JSPs. We want a taglib that will help us work easily with html forms and form elements and provide some sort of binding. Something like struts html tablib. I'm not sure if we can use the struts taglib standalone?
The best solution I can provide for you is to use a retired tag library from jakarta.
It still works but it is no longer supported or maintained by them.
The link can be found here
The one you will be interested in is the "input" tag library which has tags for the following fields:
form
text
password
textarea
hidden
select
option
radio
checkbox
There should be no problem using Struts with appropriate JAR files in Web App lib folders (put all the Struts JARs in there)

Using a function from one jsp in another jsp

I have a jsp file were the db connection is created and queries can be executed.
I want to call these functions (of connecting to a database and executing query) from another jsp simply to add new record to a database and to stay on a same page.
I read here about an "import" tag but i don't have any packages. Also i'm not allowed to use JSTL.
Little help?
Thanks for considering my question.
You can use <%#include %> directive to statically include JSP fragments.
<%#include file="db.jsp" %>
Writing JSPs this way is however considered poor practice. Java code belongs in a Java class, not in a JSP file. You could for example use a preprocessing servlet class wherein you do the job in doGet() method and have a reuseable DAO class wherein you hide all the JDBC boilerplate code away.
You shouldn't program in your jsp. Put that connection logic out into a class and use that in your jsps.
Afterwards you can import your class with:
<%# page import="package.yourclass" %>
In general you should consider using frameworks like Spring MVC or Struts. Using JSF could be another option.
Mainly we used to import two types of pages into a jsp page,
Java pages
JSP pages
If you want to import a java page into your jsp page, syntax is
<%# page import="package_name.java_page" %>
If you want to import a jsp page, syntax is
<%# include file='jsp_page_name.jsp'%>
while adding a jsp page, if it is in an another folder, you just need to specify the paths also.

Categories

Resources