Proper installation of JSTL for Maven (Intellij IDEA) - maven

I am trying to use JSTL for my index.jsp page, but for some reason each time after packaging project to .war and running with Tomcat it gives me following errors:
HTTP Status 500 - /index.jsp (line: 12, column: 0) Unterminated <c:if tag
or
HTTP Status 500 - java.lang.ClassNotFoundException: org.apache.jsp.index_jsp
From what I found on google, there are 2 ways to install JSTL into your Maven project:
1) Add this to pom.xml
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
2) Add some jars to WEB-INF/lib, but here's the problem: no such folder was automaticaly created and if I do it manually it does not help. Project structure looks like this:
The code of index.jsp is the following:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:if> Tag Example</title>
</head>
<body>
<c:set var="salary" scope="session" value="${2000*2}"/>
<c:if test="${salary > 2000}">
<p>My salary is: <c:out value="${salary}"/><p>
</c:if>
</body>
</html>
So, what should I do to make those work? I can't find any guide or piece of information that could help me solving this. Thanks for having a look at my problem!

Just what compiler tells you: Unterminated c:if tag:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:if> Tag Example</title>
</head>
<body>
<c:set var="salary" scope="session" value="${2000*2}"/>
<c:if test="${salary > 2000}">
<p>My salary is: <c:out value="${salary}"/><p>
</c:if>
</body>
</html>
Look at line 4 of your jsp:
<title><c:if> Tag Example</title>
It must be
<title><c:if> Tag Example </c:if></title>
EDIT: As seems that I explained badly, I have tested in a running web-app, and this way (that is just what I was trying to explain) works:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:if> Tag Example</title>
</head>
<body>
<c:set var="salary" scope="session" value="${2000*2}"/>
<c:if test="${salary > 2000}">
<p>My salary is: <c:out value="${salary}"/><p>
</c:if>
</body>
</html>

Related

request.contextPath not resolved inside Apache Tiles JSP fragments

I have a Spring + Apache Tiles 3 Aplication.
I have a layout and inside of this, the header, the body and the footer:
defaultLayout.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# page isELIgnored="false" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%# taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title><tiles:getAsString name="title" /></title>
<link href="<c:url value='/static/css/bootstrap.css' />" rel="stylesheet"></link>
<link href="<c:url value='/static/css/app.css' />" rel="stylesheet"></link>
</head>
<body>
<header id="header">
Web Application Context Path="${pageContext.request.contextPath}"
<tiles:insertAttribute name="header" />
</header>
<section id="sidemenu">
<tiles:insertAttribute name="menu" />
</section>
<section id="site-content">
<tiles:insertAttribute name="body" />
</section>
<footer id="footer">
<tiles:insertAttribute name="footer" />
</footer>
</body>
</html>
The "${pageContext.request.contextPath}" works fine inside the Layout, but not on the JSP children's:
menu.jsp
<nav>
<img class="logo" src="${pageContext.request.contextPath}/static/img/Linux-icon.png">
<ul id="menu">
<li>Home</li>
<li>Products</li>
<li>Contact Us</li>
</ul>
This prints the "${pageContext.request.contextPath}" "as is" in the result HTML.
Try to use <%# page isELIgnored="false" %>. It will solve your problem
Since you mentioned HTML code and say that the context path is printed "as-is", is this code in HTML file? Then it will not work.
Check this question - ${pageContext.request.contextPath} is not working on plain HTML

unable to pass variables between jsp templates

I'm unable to pass variables from a.jsp to b.jsp, following is what I tried and the output. Can someone please help? Thanks.
a.jsp
start including
<jsp:include page="b.jsp">
<jsp:param name="somevar" value="zzzz" />
</jsp:include>
stop including
b.jsp
${somevar}
${param.somevar}
<%=request.getParameter("somevar")%>
output
start including
null
stop including
You have to set your param before include.
start including
<jsp:params>
<jsp:param name="somevar" value="zzzz" />
</jsp:params>
<jsp:include page="b.jsp">
</jsp:include>
stop including
And can u try this in b.jsp:
<%=request.getParameter("somevar");%>
What is output?
Try the following code:
home.jsp
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# page session="false"%>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Hello world!</h1>
<jsp:include page="b.jsp">
<jsp:param name="something" value="something" />
</jsp:include>
<h1>Bye World</h1>
</body>
</html>
In the b.jsp page use the following code.
<%# 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>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Param example</title>
</head>
<body>
<h1>
<c:out value="${param.something}" />
</h1>
</body>
</html>
The output must be
Hello world!
something
Bye World
Try to avoid java code in JSP pages as it is not advisable. For more details, have a look at this answer How to avoid Java Code in JSP-Files?

