2017-12-15 12 views
2

Spring MVC, Spring Security 및 Spring Security를 ​​사용하여 간단한 로그인 Java 웹 애플리케이션을 작성하기 위해 Spring 4.2.5로 Spring maven 프로젝트를 빌드 중입니다.ServletContext 유형의 메소드 servletContext.addServlet이 인수 (String, DispatcherServlet)에 적용되지 않습니다.

ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher", new DispatcherServlet(appContext)); 

오류가 말한다 :

유형의 ServletContext의 방법 addServlet (문자열, 문자열) 인수 (문자열, DispatcherServlet이 적용되지 않습니다 스프링 JDBC하지만 난 시간 오류를 컴파일 무엇입니까)

,210
이미에서 javax.servlet 버전을 사용하고

3.1.0

의 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.archmage</groupId> 
<artifactId>SpringMVCAnnotationSecurity</artifactId> 
<packaging>war</packaging> 
<version>0.0.1-SNAPSHOT</version> 
<name>SpringMVCAnnotationSecurity Maven Webapp</name> 
<url>http://maven.apache.org</url> 

<properties> 
    <!-- generic properties --> 
    <java.version>1.7</java.version> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
    <spring.version>4.2.5.RELEASE</spring.version> 
    <spring.security.version>4.0.4.RELEASE</spring.security.version> 
    <jstl.version>1.2</jstl.version> 
    <javax.servlet.version>3.1.0</javax.servlet.version> 
    <javax.servlet.jsp.version>2.2</javax.servlet.jsp.version> 
    <mysql.version>5.1.34</mysql.version> 
    <ojdbc.version>11.2.0.3</ojdbc.version> 
    <jtds.version>1.3.1</jtds.version> 
    <junit.version>3.8.1</junit.version> 
</properties> 

<repositories> 
    <!-- Repository for ORACLE JDBC Driver --> 
    <repository> 
     <id>codelds</id> 
     <url>https://code.lds.org/nexus/content/groups/main-repo</url> 
    </repository> 
</repositories> 

<dependencies> 
    <!-- Spring framework START --> 
    <!-- http://mvnrepository.com/artifact/org.springframework/spring-core --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
    <!-- http://mvnrepository.com/artifact/org.springframework/spring-web --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-web</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
    <!-- http://mvnrepository.com/artifact/org.springframework/spring-webmvc --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
    <!-- http://mvnrepository.com/artifact/org.springframework/spring-jdbc --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-jdbc</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-tx</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
    <!-- Spring framework END --> 
    <!-- Spring Security Artifacts - START --> 
    <!-- http://mvnrepository.com/artifact/org.springframework.security/spring-security-web --> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-web</artifactId> 
     <version>${spring.security.version}</version> 
    </dependency> 
    <!-- http://mvnrepository.com/artifact/org.springframework.security/spring-security-config --> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-config</artifactId> 
     <version>${spring.security.version}</version> 
    </dependency> 
    <!-- Spring Security Artifacts - END --> 
    <!-- Jstl for jsp page --> 
    <!-- http://mvnrepository.com/artifact/javax.servlet/jstl --> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>jstl</artifactId> 
     <version>${jstl.version}</version> 
    </dependency> 
    <!-- Servlet API --> 
    <!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>javax.servlet-api</artifactId> 
     <version>${javax.servlet.version}</version> 
     <scope>provided</scope> 
    </dependency> 
    <!-- JSP API --> 
    <!-- http://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api --> 
    <dependency> 
     <groupId>javax.servlet.jsp</groupId> 
     <artifactId>jsp-api</artifactId> 
     <version>${javax.servlet.jsp.version}</version> 
     <scope>provided</scope> 
    </dependency> 
    <!-- MySQL JDBC driver --> 
    <!-- http://mvnrepository.com/artifact/mysql/mysql-connector-java --> 
    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
     <version>${mysql.version}</version> 
    </dependency> 
    <!-- Oracle JDBC driver --> 
    <dependency> 
     <groupId>com.oracle</groupId> 
     <artifactId>ojdbc6</artifactId> 
     <version>${ojdbc.version}</version> 
    </dependency> 
    <!-- SQLServer JDBC driver (JTDS) --> 
    <!-- http://mvnrepository.com/artifact/net.sourceforge.jtds/jtds --> 
    <dependency> 
     <groupId>net.sourceforge.jtds</groupId> 
     <artifactId>jtds</artifactId> 
     <version>${jtds.version}</version> 
    </dependency> 
    <!-- junit --> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>${junit.version}</version> 
     <scope>test</scope> 
    </dependency> 
</dependencies> 

<build> 
    <finalName>SpringMVCAnnotationSecurity</finalName> 

    <plugins> 
     <!-- Config: Maven Tomcat Plugin --> 
     <!-- http://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat7-maven-plugin --> 
     <plugin> 
      <groupId>org.apache.tomcat.maven</groupId> 
      <artifactId>tomcat7-maven-plugin</artifactId> 
      <version>2.2</version> 
     </plugin> 
    </plugins> 
</build> 

SpringWebAppInitializer.java :

import javax.servlet.ServletContext; 
import javax.servlet.ServletException; 
import javax.servlet.ServletRegistration; 

import org.springframework.web.WebApplicationInitializer; 
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; 
import org.springframework.web.servlet.DispatcherServlet; 

public class SpringWebAppInitializer implements WebApplicationInitializer { 

    @Override 
    public void onStartup(ServletContext servletContext) throws ServletException { 
     AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext(); 
     appContext.register(ApplicationContextConfig.class); 

     ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher", new DispatcherServlet(appContext)); 
     dispatcher.setLoadOnStartup(1); 
     dispatcher.addMapping("/"); 
    } 

} 

어떤 생각을 왜 방법 addServletServletContext 인 경우유형의 두 번째 인수에는 적용 할 수 없습니다.?


그래서 방법은 addServlet에만 두 번째 매개 변수로 Servlet 클래스를 받아? 아래 코드에서

+0

깨끗한 상태로 다시 작성 하시겠습니까? –

+0

이미 청소 및 재건/이클립스 재시도했지만 오류가 여전히 존재합니다. – Rival

+0

@Rival 그것은 서블릿 인터페이스를 구현하는 모든 클래스를 받아 들일 수 있으며, DispatcherServlet은 정상적으로 작동하도록 구현합니다. – Anand

답변

0

: 당신이 appContext를 사용하는

ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher", new DispatcherServlet(appContext)); 

.

DispatcherServlet의 Spring 응용 프로그램 컨텍스트를 만든 다음 그 값을 제공하십시오.

예 : 아래 코드를 추가하십시오 :

// Create the dispatcher servlet's Spring application context 
     AnnotationConfigWebApplicationContext dispatcherContext = 
     new AnnotationConfigWebApplicationContext(); 
     dispatcherContext.register(DispatcherConfig.class); 

     // Register and map the dispatcher servlet 
     ServletRegistration.Dynamic dispatcher = 
     servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherContext)); 

이것은 작동합니다.