Do I have to include libs in every tile within Apache Tiles? - java

I am using Tiles within my web-application. I have a standard-layout (standard.jsp) within the tiles are used. On top of the standard.jsp are a lot of includes, concerning tag-libraries and such.
Let's do a simplified example.
standard.jsp:
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%# include file="/WEB-INF/jsp/includes/include.jsp" %>
<html>
<head>
<tiles:insertAttribute name="head" flush="false"/>
</head>
<body>
<tiles:insertAttribute name="body" flush="false"/>
</body>
</html>
body.jsp:
<div id="body-div">
<p>Hello, <c:out value="${forname}" />!</p>
</div>
This prints:
Hello, !
In the tiles I would like to use the tags, but it's not working. It only works, if I add the includes to the tile-jsp.
body.jsp with includes:
<%# include file="/WEB-INF/jsp/includes/include.jsp" %>
<div id="body-div">
<p>Hello, <c:out value="${forname}" />!</p>
</div>
This prints:
Hello, John!
Is there a better way to do this or do I have to add all includes to every jsp used?

You don't need ALL includes to be present in each of your tiles, but each used tag-library in a tile must specifically be included in the using tile.
eg :
In your example, each tile using the C JSTL library should at least have the <%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> include

Related

JSP adds HTML to the view but doesn't execute it to the user

I wanted to insert html code to a jsp page so I used normal spring controller populated my model with html items, then once I start to render the data on the view ,it show the user a row html tags rather than an actual elements like:
<p> <strong> Description:</strong></p>
I wanted to show the user an actual strong text not the tag itself ,anyone knows how to achieve that?
my view is like that:
<%# page isELIgnored ="false" %>
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
<c:out value="${Description}" escapeXml="false" />
</body>
</html>
any Idea how to solve it?
Edit : part of the code where I send the html :
model.addAttribute("Description", jobpost.getDescription()
.replace("<", "<")
.replace(">", ">")
.replace("&", "&")
.replace(""", "\"")
.replace("&apos;", "\\"));
Edit 2 : it finally worked guys It was a problem with the above code I forgot to insert ; at the end of & lt;
You can try wrapping the field with <b> or <h2>tag
<%# page isELIgnored ="false" %>
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
<b><c:out value="${Description}" escapeXml="false" /></b> // like this
</body><
/html>

Struts 2 optiontransferselect tag not working in jsp?

I am trying to execute the following jsp code which contains the optiontransferselect tag. However I am getting the below exception:
org.apache.jasper.JasperException: /abc.jsp(10,0) No tag "optiontransferselect label" defined in tag library imported with prefix "s"
Please find the below code i have used.
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Optiontransferselect Tag Example!</title>
</head>
<body>
<s:form>
<s:optiontransferselect label="Employee Records" name="leftSideEmployeeRecords" leftTitle="RoseIndia" rightTitle="JavaJazzUp" list="{'Deepak Kumar', 'Sushil Kumar','Vinod Kumar','Deepak Monthy','Deepak Mihanti', 'Sushil Kumar', 'Ravi Kant Kumar'}" headerKey="headerKey" headerValue="--- Please Select ---" doubleName="rightSideEmployeeRecords" doubleList="{'Amar Deep Patel', 'Amit Kumar','Chandan Kumar', 'Noor Kumar','Tammana Kumari'}" doubleHeaderKey="doubleHeaderKey" doubleHeaderValue="--- Please Select ---" />
</s:form>
</body>
</html>
Please Guide.
You are using older version of struts-core-xxx.jar in your project. Are you using 2.3.16 or above?
To use optiontransferselect tag you need to use struts-core-2.3.16 or higher..
You need to include the <s:head> tag which drags in some javascript that is needed to get the <s:optiontransferselect> tag to work.

Difference between <html:html></html:html> and <html></html>

Good day all,
I saw <html:html></html:html> from a jsp page in a java project.
Would like to ask what is the difference between these html tags.
Kindly advise.
The example code is as follow:
<%# taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%# taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<html:html>
<head>
</head>
<body>
<!-- some html code here -->
</body>
</html:html>
<html:html> uses the struts-html tag library, where <html></html> is just plain old html.
You can read all about the struts-html taglib here.
both are same.html:html is struts 1 tag which is equal to basic HTML's html tag.

Adding a css file to the <head> element in a jsp file?

I have been searching the net for the last hour but I can't see how to add a css file to the <head> element of a page.
Say I have a jsp called shopping.jsp. And inside it I conditionally include a jsp tag file called products.tag.
I want products.jsp to specify a css file to add to the head element of the page. How can this be done?
EDIT
This code is simplified for the sake of example.
// shopping.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib prefix="t" tagdir="/WEB-INF/tags" %>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<t:standard_page>
<jsp:body>
<t:products product="${product}"/>
</jsp:body>
</t:standard_page>
// products.tag
// I want to specify a css file in this tag file that will get added to the <head> element of the webpage
<%#tag description="Template for products on the shopping cart page" pageEncoding="UTF-8"%>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%#tag import="com.myapp.Product" %>
<%#attribute name="product" required="true" type="com.myapp.Product"%>
<div class="shopping-cart-row">
<div class="product-name">${product.name}</div>
<div class="product-quantity">${product.quantity}</div>
</div>
According to HTML5, <style> elements can appear inside
Any element that can contain metadata elements, div, noscript, section, article, aside
so you need not put <style> elements inside the <head> the way you do with <link> elements if you want your page to validate.
<style type="text/css">#import url("/path/to/stylesheet.css");</style>
from within the body will load the external stylesheet just fine.
If it's way down in the <body> or there are any long running non-deferred/async <script> elements before it, then those styles may arrive noticably later than styles loaded from the <head>.

What is the best way to create JSP layout template? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JSP tricks to make templating easier?
I'm new to JSPs and Servlets, I'm wondering is there a neat way to create a layout jsp and reuse it on similar jsp pages, something like asp.net master pages.
I googled it, some people say use templates
http://java.sun.com/developer/technicalArticles/javaserverpages/jsp_templates
that uses jstl tag library. It says to put a tag like this:
<%# taglib uri='/WEB-INF/tlds/template.tld' prefix='template' %>
but I get error (because jstl.jar and standard.jar are in WEB-INF/lib/ directory).
However some say jstl template have problems according to this
Struts OR Tiles OR ???...... JSP template solution
I would be glad to help me know the best way.
EDIT: What I need is to split page's layout into parts like content, header,... and set this parts in the page that uses the layout template, exactly like asp.net master page.
Put the following in WEB-INF/tags/genericpage.tag
<%#tag description="Overall Page template" pageEncoding="UTF-8"%>
<%#attribute name="header" fragment="true" %>
<%#attribute name="footer" fragment="true" %>
<html>
<body>
<div id="pageheader">
<jsp:invoke fragment="header"/>
</div>
<div id="body">
<jsp:doBody/>
</div>
<div id="pagefooter">
<jsp:invoke fragment="footer"/>
</div>
</body>
</html>
To use this:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib prefix="t" tagdir="/WEB-INF/tags" %>
<t:genericpage>
<jsp:attribute name="header">
<h1>Welcome</h1>
</jsp:attribute>
<jsp:attribute name="footer">
<p id="copyright">Copyright 1927, Future Bits When There Be Bits Inc.</p>
</jsp:attribute>
<jsp:body>
<p>Hi I'm the heart of the message</p>
</jsp:body>
</t:genericpage>
That does exactly what you think it does!
This was part of a great answer by Will Hartung on this link.

Categories