Spring MVC-存储和检索@PathVariable Map< String,String> [英] Spring MVC- Storing and Retrieving @PathVariable Map<String, String>

查看:144
本文介绍了Spring MVC-存储和检索@PathVariable Map< String,String>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经采用了2个PathVariable,而不是单独采用这些,我想将这两个PathVariables存储到Map中,并希望从Map中检索它。

I have taken 2 PathVariable, and instead of taking these separately i want to store these 2 PathVariables in to a Map and want to retrieve it from the Map.

在Spring MVC 3.1.0中,这是我的Controller类方法:

In Spring MVC 3.1.0 here is the my Controller class method:

@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";
}




我的请求网址为:
http:// localhost:3030 / spring_mvc_demo / welcome / India / ashrumochan123

但是当使用此URL发出请求时,我收到HTTP状态400 -

描述:客户端发送的请求是语法不正确。

But when giving request using this url, i am getting HTTP Status 400 -
Description: The request sent by the client was syntactically incorrect.

当我分别采用这些路径变量时,它工作正常。
这是代码 -

When i am taking these Path Variables separately then it is working fine. Here is the code -

@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";
    }

请告诉我我做错了什么?

Please tell me whether i am doing anything wrong?

任何帮助都将不胜感激。

Any help would be greatly appreciated.

推荐答案

根据 Spring文档,自版本3.2以来一直存在。

According to the Spring documentation, it's been there since version 3.2.

@PathVariable Map< String,String> ,我认为你在Spring servlet配置上缺少< mvc:annotation-driven />

For the @PathVariable with Map<String,String>, I think you are missing <mvc:annotation-driven/> on your Spring servlet configuration:

<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/>
...

我发现它在此链接

这篇关于Spring MVC-存储和检索@PathVariable Map&lt; String,String&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