spring JspException 和 PageContext 无法解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13377396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:31:31  来源:igfitidea点击:

JspException and PageContext cannot be resolved

springjsp

提问by jonnie

This is a follow up to a question on accessing resources in jsp page of spring mvc appThanks 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

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

javax.servlet.jsp.PageContext cannot be resolved to a type

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

as suggest by kmb385, here is my controller:

按照 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;
        }   
    }

and here is my index.jsp page just in case:

这是我的 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>

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

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

Any Help appreciated

任何帮助表示赞赏

UPDATE:Build path screen grab as requested by @kmb385 Eclipse Build Path

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

回答by Kevin Bowersox

Try setting the servlet-apidependency in your pom.xml to provided. This jar maybe conflicting with the tomcat provided servlet-api.jar.

尝试将servlet-apipom.xml 中的依赖项设置为提供。这个 jar 可能与 tomcat 提供的 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:

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

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

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

You may also need to add your maven dependencies to the build path. Right click your project > Maven > Update Project Configuration.

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

回答by bpjoshi

If you have downloaded all the dependencies in maven and the error is still not getting away, follow the steps below:

如果您已经下载了 maven 中的所有依赖项并且错误仍然没有消失,请按照以下步骤操作:

  1. Right click on project and go to properties
  2. Click over target run time
  3. Check the box in front of the server you are using.
  1. 右键单击项目并转到属性
  2. 单击目标运行时间
  3. 选中您正在使用的服务器前面的框。

This should work.

这应该有效。

回答by mwangi

Try import the class.

尝试导入类。

Modify your jsp's first line to look like this;

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

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

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

回答by mfruizs2

Add to the pom.xml dependencies:

添加到 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:

Make sure to add on jsp, before tag:

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

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

获取 JSP 上的上下文:

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

Import style css:

导入样式 css:

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

Dispatcher-servlet:

调度程序servlet:

On your "spring-dispatcher-servlet.xml" add the following lines:

在您的“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/" />

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

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

回答by Sachindra N. Pandey

How to solve javax.servlet.jsp.PageContext cannot be resolved to a type

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

1:- Select your project and Right click

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

2:- Go to Properties

2:- 转到属性

3:- Click Targated Runtimes

3:- 单击目标运行时

4:- Check mark "Apache Tomcat v8.0"

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

I am using Apache v8.0 in my case

在我的情况下,我使用的是 Apache v8.0

回答by Shashikant Sharma

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>

回答by CLUTCHER

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

这个替代方案对我<%=request.getContextPath()%>有用,它获取了应用程序上下文。