2017-10-13 45 views
-1

我在設置我的servlet時遇到了問題。當我添加到我的app.servlet.xml時,到達我的控制器方法中的參數自動爲null。<mvc:annotation-driven />的問題

我想在繼續之前測試基本配置,但是我無法解決這個問題。任何人都可以幫忙嗎?

APP-servlet.xml中:

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

    <!-- Scans the classpath of this application for @Components to deploy as beans --> 
    <context:component-scan base-package="com.app.controller" /> 

    <!-- Configures the @Controller programming model --> 
    <mvc:annotation-driven/> 

holaController:

@Controller 
@RequestMapping("/") 
public class HolaController { 

@RequestMapping("/hola/{id}") 
public ModelAndView hola(@PathVariable Integer id, 
     HttpServletRequest httpServletRequest, 
     HttpServletResponse httpServletResponse){ 

    String mensaje = "<br><div><h2>Hola</h2></div><br><br>"; 

    return new ModelAndView("hola", "mensaje", mensaje); 
} 

} 

web.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
version="3.0"> 

<display-name>APP</display-name> 

<servlet> 
    <servlet-name>app</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/app-servlet.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>app</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 

<welcome-file-list> 
<welcome-file>index.jsp</welcome-file> 
</welcome-file-list> 
</web-app> 

的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.app</groupId> 
     <artifactId>app</artifactId> 
     <packaging>war</packaging> 
     <version>0.3</version> 
     <name>app</name> 
     <url>http://maven.apache.org</url> 

     <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <spring.version>4.3.0.RELEASE</spring.version> 
     <hibernate.version>4.3.11.Final</hibernate.version> 
     <jackson.library>2.7.5</jackson.library> 
     </properties> 

     <dependencies> 

     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <version>5.1.6</version> 
     </dependency> 

     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>servlet-api</artifactId> 
      <version>2.5</version> 
      <scope>provided</scope> 
     </dependency> 

     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
     </dependency> 

     <dependency> 
      <groupId>javax.inject</groupId> 
      <artifactId>javax.inject</artifactId> 
      <version>1</version> 
     </dependency> 

     <dependency> 
      <groupId>org.aspectj</groupId> 
      <artifactId>aspectjrt</artifactId> 
      <version>1.7.4</version> 
     </dependency> 

     <dependency> 
      <groupId>commons-dbcp</groupId> 
      <artifactId>commons-dbcp</artifactId> 
      <version>1.4</version> 
     </dependency> 

    <!-- -->  
    <!-- HIBERNATE --> 
    <!-- --> 

     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-core</artifactId> 
      <version>${hibernate.version}</version> 
     </dependency> 

     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-entitymanager</artifactId> 
      <version>${hibernate.version}</version> 
     </dependency> 

    <!-- -->  
    <!-- END HIBERNATE --> 
    <!-- --> 

    <!-- -->  
    <!-- LOGGER --> 
    <!-- --> 

     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-api</artifactId> 
      <version>1.5.2</version> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-log4j12</artifactId> 
      <version>1.5.2</version> 
      <type>jar</type> 
      <scope>compile</scope> 
     </dependency> 
     <dependency> 
      <groupId>dom4j</groupId> 
      <artifactId>dom4j</artifactId> 
      <version>1.6</version> 
     </dependency> 
     <dependency> 
      <groupId>commons-logging</groupId> 
      <artifactId>commons-logging</artifactId> 
      <version>1.1.1</version> 
     </dependency> 

    <!-- -->  
    <!-- END LOGGER --> 
    <!-- --> 

    <!-- -->  
    <!-- SPRING --> 
    <!-- --> 

      <dependency> 
       <groupId>org.springframework</groupId> 
       <artifactId>spring-webmvc</artifactId> 
       <version>${spring.version}</version> 
      </dependency> 
      <dependency> 
       <groupId>org.springframework</groupId> 
       <artifactId>spring-tx</artifactId> 
       <version>${spring.version}</version> 
      </dependency> 
      <dependency> 
       <groupId>org.springframework</groupId> 
       <artifactId>spring-orm</artifactId> 
       <version>${spring.version}</version> 
      </dependency> 

    <!-- --> 
    <!-- FIN SPRING --> 
    <!-- --> 

     </dependencies> 

     <build> 
      <finalName>app-web</finalName> 
     </build> 
    </project> 
+0

如果您正在開始一個新項目,有什麼理由不使用Spring Boot? – Andreas

+0

你是說'id','httpServletRequest'和'httpServletResponse'都是'null'? –

+0

我運行了你的代碼,我沒有得到httpServletRequest和httpServletResponse爲空 –

回答

-1

如果你希望所有的servlet和過濾器共享任何其他設置/ bean配置,那麼你需要添加下面的配置在web.xml

<listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 


<context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/root-context.xml</param-value> 
    </context-param> 

及以下關係在你的POM也不翼而飛。 xml

 <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
+0

不需要'COntextLoaderListener',你可以完全使用'DispatcherServlet'工作。 \ –

+0

我同意,我更新了我的評論,如果需要其他servlet或過濾器來共享通用設置,並且您的代碼需要Web應用程序的根web應用程序上下文。這個上下文可以用來加載和卸載彈簧管理的bean,而不管在控制器層使用什麼技術。 –

+0

這仍然可以通過一個「DispatcherServlet」來完成(儘管沒有多少人知道這一點)。儘管如此,它並不是對這個問題的回答。 –