Is it possible to inherit taglibs or imports from parent JSPs in their "descendants"?
Let me show you an example
header.jsp
<%# page contentType="text/html" isELIgnored="false"
import="org.something.utils.Constants"%>
//some code, Constants class is available here
index.jsp
<jsp:include page="template/header.jsp" />
//Constants is not available, I get a JasperException: Unable to compile class for JSP
also the taglibs inheritance doesn't seem to work. So is there a way how to make this work?
Taglibs and imports are not inherited, and everything in a tag as well cannot be inherited or passed through pages (except for JspContext and request attributes).
You have two options here:
Make the import in every JSP you have.
Make the common classes and libraries Global ones, this depends on the IDE and the server you are running.
Edit
Defining JSP implicit includes:
For Netbeans http://docs.oracle.com/cd/E19575-01/819-3669/bnajl/index.html
A global tutorial http://sabahmyrsh.blogspot.com/2009/06/jsp-defining-implicit-includes.html
Related
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.
I'm building a taglib with a few custom tags. Some of these require a bit of JavaScript to work properly. What would be the best way to add the JavaScript so that it's included in the taglib jar?
The most obvious approach to me would be to put it into the custom tags' doStartTag() method like this pageContext.getOut().print("<script>my js</script>");, which I don't like for 2 reasons: It looks absolutely horrible and it gets added every time the tag gets used on a page.
I'm looking for a solution that lets me keep the JavaScript in a separate .js file that rests somewhere in the taglib jar and gets included only once per page whenever a custom tag is used.
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.
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)
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.