JspException and PageContext cannot be resolved - spring

This is a follow up to a question on accessing resources in jsp page of spring mvc app
Thanks to #kmb385 I was able to resolve that problem but now I get the following eclipse errors in my JSP file
javax.servlet.jsp.JspException cannot be resolved to a type
and
javax.servlet.jsp.PageContext cannot be resolved to a type
as suggest by kmb385, here is my controller:
#Controller
public class AdminController {
#RequestMapping("/")
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("index");
model.addObject("msg", "hello world");
return model;
}
}
and here is my index.jsp page just in case:
<%# 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>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<!-- <link type="text/css" rel="stylesheet" href="css/style.css"/> -->
<style type="text/css">
<%#include file="css/style.css" %>
</style>
<title>My Project - MainPage</title>
</head>
<body>
<h2 class="main_heading2">My Project - MainPage</h2>
<div style="float: right; width: 30%; text-align: center">
<p style="float:left;">an image should be here</p>
<img src="images/logo.jpg" alt="Logo"/>
<img src="${pageContext.servletContext.contextPath}/resources/images/logo.jpg" />
</div>
</body>
I have come across "solutions" to this by disabling it in the JSP validator but Please do not suggest this unless you can give a legitimate reason. I would rather correct this issue properly
Any Help appreciated
UPDATE:
Build path screen grab as requested by #kmb385

Try setting the servlet-api dependency in your pom.xml to provided. This jar maybe conflicting with the tomcat provided servlet-api.jar.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
Also be sure to include the jsp-api dependency, once again set as provided:
<!-- Servlet -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
Make sure all of your maven dependencies are being used to build the project by right clicking your project > Properties. On the deployment assembly tab click the add button, then Java Build Path Entries, then Maven Dependencies and finally Finish.
You may also need to add your maven dependencies to the build path. Right click your project > Maven > Update Project Configuration.

If you have downloaded all the dependencies in maven and the error is still not getting away, follow the steps below:
Right click on project and go to properties
Click over target run time
Check the box in front of the server you are using.
This should work.

Try import the class.
Modify your jsp's first line to look like this;
<%# page language="java" import="javax.servlet.jsp.PageContext" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>

Add to the pom.xml dependencies:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
JSP:
Make sure to add on jsp, before tag:
<%# taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
Get the context on JSP:
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>
Import style css:
<link type="text/css" rel="stylesheet" href="${contextPath}/css/yourCssFile.css"/>
Dispatcher-servlet:
On your "spring-dispatcher-servlet.xml" add the following lines:
<beans xmlns="... xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="...
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:resources mapping="/resources/**" location="/resources/css/" />
Maybe, you need to add this adapters:
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> [Optional]
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="0"/>
</bean>
explained here: how to include js and css in jsp with spring MVC

How to solve javax.servlet.jsp.PageContext cannot be resolved to a type
1:- Select your project and Right click
2:- Go to Properties
3:- Click Targated Runtimes
4:- Check mark "Apache Tomcat v8.0"
I am using Apache v8.0 in my case

This will solve the problem
<!-- Need this to compile JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>

This alternative worked for me <%=request.getContextPath()%> which gets the Application context.

Related

Equivalent of s:property in Spring MVC

I'm currently converting a Struts2 project to Spring MVC and I'm struggling finding the equivalent of the s:property tag in Spring.
Here is some example of what I want to convert:
<s:property value="#contentha.idcontent"/>
<s:property value="%{topRex.idcontent}"/>
Thanks for your help !
You can write
${contentha.idcontent}
${topRew.idcontent}
If you want to use a tag you can write :
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:out value="${contentha.idcontent}" />
<c:out value="${topRew.idcontent}" default="Value if null" />
This will require the jstl dependency. For example if you use Maven :
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

Why is the forEach tag not working with Tomcat but all is fine with Jetty?

