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 eclip
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
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
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.
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.