2014-11-27 4 views
1

풍부한 응용 프로그램에서 풍부한 컨텍스트를 사용하는 데 문제가 있으며 매핑되지 않은 이유를 이해할 시간을 보냈습니다.경로 변수가있는 스프링 MVC 매핑

<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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     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"> 

    <mvc:annotation-driven> 
     <mvc:message-converters> 
      <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> 
     </mvc:message-converters> 
    </mvc:annotation-driven> 

    <context:component-scan base-package="com.springapp.mvc"/> 

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/pages/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 
</beans> 
: 그래서 같은 문제 :(

의 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.springapp</groupId> 
    <artifactId>testMvc</artifactId> 
    <packaging>war</packaging> 
    <version>1.0-SNAPSHOT</version> 
    <name>testMvc</name> 

    <properties> 
     <spring.version>4.1.1.RELEASE</spring.version> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-core</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 

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

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

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

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-test</artifactId> 
      <version>${spring.version}</version> 
      <scope>test</scope> 
     </dependency> 

     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.11</version> 
      <scope>test</scope> 
     </dependency> 

     <dependency> 
      <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-core</artifactId> 
      <version>2.4.3</version> 
     </dependency> 

     <dependency> 
      <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-databind</artifactId> 
      <version>2.4.3</version> 
     </dependency> 
    </dependencies> 

    <build> 
     <finalName>testMvc</finalName> 
     <plugins> 
      <plugin> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <configuration> 
        <includes> 
         <include>**/*Tests.java</include> 
        </includes> 
       </configuration> 
      </plugin> 

      <plugin> 
       <groupId>org.mortbay.jetty</groupId> 
       <artifactId>maven-jetty-plugin</artifactId> 
       <version>6.1.10</version> 
       <configuration> 
        <scanIntervalSeconds>10</scanIntervalSeconds> 
        <connectors> 
         <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"> 
          <port>8080</port> 
          <maxIdleTime>60000</maxIdleTime> 
         </connector> 
        </connectors> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

web.xml을

<web-app version="2.4" 
     xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

    <display-name>Spring MVC Application</display-name> 

    <servlet> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

컨텍스트를 간단한 프로젝트를 만들고 얼굴을 시도

컨트롤러 :

package com.springapp.mvc; 

import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 

import java.util.HashMap; 
import java.util.Map; 

import static org.springframework.web.bind.annotation.RequestMethod.GET; 

@RestController 
@RequestMapping(value = "/items", produces = "application/json") 
public class HelloController { 

    @RequestMapping(method = GET, value = "/${id}/item") 
    public Map<String, Object> printWelcome(@PathVariable("id") String id) { 
     Map<String, Object> model = new HashMap<>(1); 
     model.put("item", id); 
     return model; 
    } 

    @RequestMapping(method = GET, value = "/clear") 
    public Map<String, Object> printWelcome() { 
     Map<String, Object> model = new HashMap<>(); 
     model.put("item", null); 
     return model; 
    } 

} 

테스트 :

GET http://localhost:8080/testMvc/items/clear => {"item":null} 
GET http://localhost:8080/testMvc/items/1/item => 404 Not Found 

에 어떤 문제가 있습니까?

+2

@RequestMapping (method = GET, value = "/ {id}/item")이 아니어야합니까? $를 사용할 필요가 없습니다. – StanislavL

답변

1

value = "/${id}/item")value ="/{id}/item"으로 바꾸십시오. 이것은 EL 표현식이 아니므로 여기서 $는 필요하지 않습니다.

Path 변수에 대해 Regrex 표현식을 사용하려면 {}에 넣으십시오. 구문은 {varName:regex}입니다. 여기서 첫 번째 부분은 변수 이름을 정의하고 두 번째 부분은 정규 표현식을 정의합니다.

자세한 내용은 Spring MVC Reference을 확인하십시오. URI 템플릿 패턴.

관련 문제