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>
Related
I'm looking for alternatives to Jade templates in express 4.x because I really don't like Jade's syntax. I'm tending towards EJS, because it's basically just HTML on steroids.
However, one really nice feature of Jade templates is the ability to use layouts. I've found https://www.npmjs.org/package/express-ejs-layouts, but it seems to be made for express 3 and its build is failing :/.
I also found https://www.npmjs.org/package/ejs-mate which is made for express 4.x but it only seems to support a single content block (body).
I would like to have something like this:
layout.something:
<html>
<head>
<% block styles %>
<% block scripts %>
</head>
<body>
<% block body %>
</body>
</html>
index.html:
uses layout "layout.somehing"
scripts:
<script src="my_custom_script.js"></script>
styles:
<link rel="stylesheet ...></link>
body:
<h1>This is my body!</h1>
So that this yields:
<html>
<head>
<link rel="stylesheet ...></link>
<script src="my_custom_script.js"></script>
</head>
<body>
<h1>This is my body!</h1>
</body>
</html>
Does anyone know an engine that is capable of that besides Jade?
You can try express-handlebars, it supports layout and partial views.
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>
I have a jsf project
and my pages are all referencing a template.xhtml
currently i have the following in the web.xml
<welcome-file-list>
<welcome-file>/pages/home/index.xhtml</welcome-file>
</welcome-file-list>
when i try to access the root directory now it shows me some content from that page as jsf code (not rendered) but everything from within the template vanished as well. It just shows the title and 2 lines of code.
All help appreciated
Check the url mapping for the Faces Servlet in your web.xml file, it's probably wrong.
A good one should be like the one at the JSF tag info page:
<servlet>
<servlet-name>facesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
solved it
added the following in the web.xml
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
and created the following index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="refresh" content="0; faces/pages/home/index.xhtml">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
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.
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.