【问题标题】:While using getOrDefault() I am not getting default Value使用 getOrDefault() 时,我没有获得默认值
【发布时间】:2016-07-17 10:20:33
【问题描述】:

如果我在文本框页面中提供了一些值,那么我会得到正确的输出, 但是,如果我没有在我的文本框中提供任何值,我想要的是打印默认值。

为此,我使用了 Map 类的 getOrDefault() 方法,但仍然没有获得默认值。

StudentAdmissionController.java

package com.diwakar;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class StudentAdmissionController {

    @RequestMapping(value="/admission.html", method = RequestMethod.GET)
    public ModelAndView getAdmissionForm() {

        ModelAndView model =  new ModelAndView("AdmissionForm");
        return model;
    }

    @RequestMapping(value="/submitForm", method =  RequestMethod.POST)
    public ModelAndView submitAdmissionForm(@RequestParam Map<String, String> reqParm) {

        String name  = reqParm. ("studentName", "Default Name");
        String hobby = reqParm.getOrDefault("studentHobby", "Default Hobby");

        ModelAndView model = new ModelAndView("AdmissionSuccess");
        model.addObject("msg", "Details submitted by you, Name : " + name + " and hobby is : " + hobby);
        return model;
    }
}

spring-dispatcher-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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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-3.0.xsd">



<!--      <bean id="HandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
          <bean name="/welcome.html" class="com.diwakar.HelloController" />  -->

     <context:component-scan base-package="com.diwakar" />
     <mvc:annotation-driven />

     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/" />
        <property name="suffix" value=".jsp"></property>
     </bean>                    
</beans>

输出是

恭喜!详情已提交。

您提交的详细信息,姓名:和爱好是:

代替

【问题讨论】:

    标签: java jsp spring-mvc


    【解决方案1】:

    getOrDefault 仅在没有映射时返回默认值,您可能有一个到空字符串的映射,您应该检查自定义代码,也许构建一个方法:

    private static String getOrDefaultIfEmpty(Map<String,String> map, String key, String def)
        {
            String v = map.get(key);
            return v==null || v.length()==0 ? def : v;
        }
    

    您需要进行此检查,因为一个空的 HTML 文本框会发送一个创建映射的空字符串作为参数,因此您不会获得默认值,而是一个空字符串。

    【讨论】:

    • 感谢@testo 的回复,但是如果我想单独使用这个方法进行检查,如果我错了,请纠正我 这个方法返回指定键映射到的值,如果是 defaultValue此映射不包含键的映射。在我的应用程序中有一个名为 studentHobby 的文本框,我没有提供任何内容,它将检查是否有任何与该名称相关的值,如果没有,它将打印第二个参数,即默认值 String hobby = reqParm.getOrDefault("studentHobby", "Default Hobby");
    • 上述方法是不是错了,..?如果是那么如何。我没有得到这个。根据 oracle 文档,它应该打印第二个参数,即默认值。如果我只想使用这个 String hobby = reqParm.getOrDefault("studentHobby", "Default Hobby"); 谁能帮我解决这个问题..?
    • 我更新了答案。一个空文本框发送一个空字符串,而不是“nothing”
    猜你喜欢
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多