我正在使用Spring MVC Web应用程序,我遇到了这些导入问题:
import javax.servlet.FilterRegistration;
import javax.servlet.ServletRegistration;
错误说The import javax.servlet.FilterRegistration cannot be resolved
这是我的初始应用程序(我正在使用java confgi)
public class InitApp implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container)
{
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(RootConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();
dispatcherServlet.register(MvcConfig.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherServlet));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
private void registerCharacterEncodingFilter(ServletContext servletContext) {
CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
encodingFilter.setEncoding("UTF-8");
encodingFilter.setForceEncoding(true);
FilterRegistration.Dynamic characterEncodingFilter = servletContext.addFilter("characterEncodingFilter", encodingFilter);
characterEncodingFilter.addMappingForUrlPatterns(null, false, "/*");
}
}
this is my maven dependency
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
如果我把这个maven依赖项错误不再显示
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
但是我的服务器给了我这个错误“Dynamic Web Module 3.1 requires Java 1.7 or newer”
是那些仅在我使用javax.servlet-api版本3.1.0
时才可用的导入答案 0 :(得分:1)
如果您需要使用Java Servlet 3.1中的类,则需要一台实现该级别规范的服务器(例如:Tomcat 8,Wildfly,Glassfish 4)。
否则,如果您的应用程序包含带有某些Servlet API的JAR,则应用程序服务器将不会加载它。您将获得:jar not loaded
。
顺便说一句,在我看来,可能使用Servlet 3.0已经足够了(Tomcat 7,JBoss AS 7,Glassfish 3.x)。
另一个解决方案是使用Spring的older version。