How to solve this problem with JSP tag extension? - jsp-tags

Begining with JSP and servlet development, I have some problems with a bodyless custom tag to be inserted in a JSP page.
Steps done:
Wrote and compiled successfully a CustomTag.java (extending TagSupport) in WEB-INF/classes directory;
Defined the TLD file, with a very simple example, including <body-content> with an empty value for a bodyless tag;
Used the tag in a JSP page with taglib directive pointing to my /WEB-INF/tlds/site.tld file.
With all this in mind, do you have a clue why Tomcat is sending an error like this:
CustomTag cannot be resolved to a type
Thanks in advance for your answers, and please ask if you need more details.
Here's my TLD file:
<?xml version="1.0" encoding="ISO-8859-1"?>
< ! DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>customlib</short-name>
<description>Custom library.</description>
<tag>
<name>header</name>
<tag-class>HeaderTag</tag-class>
<body-content>empty</body-content>
<description>...</description>
</tag>
</taglib>
The JSP file:
<%# page contentType="text/html; charset=UTF-8" language="java" import="java.sql.*" errorPage="" %>
<%# taglib uri="/WEB-INF/tlds/customlib.tld" prefix="clib" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>title</title>
</head>
<body>
<clib:header />
</body>
</html>
The HeaderTag class:
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
public class HeaderTag extends TagSupport {
public int doEndTag() throws JspTagException {
try {
pageContext.getOut().print("<p>header</p>");
}
catch (IOException e) {
throw new JspTagException("Error.");
}
return EVAL_PAGE;
}
}

You've rebuilt and redeployed, correct? In that case my best guess is that you left out the <tag-class> directive in the TLD file.
<tag>
<name>cookieIterator</name>
<tag-class>util.infoTemplates.CookieIterator</tag-class>
<body-content>JSP</body-content>
</tag>
If that isn't the cause, please post your TLD file and an example JSP.
Edit: All tag classes must have a package. Per the JSP 2.0 spec (section JSP 11.2):
As of JSP 2.0, it is illegal to refer to any classes from the unnamed (a.k.a.
default) package.

Related

No warning or error on missing bean in tomcat

I have a very simple "dynamic web application" which results in a war-file. I (deliberately) used the name of a non-existing bean (for experimentation and learning purpose). When deployed to tomcat I can see the page in firefox, but I get no error or warning neither in catalina.out nor in catalina*.log.
What is wrong, where can I see such warnings?
I'm using java 8, tomcat 8, eclipse 4.5.1 (mars), jsf-api.2.2.8.jar and jsf-impl.2.2.8.jar.
I don't have any java code of my own (yet), especially no beans.
The index.xhtml that is used (and, aside of the missing bean value) displayed correctly is:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
<ins>This is some other xhtml</ins>
<h:commandButton value="A#{missingBean.name}Z" action="#{missingBean.doAction}"></h:commandButton>
<h:messages></h:messages>
</body>
</html>
EDIT:
thanks to #Kukeltje I added
<context-param>
<param-name>javax.faces.DEVELOPMENT_STAGE
</param-name>
<param-value>Development</param-value>
</context-param>
to my web.xml file.
This results in an
HTTP Status 500 - javax.el.PropertyNotFoundException: /index.xhtml #13,81 action="#{missingBean.doAction}": Target Unreachable, identifier 'missingBean' resolved to null.
This solved the original problem that I didn't get errors nor even warnings on an obvious (and deliberate) coding error.
Background information: I try to learn jsf and did some experiments based on a tutorials-point tutorial. Having messed that up by trying far too many things in one go, I resolved to start a new eclipse project from scratch, going very tiny step by step, to see not only the result (as would be by blindly doing copy&paste), but also what errors I'll see if I neglect something on the way.

OpenCms tags in OpenCms tags

