我在線時使用了一個示例,但是當使用某個函數request.getServletContext()。getServletRegistrations()時。當我使用同樣的功能,在常規的動態Web項目,它工作正常,沒有錯誤,但使用maven這些相同的方法是不確定它不承認這個功能類型getServletContext()未定義爲Eclipse的Maven項目中的HttpServletRequest類型
// Check the target of the request is a servlet?
private boolean needJDBC(HttpServletRequest request) {
System.out.println("JDBC Filter");
String servletPath = request.getServletPath();
// => /abc/mnp
String pathInfo = request.getPathInfo();
String urlPattern = servletPath;
if (pathInfo != null) {
// => /spath/*
urlPattern = servletPath + "/*";
}
// Key: servletName.
// Value: ServletRegistration
Map<String, ? extends ServletRegistration> servletRegistrations = request.getServletContext()
.getServletRegistrations();
// Collection of all servlet in your webapp.
Collection<? extends ServletRegistration> values = servletRegistrations.values();
for (ServletRegistration sr : values) {
Collection<String> mappings = sr.getMappings();
if (mappings.contains(urlPattern)) {
return true;
}
}
return false;
}
。我試圖改變 request.getServletContext()。getServletRegistrations();
到 request.getSession()。getServletContext()。getServletRegistrations();
我認識到getSession.getServletContext(),但它不承認getServletRegistrations()。我想這可能已經是一個依賴問題,所以我增加了一些依賴於我的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ers</groupId>
<artifactId>ERSProject</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>ERSProject Maven Webapp</name>
<url>http://maven.apache.org</url>
<repositories>
<!-- Repository for ORACLE ojdbc6. -->
<repository>
<id>codelds</id>
<url>https://code.lds.org/nexus/content/groups/main-repo</url>
</repository>
</repositories>
<dependencies>
<!-- Oracle database driver -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.web/javax.servlet.jsp.jstl -->
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.servlet.jsp.jstl</artifactId>
<version>1.2.5-b03</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/javax.servlet.jsp.jstl-api -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>ERSProject</finalName>
</build>
任何幫助將不勝感激。謝謝。
Just hold這個javax.servlet-api在你的pom.xml中。並確保您的pom.xml位於主項目文件夾下的正確位置。輸入mvn clean install命令並檢查安裝成功的日誌,然後檢查.m2文件夾是否存在javax.servlet-api jar。如果一切正常,你必須確定maven和eclipse之間是否存在任何路徑衝突。 – erhun
檢查此答案https://stackoverflow.com/questions/29314336/how-to-figure-out-why-certain-javax-servlet-dependency-is-added-to-a-maven-proje –