복붙노트

[SPRING] 스프링 MVC - @PathVariable 맵 저장 및 검색 <String, String>

SPRING

스프링 MVC - @PathVariable 맵 저장 및 검색

나는 2 PathVariable을 가져 왔으며 이것들을 따로 따로 가져가는 대신이 2 PathVariables를 Map에 저장하고 Map에서 검색하려고합니다.

Spring MVC 3.1.0에서는 My Controller 클래스 메쏘드가있다.

@Controller
@RequestMapping("/welcome")
public class HelloController {

@RequestMapping(value="/{countryName}/{userName}",method=RequestMethod.GET)
public String getPathVar(@PathVariable Map<String, String> pathVars, Model model) {

    String name = pathVars.get("userName");
    String country = pathVars.get("countryName");

    model.addAttribute("msg", "Welcome " + name+ " to Spring MVC & You are from" + country);
    return "home";
}

하지만이 URL을 사용하여 요청을 줄 때, 나는 HTTP Status 400을 얻고있다. 설명 : 클라이언트가 보낸 요청의 구문이 올바르지 않습니다.

이 경로 변수를 개별적으로 가져갈 때 제대로 작동합니다. 다음은 코드입니다.

@RequestMapping(value="/{countryName}/{userName}", method=RequestMethod.GET)
    public String goHome(@PathVariable("countryName") String countryName,
            @PathVariable("userName") String userName, Model model) {
        model.addAttribute("msg", "Welcome " + userName
                + " to Spring MVC& You are from " + countryName);
        return "home";
    }

내가 뭐 잘못하고 있는지 말해줘?

어떤 도움이라도 대단히 감사하겠습니다.

해결법

  1. ==============================

    1.Spring의 문서에 따르면 3.2 버전 이후로 나온 것이다.

    Spring의 문서에 따르면 3.2 버전 이후로 나온 것이다.

    Map 을 사용하는 @PathVariable의 경우 Spring 서블릿 구성에서 가 누락되었다고 생각합니다.

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

    나는이 링크에서 그것을 발견했다.

  2. from https://stackoverflow.com/questions/35905876/spring-mvc-storing-and-retrieving-pathvariable-mapstring-string by cc-by-sa and MIT license