These are the dependencies I have in pom.xml
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.13</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.13</version>
</dependency>
and the Jetty plugin I use:
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.1.v20140609</version>
</plugin>
and the Tomcat I use is: 8.5.4.
This is the view I have:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
<h:head>
<title>Student List</title>
</h:head>
<h:body>
<c:forEach items="#{studentBean.studentList}"
var="student">
#{student.fullname}
<br/>
</c:forEach>
</h:body>
</html>
When I run this application like this:
mvn clean install
mvn jetty:start
and visit localhost:8080, I will see the list just fine on my browser.
Koray Tugay
Mick Jagger
Now if I copy the .war file created and deploy it to Tomcat, I will see:
type Exception report
message javax/servlet/jsp/jstl/core/LoopTagStatus
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: javax/servlet/jsp/jstl/core/LoopTagStatus
javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
javax.faces.view.facelets.FaceletException: javax/servlet/jsp/jstl/core/LoopTagStatus
com.sun.faces.facelets.tag.AbstractTagLibrary$UserComponentHandlerFactory.createHandler(AbstractTagLibrary.java:344)
java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.LoopTagStatus
(full stack trace here..)
Now you maybe tempted to say Tomcat does not come with JSTL, you should add it in your pom.xml as dependency! However, at this point I will ask you:
Why does it work fine with Jetty?
Also, when I go through the jsf-impl-2.2.13.jar, which maven downloaded, I find the file called: jstl-core.taglib under com/sun/faces/metadata/taglib.
And in this file, I see this tag declaration:
<tag>
<description><![CDATA[
The basic iteration tag, accepting many different
collection types and supporting subsetting and other
functionality
]]></description>
<tag-name>forEach</tag-name>
<handler-class>com.sun.faces.facelets.tag.jstl.core.ForEachHandler</handler-class>
<attribute>
<description><![CDATA[
Collection of items to iterate over.
]]></description>
<name>items</name>
<required>false</required>
<type>java.lang.Object</type>
</attribute>
</tag>
and also, class com.sun.faces.facelets.tag.jstl.core.ForEachHandler is already included in the jsf-impl-2.2.13.jar.
So my understanding is, c:forEach is supposed to be included in the JSF implementation. Why is Tomcat not liking this situation?
If I include this dependency:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
in pom.xml, for loop will loop happily in Tomcat as well. How does this work, or not work?
The class com.sun.faces.facelets.tag.jstl.core.ForEachHandler has via its dependency com.sun.faces.facelets.tag.jstl.core.JstlIterationStatus a dependency on javax.servlet.jsp.jstl.core.LoopTagStatus.
Tomcat doesn't ship with it. Hence the required JSTL dependency.
Jetty apparently provides its own JSTL library. So you don't need to include it via webapp. You should mark it at least <scope>provided</scope>.

the message is not printed correctly on the view page in spring mvc [duplicate]

This question already has answers here:
EL expressions not evaluated in JSP
(5 answers)
Closed 7 years ago.
JSTL variable values are not shown in EL. For example this code:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="s" uri="http://www.springframework.org/tags" %>
<html>
<body>
<c:forEach var="i" begin="1" end="5" >
<c:out value="${i}" />
</c:forEach>
</body>
</html>
browser renders like: ${i} ${i} ${i} ${i} ${i}
Or this one:
<c:set var="someVar" value="Hello"/>
<c:out value="${someVar}"/>
browser displays: ${someVar}
I'm using Spring-MVC 3 and Maven to build the sample project, deploying it to Tomcat 7.
In Spring's context I have view resolver configurated as follows:
<bean class=
"org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="
org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp" />
</bean>
Model variables passed form Spring's controller not shown as well.
Mavens pom.xml has following jstl related dependencies:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
So, any suggestions how to resolve this?
So, the EL (those ${} things) doesn't get executed? That can happen when your servletcontainer runs at Servlet 2.3 / JSP 1.2 modus or lower, while you're using JSTL 1.1 or newer. During the change from JSTL 1.0 to 1.1, EL was moved from JSTL into JSP. That was JSP 2.0, which is part of Servlet 2.4. JSP 1.2 and older doesn't have EL bundled. JSTL 1.1 and newer doesn't have EL bundled.
You need to make sure that your web.xml root declaration conforms at least Servlet 2.4. As you're using JSP 2.1, which is part of Servlet 2.5, you're apparently targeting a Servlet 2.5 compatible container. So, make sure that your web.xml root declaration conforms Servlet 2.5:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"
version="2.5">
<!-- Config here. -->
</web-app>
Tomcat 7 is however a Servlet 3.0 compatible container. I'd consider changing maven pom to declare Servlet 3.0 / JSP 2.2 so that you can benefit all the new Servlet 3.0 features.
See also:
Our JSTL wiki page
History of EL

