13

これは、 Spring MVC アプリの jsp ページでのリソースへのアクセスに関する質問へのフォローアップです @kmb385 のおかげで、その問題を解決できましたが、JSP ファイル javax.servlet.jsp.JspException cannot be で次の eclipse エラーが発生しますタイプに解決され、

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 ビルド パス

4

7 に答える 7

36

servlet-apipom.xmlの依存関係を provided に設定してみてください。この 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 > プロジェクト構成を更新します。

于 2012-11-14T11:02:33.793 に答える
6

Maven ですべての依存関係をダウンロードしてもエラーが解消されない場合は、次の手順に従ってください。

  1. プロジェクトを右クリックし、プロパティに移動します
  2. 目標実行時間をクリックします
  3. 使用しているサーバーの前にあるチェックボックスをオンにします。

これはうまくいくはずです。

于 2016-03-03T14:46:18.427 に答える
3

クラスをインポートしてみてください。

jsp の最初の行を次のように変更します。

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

于 2012-11-14T11:14:59.857 に答える
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"/>

ディスパッチャーサーブレット:

「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 を含める方法

于 2015-04-30T11:42:12.387 に答える
0

javax.servlet.jsp.PageContext を型に解決できない問題の解決方法

1:- プロジェクトを選択して右クリック

2:- プロパティに移動

3:- [ターゲット ランタイム] をクリックします。

4:- 「Apache Tomcat v8.0」にチェックマークを付ける

私の場合、Apache v8.0を使用しています

于 2016-06-27T15:05:19.500 に答える
0

<%=request.getContextPath()%>アプリケーションコンテキストを取得するこの代替手段は私にとってはうまくいきました。

于 2019-07-24T07:53:53.200 に答える