使用getOrDefault()时,我没有获得默认值

时间:2016-07-17 10:20:34

标签: java jsp spring-mvc

如果我在文本框页面中提供一些值,那么我得到正确的输出, 但如果我不在我的文本框中提供任何值,我希望它将打印默认值。

为此,我使用了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;
    }
}

弹簧调度-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>

输出

恭喜!详细信息已提交。

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

而不是

1 个答案:

答案 0 :(得分:0)

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文本框将参数作为参数发送为创建映射的空字符串,因此您不会获取默认值,而是使用空字符串。