Embedding Tag Files in a JAR - jsp-tags

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.

Related

Facelets equivalent of <jsp-file> servlet mapping

Note - This question might have been answered earlier but I am not able to find any note on this. hence asking!
Background - I am working on a legacy JSF application which uses JSP as view technology. Now since we have decided to move to JSF 2.2/2.3, we are also changing the JSP pages to facelets.
Issue - In the web.xml, we have following mapping -
<servlet>
<servlet-name>dummyframe</servlet-name>
<jsp-file>/WEB-INF/dummyframe.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>dummyframe</servlet-name>
<url-pattern>dummyframe</url-pattern>
<servlet-mapping>
We have converted jsp file to facelet file but not sure how to handle this jsp-file mapping.
We are planning to write java classes which will redirect to facelet page. In this case, the mapping will be -
<servlet>
<servlet-name>dummyframe</servlet-name>
<servlet-class>xxx.xxxx.dummyframe</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dummyframe</servlet-name>
<url-pattern>dummyframe</url-pattern>
<servlet-mapping>
The questions I have -
1) Is this a good alternative?
2) Is there any other alternative available without writing java classes?
1) Is this a good alternative?
Yes, but I think it would be better to implement redirection in a filter instead of a servlet class.
2) Is there any other alternative available without writing java
classes?
Leave dummy servlet mapping in web,xml as is and put into /WEB-INF/dummyframe.jsp this:
<html>
<head>
<meta http-equiv="Refresh" content="0; URL=mynewdummyfile.jsf">
</head>
</html>
or this:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<body>
<c:redirect url="mynewdummyfile.jsf"/>
</body>
</html>
or this:
<%# page import = "java.io.*,java.util.*,javax.servlet.http.HttpServletResponse" %>
<html>
<body>
<%
response.sendRedirect("mynewdummyfile.jsf");
%>
</body>
</html>
or this:
<%# page import = "java.io.*,java.util.*,javax.servlet.http.HttpServletResponse" %>
<html>
<body>
<%
response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
response.setHeader("Location", "mynewdummyfile.jsf");
%>
</body>
</html>
or this:
<%# page import = "javax.servlet.ServletContext" %>
<html>
<body>
<%
ServletContext sc = getServletContext();
sc.getRequestDispatcher("/WEB-INF/mynewdummyfile.jsf").forward(request, response);
%>
</body>
</html>

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>

Can we include angularjs component in Facelets page

I am new to angularjs, and was trying to create a sample angularjs in a Facelets file. But I am getting an error in the line <html ng-app> in Eclipse IDE. The error specifies that the ng-app attribute should be followed by an = character. Is it not possible to include angularjs code in a Facelets XHTML file?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:ng="http://angularjs.org" ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
</head>
<body>
<div>
<label>Name:</label>
<input type="text" ng-model="yourName" placeholder="Enter a name here">
<hr>
<h1>Hello {{yourName}}!</h1>
</div>
</body>
</html>
Take a look at following:
Try angular faces: https://github.com/stephanrauh/AngularFaces
And there is also a project: https://github.com/pankajtandon/PointyPatient
Prime faces refer to: http://angularprime.appspot.com/#/main
The best way is to go with HTML & JS as Client, and JAX-RS on Server Side.
I had similar problems, with Primefaces and Bootstrap, and became convinced, that JS & HTML at the moment is better for client side development.
Give the attribute the ng-app.
The value of the attribute would be the same as the name of variable that contains the return value of angular.module().
for ex:
ng-app="SampleApp"
in the html page
var SampleApp = angular.module("SampleApp", ["ngResource"]).
config(
//your code
);
in the app.js file
I think this should work for you.
The logic is that, when you define the ng-app it gives the reference to the particular module of app.js that would call the relevant controllers.
There is an app.js file in angularjs. This contains all the controllers define. These controllers act as connecters and data pipeliners between the html(our view) and the .cs(mvc controller) files.
These controllers are defined in a module (as i have defined one in the above answer).
There can be numerous modules in app.js. and hence they wil have numerous controllers.
ng-app is a directive that contains the name of the module you want to use as attribute.
ng-app defined will be active till the closing tag of the tag defined. That means if you have defined ng-app in html tag, it will work till the html tag closes, if in a div, it will work for that division.
If you dont get anything, google it, or refer the angularjs documentation, or shoot me a question.!!
I hope you got something out of it.:-)
Stumbled on this while searching myself.
XHTML does not allow an attribute without a value. While you can ignore this in Eclipse, the latest JSF libraries blow up and you'll get an exception.
Specify a value, which corresponds to your module name. For example, if you initialize your angular module with:
var myAppModule = angular.module('myApp', []);
Then you need to specify:
<html ng-app="myApp">
And all should be good minus some warnings in eclipse for not recognizing "ng-app". Works for me. Also, you probably want the HTML 5 doc type instead of the strict you have. Try:
<!DOCTYPE html>
<html ng-app="myApp">

Making JSF website multilingual

Trying to make JSF web application multilingual. For that purpose cover all the HTML code with <f:view> tags:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns= ... >
<f:view locale="#{actionsContainer.languageDetails.locale}">
<head>
<meta charset="utf-8" />
<f:loadBundle basename="messages.Messages" var="key" />
</head>
<body>
<h:outputText value="#{key.myText}" />
</body>
</f:view>
</html>
Than in resources I have several 'Messages.properties' files with a translations to some languages:
Messages_en.properties
Messages_es.properties
Messages.properties // default
The sample content of this files 'Messages_es.properties' is:
myText=España
Let's take, my locale is "es", and than Spanish translation file loads it's value and renders to the screen. However, the special characters (eg 'ñ') doesn't display properly in a browser output. Instead of special Spanish letters I am getting something like this "ó".
I have tried to save .properties file with UTF-8 encoding, without BOM. And the output has been changed to something like this "".
And so the question is how do I get this special any language letters in an output?!
Properties files are by default read using the ISO-8859-1 encoding. You need the JDK native2ascii tool to convert UTF-8 properties files to ISO-8859-1 properties files and then use those instead.
In JSF you can however also specify a custom ResourceBundle with a Control wherein you overridde the reading of properties files to use UTF-8 instead. See also this article.
when working with resource bundles, consider using ResourceBundle Editor plugin for eclipse (http://sourceforge.net/projects/eclipse-rbe/).

How to solve this problem with JSP tag extension?

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.

Resources