Tomcat 6.0.37 not processing jstl tag statements

I have a simple test page with the following statement but it appears that the ${} tags are not being processed by Tomcat and i get no errors in the log.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>JSTL Test Example</title>
<body>
Setting value using c:out <c:set var="name" scope="request" value="Testing" /><br>
Value is: <b><c:out value="${name}"/></b><br>
</body>
</html>
Browser Output:
Setting value using c:out
Value is: ${name}
Using the information from https://stackoverflow.com/tags/jstl/info
I verified
1) Tomcat lib has jstl.2 jar , there are no duplicate jars - if i remove this jar i get a big exception complaining about missing tag classes.
2) My webapp does not have jstl jar in the classpath.
3) Tomcat web.xml has the correct servlet specification
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
Any help would be greatly appreciated.
Thanks
their may be conflict of jsp-api jar
remove jstl.version.jar from tomcat lib
and add directly dependency in your project
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
and try !

Tiles 2.2 put-list-attribute + Spring 3.1.4

I am migrating our application from struts 1.3 to spring 3.1.4 mvc. In the process i am also upgrading from tiles 1.1 to tiles 2.2. Jstl version is 1.1. With Tiles 2.2, i am having an issue about using put-list-attribute. I am trying to show a simple jsp page which should does following:
a) Header : include bunch of css (i commented it out as it doesn't work. Using firebug i see error "NetworkError: 404 Not Found - http://localhost:8080/appname/%BeachStyle%7d" --- {eachStyle}
No idea where %BeachStyle% came from.
b) Header : include title -- Works fine
c) body : show static text
tiles.xml
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
"http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<tiles-definitions>
<definition name=".login" preparer="com.tiles.LoginController"
template="/tiles/layouts/layoutmain.jsp">
<put-attribute name="pageTitle" value="vivi test" />
<put-list-attribute name="baseStylesTest">
<add-attribute value="/styles/css/grids.css"/>
<add-attribute value="/styles/css/superfish.css"/>
<add-attribute value="/styles/css/styles.css"/>
</put-list-attribute>
</definition>
</tiles-definitions>
layoutmain.jsp
<%# taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<tiles:useAttribute id="stylesList" name="baseStylesTest" classname="java.util.List"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%-- <c:forEach var="eachStyle" items="${baseStylesTest}">
<link type="text/css" rel="stylesheet" href="<c:out value='${eachStyle}'/>" /> --%>
<title><tiles:insertAttribute name="pageTitle" /> </title>
</head>
<body>
Login page body
</body>
</html>
I tried the following:
i) Checked the tiles2.2 dtd and example to use it - found similar usage
ii) Checked examples over internet and how it's being used - found similar usage
iii) Checked how its used with struts -- found they use #attr.xxx as it is in pagescope
I don't see anything wrong in my implementation. The title attribute is rendered properly but on the attribute belonging to put-list-attribute.
Any help is much appreciated.
Found the solution. JSTL wasn't working as i declared my web-app version to be 2.5. This is not compatible with tomcat5.5 which i was using. Changed the web-app version to be 2.4 and all is good.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
---
</web-app>

Resources