I know this may be a silly question but I ma trying to understand the jsp templates in opencms.
Even though we have htm tags in jsp. what is the actual use of cms tags such as :
<cms:template element="body">
<cms:include element="body" />
As described in this wiki page you can define your template parts in a jsp file through cms tag cms:template and then include them in your jsp page through cms tag cms:include
The cms:template tag
With the tag, you can add control structures to the template which allows it to deal with multiple page elements.
The cms:include tag
This tag is used to include files from the OpenCms VFS dynamically at runtime. The included file is treated like a request with optional additional request parameters.
There are different options to determine the name of the included file by using one of the following attributes:
- page
- property
- attribute
If none of these attributes has been set, the body of the tag is evaluated and the result is used as filename.
<%# page session="false" %>
<%# taglib prefix="cms" uri="http://www.opencms.org/taglib/cms" %>
<cms:template element="head">
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title><cms:property name="title" escapeHtml="true" /></title>
<meta HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; CHARSET=<cms:property name="content-encoding" default="ISO-8859-1" />">
<link type="text/css" rel="stylesheet" href="<cms:link>../resources/mystyle.css</cms:link>">
<cms:editable />
</head>
<body>
<h2>My first template head</h2>
<!-- Main page body starts here -->
</cms:template>
<cms:template element="body">
<h2>This is the first page element:</h2>
<cms:include element="body" editable="true"/>
<cms:template ifexists="body2">
<h2>This is the second page element:</h2>
<cms:include element="body2" editable= "true"/>
</cms:template>
</cms:template>
<cms:template element="foot">
<!-- Main page body ends here -->
<h2>My first template foot</h2>
</body>
</html>
</cms:template>

Using native hindi characters in a web site's source

I have made a sample webpage in hindi. The code is:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
हिन्दिमेबदलना
</body>
</html>
But I want the source code to appear in hindi as well (just like the BBC hindi website) and not in unicode. I mean ह instead of ह
How can I do this?
You can get Devanagari characters in your HTML source by not using HTML entities when creating the file. The following lines are equivalent.
<p>अ आ इ ई</p>
<p>अ आ इ ई</p>
If you are generating your HTML from a database, you might be applying an HTML entity conversion function at the time of generating the markup (such as htmlentities() in PHP). You'll have to remove that function call or apply it selectively.

Embedding Tag Files in a JAR

Is it possible to do this? What I'm trying to accomplish here is the creation of an extensible Struts 2 plugin with customizable screens to avoid code duplication in similar projects.
Yes, it is possible, but it has nothing to do with Struts 2:
http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPTags6.html#wp90207 (under "Packaged Tag Files").
Here is an example: http://www.examulator.com/moodle/mod/resource/view.php?id=473
Quoting steps from this source(taken from prev. answer) in case URL changes. (Simplest solution I found on the internet)
When wrapped in a jar (Java archive) tag files require a tld.
menu.tld placed in the META-INF directory.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>menutagfile</shortname>
<uri>www.examulator.com/menutagfile</uri>
<tag-file>
<name>menu</name>
<path>/META-INF/tags/menu.tag</path>
</tag-file>
</taglib>
menu.tag placed in the META-INF\tags directory.
<%# tag body-content="tagdependent" %>
<%# attribute name="menutext" rtexprvalue="true"%>
<h1>This is my tag file</h1>
<jsp:doBody/>
The command to package these into JAR(Auto package in case of maven)
Jar cvf menutagfile.jar .\META-INF\*.*
Usage in the parent project
<%# taglib prefix="mytagfile" uri="www.examulator.com/menutagfile" %>
<html>
<head>
<title>Demonstration of Tag Files</title>
</head>
<body>
<h1> What is going down? </h1>
<mytagfile:menu/>
</body>
</html>
Note: In case you do not have a META-INF folder in your project, create one inside src/main/resources.

How to make a meta tag the first one in the <head> section?

