在Map <string,string> </string,string>中捕获GET请求参数

时间:2014-08-28 13:07:01

标签: java spring spring-mvc

我正试图在Spring MVC端捕获Map参数中的键值对。这看起来很简单,但我现在无法绕过它。请关注以下网址

www.goudengids.be.localhost:8080/ms/view/sendContactForm.ajah?pageId=711408&listingId=685592&siteId=353009&url=http%3A%2F%2Fwww.goudengids.be.localhost%3A8080%2Fms%2Fms%2Fkbc-bank-versicherung-recht-4780%2Fms-353009-preview%2F&moduleId=65920100&mySiteId=353009&pageShortId=1&prefills[Naam]=janneke

你最后会注意到我最近尝试获得这个工作前缀[Naam] = janneke。我想在以下控制器中捕获它。

public String getContactForm(@RequestParam(required = true) Long moduleId, @RequestParam(required = true) String url, @RequestParam(required=false) Map<String,String> prefills, Long mySiteId, Integer pageShortId,
      DefaultPageParameters defaultPageParameters, ModelMap model, HttpServletRequest request, HttpServletResponse response, Locale locale) throws Exception {

但是我在prefills变量中接收请求中的所有参数,而不仅仅是Naam,janneke。这甚至可能是我正在尝试的,还是应该使用带有令牌的大字符串进行标记化?

prefills=naam:janneke|title:maan|subject:space

1 个答案:

答案 0 :(得分:0)

我找不到干净的出路,所以我选择了实用的解决方案

  public String getContactForm(@RequestParam(required = true) Long moduleId, @RequestParam(required = true) String url, @RequestParam(required=false) List<String> prefills, Long mySiteId, Integer pageShortId,
      DefaultPageParameters defaultPageParameters, ModelMap model, HttpServletRequest request, HttpServletResponse response, Locale locale) throws Exception {


  private void prefillFieldsWithData(List<String> prefills, ModelMap model, BasicContactFormVo contactFormVo) {
    if(prefills != null && !prefills.isEmpty()){
      Map<String, String> valuesOfCustomFields = new HashMap<String, String>();
      List<ContactFormElementVo> customFormElements = contactFormVo.getCustomFormElements();
      for (String prefillData : prefills) {
        if(prefillData.contains("|")){
          String[] prefillFieldData = prefillData.split("|");
          for (ContactFormElementVo contactFormElementVo : customFormElements) {
            if(contactFormElementVo.getLabel().equals(prefillFieldData[0])){
              valuesOfCustomFields.put("cfe"+contactFormElementVo.getId().toString(), prefillFieldData[1]);
              break;
            }
          }                    
        }
      }
      model.addAttribute("customFieldValues",valuesOfCustomFields);
    }
  }

我有点难过,我必须这样做,但似乎Spring有一种通用的方法来检测Map作为请求参数并用请求中的所有参数填充它。除了重载那个类之外我没有办法解决这个问题,我宁愿不考虑它是整个MVC机制的一部分。

我现在用以下URL调用控制器。它只是......嗯...不是我最喜欢的解决方案

http://www.goudengids.be.localhost:8080/ms/view/sendContactForm.ajah?pageId=711408&listingId=685592&siteId=353009&url=http%3A%2F%2Fwww.goudengids.be.localhost%3A8080%2Fms%2Fms%2Fkbc-bank-versicherung-recht-4780%2Fms-353009-preview%2F&moduleId=65920100&mySiteId=353009&pageShortId=1&prefills=Naam|janneke%20zag%20eens%20pruimen&prefills=E-mail|maan