JspException和PageContext无法解析

问题描述 投票:0回答:8

这是关于 在 spring mvc 应用程序的 jsp 页面中访问资源的问题的后续问题 感谢@kmb385,我能够解决该问题,但现在我的 JSP 文件中出现以下 eclipse 错误 javax.servlet.jsp.JspException 无法解析为类型 和

javax.servlet.jsp.PageContext 无法解析为类型

根据 kmb385 的建议,这是我的控制器:

@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;
        }   
    }

这是我的 index.jsp 页面,以防万一:

<%@ 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>

我遇到过“解决方案”,通过在 JSP 验证器中禁用它,但请不要建议这样做,除非您能给出合理的理由。我宁愿正确地纠正这个问题

任何帮助表示赞赏

更新: 按照@kmb385的要求构建路径屏幕抓取 Eclipse Build Path

spring jsp
8个回答
36
投票

尝试将 pom.xml 中的

servlet-api
依赖项设置为提供。这个jar可能与tomcat提供的servlet-api.jar冲突。

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>

还要确保包含 jsp-api 依赖项,再次按提供的设置:

    <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.3</version>
        <scope>provided</scope>
    </dependency>

右键单击您的项目 > 属性,确保所有 Maven 依赖项都用于构建项目。在部署程序集选项卡上,单击“添加”按钮,然后单击“Java 构建路径条目”、“Maven 依赖项”,最后单击“完成”。

您可能还需要将 Maven 依赖项添加到构建路径中。右键单击您的项目 > Maven > 更新项目配置。


6
投票

如果您已经在maven中下载了所有依赖项,但错误仍然存在,请按照以下步骤操作:

  1. 右键单击项目并转到属性
  2. 单击目标运行时间
  3. 选中您正在使用的服务器前面的复选框。

这应该有效。


3
投票

尝试导入类。

将 jsp 的第一行修改为如下所示;

<%@ page language="java" import="javax.servlet.jsp.PageContext" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>


1
投票

添加到pom.xml依赖项:

     <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:

确保在jsp上添加,标签之前:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

获取 JSP 上的上下文:

<c:set var="contextPath" value="${pageContext.request.contextPath}"/>

导入样式CSS:

<link type="text/css" rel="stylesheet" href="${contextPath}/css/yourCssFile.css"/>

调度程序 servlet:

在“spring-dispatcher-servlet.xml”上添加以下行:

<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/" />

也许,您需要添加此适配器:

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

这里解释:如何使用spring MVC在jsp中包含js和css


0
投票

如何解决javax.servlet.jsp.PageContext无法解析为类型

1:- 选择您的项目并右键单击

2:- 转到属性

3:- 单击目标运行时

4:- 勾选“Apache Tomcat v8.0”

我在我的例子中使用 Apache v8.0


0
投票
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>

0
投票

这个替代方案对我有用

<%=request.getContextPath()%>
它获取应用程序上下文。


0
投票

当我使用这个时,我收到一个错误 The import javax.servlet.jsp.PageContent无法在之前的错误之上得到解决 – 乔尼 2012 年 11 月 14 日 11:32 不适合我...

© www.soinside.com 2019 - 2024. All rights reserved.