Spring MVC application not working

I am trying as explained here, but couldn't get the output that is shown for the second page.
It redirects to the second page but doesn't print the text i entered, rather returns the empty page.
Any suggestions ? i am using spring framework 3.0.2.
helloView.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello</title>
</head>
<body>
<h1>${helloMessage}</h1>
</body>
</html>
nameView.jsp
<%#taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Name</title>
</head>
<body>
<h1>Enter your Name</h1>
<spring:nestedPath path="name">
<form action="" method="post">
Name:
<spring:bind path="value">
<input type="text" name="${status.expression}" value="${status.value}">
</spring:bind>
<input type="submit" value="OK">
</form>
</spring:nestedPath>
</body>
</html>
Add this bean class
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
This bean is responsible for automatically creating an URL mapping for all controllers registered in the file. It takes the fully-qualified class name of the controller (in our case, controller.HelloController) and strips the package name and Controller suffix, then uses the result as a URL mapping.

generic linking, variables and paths in jsp

Requests: Please only answer if you know implications in depth.
Related Question that I found help from: Nested expression in JSP/JSTL
I want to make my links, paths, and variables (for images, scripts, and other pages and includes) generic so that refactoring or changes file locations in future would make no big problems.
What I thought was to make:
variables.jsp:
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%#taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<c:set var="mainDir" value="${pageContext.request.contextPath}/view/jsp" />
<c:set var="resourcesDir" value="${mainDir}/resources" />
header.jsp
<%# taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%#include file="variables.jsp" %>
<html>
<head><title>${pageTitle}</title></head>
login.jsp
<c:set var="pageTitle" >
<spring:message code="pageTitle.login" />
</c:set>
<%#include file="${mainDir}/common/header.jsp" %>
<body>...
...
...
<%#include file="${mainDir}/common/footer.jsp" %>
Please observe the includes and c:sets / c:outs with spring:messages.
What is the proper way to do what I want to (if my current strategy is not sane).

Struts2 ajax theme problem in IE9

We have an running application that is developed in struts-2.0.14. In this we used Ajax theme for displaying contents.
This ajax theme is running properly in all browsers except IE9 browsers, In IE9 this ajax theme making problem . The result is showing in new tab instead of showing in target element(div) specified by targets property.
Parent Page
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%# taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<s:head theme="ajax" debug="false" />
</head>
<body>
<s:url id="changePwd" action="changePassword" />
<s:div theme="ajax" id="pwdDiv" executeScripts="true" href="%{changePwd}" loadingText="Loading..."/>
</body>
</html>
Inner page
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%# taglib prefix="s" uri="/struts-tags" %>
<%# taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<s:form action="changePassword" id="resetPassword" name="resetPassword">
<s:textfield name="username" id="username" />
<s:password showPassword="true" name="newpassword" id="newpassword" value="%{newpassword}"/>
<s:password showPassword="true" name="confirmpassword" id="confirmpassword" value="%{confirmpassword}"/>
<s:submit value="Confirm" showLoadingText="false" onclick="clearMsg();" theme="ajax" targets="pwdDiv" executeScripts="true" cssClass="userbutton" />
</s:form>
When Submitting the form in Inner Page the resulting page is popup into new tab. actually it should replace the content of pwdDiv.
Note: the same working properly in other browsers including (IE7,8)
Update:
making showLoadingText as true making the request as Ajax but the values are passed as null (password,confirm password ect,. -all fields)
Adding type="button" in s:submit button solve the problem. Don't know what is behind this !
<s:submit **type="button"** value="Confirm" showLoadingText="false" onclick="clearMsg();" theme="ajax" targets="pwdDiv" executeScripts="true" cssClass="userbutton" />

Resources