unknown tag c:forEach - html

I am using a <c:forEach>.....</c:forEach> tag in the following .jsp file and receiving unknown tag c:forEach. I am using maven and have the following dependency included:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
and my simple .jsp file looks as follows:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<h3>Senior Design Project</h3>
<br>
<form action="query" method="post" commandName="queryForm">
Enter an SQL Query: <br> <br>
<textarea name="query" path="query" cols="55" rows="3"
style="overflow: hidden"></textarea>
<br> <input type="submit" value="submit">
</form>
<h3>Results:</h3>
<br>
<h5>File Paths:</h5>
<c:forEach var="individualPath" items="${paths}">
<tr>
<td>${individualPath}</td>
</tr>
</c:forEach>
</body>
</html>
Is there more setup needed for jstl? From what I've read the maven dependency should handle everything. Help is greatly appreciated.

the taglib at the top of the .jsp page needs to be included as follows:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<h3>Senior Design Project</h3>
<br>
<form action="query" method="post" commandName="queryForm">
Enter an SQL Query: <br> <br>
<textarea name="query" path="query" cols="55" rows="3"
style="overflow: hidden"></textarea>
<br> <input type="submit" value="submit">
</form>
<h3>Results:</h3>
<br>
<h5>File Paths:</h5>
<c:forEach var="individualPath" items="${paths}">
<tr>
<td>${individualPath}</td>
</tr>
</c:forEach>
</body>
</html>

Even I had this issue, and I resolved it by removing the white space between <%# and taglib.

do 3 things these will make sure that everything works:
add the jstl jar in the build path
add the same jstl jar in the lib folder inside WEB-INF
Add the tag lib
'''
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
'''

<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Adding this in the top of the jsp file solved the issue.

Related

Background image not showing in jsp dynamic web project

Hi I am trying to use a background image in home.jsp page.when i run the project image is not loading a red cross mark is showing instead of the image
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" href="ac-styles.css" type="text/css" media="screen" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home Page</title>
</head>
<body >
<img src="img/bg.png">
<p> Hello This Is The Home Page For your J2ee Spring Application</p>
</body>
</html>
I had placed the image under
when i run the application the result is
please help..
Thanks in advance..
The JSP file is not in the same folder as of image file root folder is present. So either you have to move your .jsp file in web-content folder or you should use backspace commands like:
<img src="../../img/bg.png">
This should work.
Conversely the best method would be to migrate your image file in web-inf/views folder.That would be easy but not recommended for big projects.
note that I included "../" two times. In your directory structure it
is needed. For more information read the refering manual for HTML
src's.

Struts2 Exception Handling

How can I set single error page for every errors occured during
Execution in Struts2. My error page is as below.
error.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>HOME</title>
</head>
<body>
<center>
<h2 style="color: red;">Error Occurred during processing your Request...!!!!</h2>
</center>
<hr/>
</body>
</html>
You need global exception configuration. Take a look at this

Codification in JSP Imports

Good night.
I'm having trouble import static html page to a JSP page with UTF-8.
My JSP is UTF-8. I Write my HTML fragment with UTF-8, but I include with <%#include file="includes/menu.html"%>, the text is with the wrong encoding.
My JSP is complex, but my html is, an simple example:
<div>
<ul>
<li>Text with Á (acentuation)</li>
</ul>
</div>
Is very simple html, containing the application menu, but this error is occurring.
A very simple JSP is:
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<head>
<meta charset="UTF-8" />
<title>Example</title>
</head>
<body>
<%#include file="includes/menu.html"%>
</body>
PS: if I put directly in JSP, it works normally.
The solution to this problem is to change the menu.html to menu.jsp and on the top of the new file add
<%#page contentType="text/html" pageEncoding="UTF-8"%>
and then include this jsp in your page
<body>
<%#include file="includes/menu.jsp"%>
</body>

Importing CSS in JSP tags

What are some ways (if any) to link or import a style sheet from a JSP tag called in the <body>? It would be great if I can encapsulate all necessary imports in the JSP tag.
Current State
index.jsp:
<%# page contentType="text/html; charset=utf-8" %>
<%# taglib prefix="x" tagdir="/WEB-INF/tags" %>
<html>
<head>
<!-- we have to know what css file somecontent uses and include it here -->
<!-- the tag below prints <link rel="/somecontent.css"... /> but makes sure this url is only included once -->
<x:requireOnce loc="/somecontent.css" type="css" />
</head>
<body>
<x:somecontent />
</body>
</html>
Goal
index.jsp:
<%# page contentType="text/html; charset=utf-8" %>
<%# taglib prefix="x" tagdir="/WEB-INF/tags" %>
<html>
<head>
<%-- nothing for somecontent tag here --%>
</head>
<body>
<x:somecontent />
...
</body>
</html>
somecontent.tag:
<%# tag description="some independent content" %>
<%# taglib prefix="x" tagdir="/WEB-INF/tags" %>
<%-- the inline attribute will indicate that it is in the body and shouldn't use <link> which won't work here --%>
<x:requireOnce loc="/somecontent.css" type="css" inline="true" />
<%-- this will print <script type="text/javascript" src="/somecontent.js" ...></script> --%>
<x:requireOnce loc="/somecontent.js" type="js" />
...
Is there a way to keep a reference to the position in the head tag within the JspWriter and insert content there when necessary, i.e. new link tags?
Ideally, I don't want to inline the contents of the stylesheet or use javascript to include the style sheet. Hopefully there's some way with #import, <link> or some JSP magic... Thoughts?
You could do something along the lines of what SiteMesh does.
Each requireOnce tag would just put the file to link to in a list of files, stored in a request attribute. A servlet filter would buffer the whole response, and rewrite it when completed with the head section rewritten to include all the links.

HTML output with jsp:include and json-taglib

I'm using jsp:include inside json-taglib's json:property element. The problem with this is that all HTML elements from included JSP page gets stripped at some point and only plain text remains. I have already stripped all newlines so the result shoud be valid JSON data.
How do I get full HTML returned by json-taglib?
Below is a snippet demonstrating the situation.
<%# page language="java" %>
<%# page pageEncoding="UTF-8" %>
<%# page contentType="text/html; charset=UTF-8" %>
<%# taglib uri="http://www.atg.com/taglibs/json" prefix="json" %>
<json:object>
<json:property name="id" value="${element.id}" />
<json:property name="html" escapeXml="false">
<jsp:include page="/templates/generate-component.jsp">
<jsp:param name="element_id" value="${element.id}" />
</jsp:include>
</json:property>
</json:object>
Maybe you should encode the data passed to json-taglib.
Regards.
One solution is to wrap the jsp:include in <c:out> tag and (mis)use the body-as-default-value, like so:
<c:out value="${null}">
<jsp:include ...>
<jsp:param ... />
</jsp:include>
</c:out>
However, this won't work in a situation where the included JSP itself uses jsp:include.