I'm using JSF2, GlassFish 3.1, PrimeFaces 2.x.
I'm having strange rendering problems on IE9. I'm supposed to be able to force IE9 to render as IE9 by inserting the following:
<html>
<head>
<!-- Enable IE9 Standards mode -->
<meta http-equiv="X-UA-Compatible" content="IE=9" />
...
But the thing is, it's not working because (I'm told) the meta tag MUST be the first tag in the section.
When I do this in my XHTML file ...
<html ...>
<f:view contentType="text/html" locale="#{loginHandler.currentLocale}">
<h:head>
<!-- Enable IE9 Standards mode -->
<meta http-equiv="X-UA-Compatible" content="IE=9" />
The resulting HTML looks like this, where JSF/PrimeFaces has inserted a bunch of "link" and "script" tags before my new meta tag.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link type="text/css" rel="stylesheet" href="/orcf-webui/javax.faces.resource/jquery/ui/jquery-ui.css.jsf?ln=primefaces&v=2.2" />
<link type="text/css" rel="stylesheet" href="/orcf-webui/javax.faces.resource/wijmo/wijmo.css.jsf?ln=primefaces&v=2.2" />
<script type="text/javascript" src="/orcf-webui/javax.faces.resource/jquery/jquery.js.jsf?ln=primefaces&v=2.2"></script>
<script type="text/javascript" src="/orcf-webui/javax.faces.resource/jquery/ui/jquery-ui.js.jsf?ln=primefaces&v=2.2"></script>
<!-- Enable IE9 Standards mode -->
<meta http-equiv="X-UA-Compatible" content="IE=9" />
Is there any way to get my meta tag in the right place so it will work? (Or an alternative way to make this IE9 problem go away?
The meta tag must go before all PrimeFaces stuff:
http://blogs.msdn.com/b/cjacks/archive/2012/02/29/using-x-ua-compatible-to-create-durable-enterprise-web-applications.aspx
HTTP Header and HTML HEAD are completly different things.
In PrimeFaces 3.0 the new facet was added to h:head:
http://blog.primefaces.org/?p=1433
So the solution would be:
<h:head>
<f:facet name="first">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
</f:facet>
</h:head>
I think best solution is to create JSF PhaseListener which adds X-UA-Compatible header to HTTP response
public class UACompatibleHeaderPhaseListener implements PhaseListener {
private static final long serialVersionUID = 1L;
#Override
public PhaseId getPhaseId() {
return PhaseId.RENDER_RESPONSE;
}
#Override
public void beforePhase(PhaseEvent event) {
final FacesContext facesContext = event.getFacesContext();
final HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
response.addHeader("X-UA-Compatible", "IE=edge");
}
#Override
public void afterPhase(PhaseEvent event) {
}
}
and register it in faces-config.xml
<faces-config xmlns="http://java.sun.com/xml/ns/javaee" version="2.0">
<lifecycle>
<phase-listener>com.example.UACompatibleHeaderPhaseListener</phase-listener>
</lifecycle>
</faces-config>
Another option would be to create servlet Filter and register it in web.xml.
Why is this needed?
Imagine your web application is deployed on a domain (or sub-domain) which is on IE compatibility list here: http://ie9cvlist.ie.microsoft.com/ie9CompatViewList.xml so you need to use X-UA-Compatible header to switch IE back to latest mode.
Imagine your web application is deployed on WebLogic server (which uses mojarra 2.0.4) so you cannot change JSF implementation.
mojarra
You can create Filter which adds header:
X-UA-Compatible: IE=9
to response object.
Source
Just to comment on your answer and previous comments:
HTTP Header and HTML Head are not completly different things (effectively) if you view page on IE8 as shown by diagram here. If you set HTTP header, but not HTML Head, the directive from HTTP header is still taken into account.
I don't know how IE9 behaves, but I guess that in a similar way.
You might want to switch from Mojarra to MyFaces. Looking at the source code of MyFaces' HEAD renderer - first gets rendered the content of the element and then other resources. Mojarra is doing this probably other way around. If you don't want to switch JSF implementations you can just implement your own HEAD element renderer.
However I would suggest just to find out why IE9 is not working without the X-UA-Compatible meta tag. It is supposed to make newer versions to behave like older versions.

Resources