【问题标题】:how to receive html check box value in spring mvc controller如何在spring mvc控制器中接收html复选框值
【发布时间】:2016-09-18 11:54:08
【问题描述】:

问题是如果复选框未选中请求无法在springMVC控制器中找到正确的映射函数。因为似乎如果选中它只会发送真值,但如果不选中它不会发送假值.

<form action="editCustomer" method="post">
  <input type="checkbox" name="checkboxName"/>
</form>

@RequestMapping(value = "/editCustomer" , method = RequestMethod. POST)
public void editCustomer(@RequestParam("checkboxName")String[] checkboxValue) 
{
  if(checkboxValue[0])
  {
    System.out.println("checkbox is checked");
  }
  else
  {
    System.out.println("checkbox is not checked");
  }
}

【问题讨论】:

    标签: html spring-mvc


    【解决方案1】:

    我解决了在@RequestMapping 中指定required = false 的类似问题。 这样,如果未在表单中选中复选框,则参数checkboxValue 将设置为null,如果选中,则将参数设置为"on"

    @RequestMapping(value = "/editCustomer" , method = RequestMethod. POST)
    public void editCustomer(@RequestParam(value = "checkboxName", required = false) String checkboxValue) 
    {
      if(checkboxValue != null)
      {
        System.out.println("checkbox is checked");
      }
      else
      {
        System.out.println("checkbox is not checked");
      }
    }
    

    希望这可以帮助某人:)

    【讨论】:

      【解决方案2】:

      我有同样的问题,我终于找到了一个简单的解决方案。

      只需将默认值添加到 false 即可完美运行。

      HTML代码:

      <form action="path" method="post">
          <input type="checkbox" name="checkbox"/>
      </form>
      

      Java 代码:

      @RequestMapping(
          path = "/path",
          method = RequestMethod.POST
      )
      public String addDevice(@RequestParam(defaultValue = "false") boolean checkbox) {
          if (checkbox) {
              // do something if checkbox is checked
          }
      
          return "view";
      }
      

      【讨论】:

        【解决方案3】:

        我必须添加与复选框名称相同的隐藏输入。值必须是“检查”。然后我可以检查控制器或我的服务类中字符串数组的长度。

        <form action="editCustomer" method="post">
          <input type="hidden" name="checkboxName" value="checked">
          <input type="checkbox" name="checkboxName"/>
        </form>

        @RequestMapping(value = "/editCustomer" , method = RequestMethod. POST)
        	public void editCustomer(@RequestParam("checkboxName")String[] checkboxValue) 
        	{
        		if(checkboxValue.length==2)
                	{
                  	  System.out.println("checkbox is checked");
                	}
        		else
                	{
                  	  System.out.println("checkbox is not checked");
                	}
        	}

        【讨论】:

          【解决方案4】:

          我认为正确的答案是 Paolo 和 Phoste 的答案。

          ITOH,如果您有许多复选框/参数/lasrge 表单,您可以使用 Map RequestParam,所以如果属性不存在 - Map.get() return null - 复选框未被选中。

              @RequestMapping(value = "/editCustomer" , method = RequestMethod. POST)
              public void editCustomer(@RequestParam Map<String,String> params) 
              {
                  String checkboxValue = map.get("checkboxName");
                  if(checkboxValue != null)
                  {
                      System.out.println("checkbox is checked");
                      return;
                  }
                  System.out.println("checkbox is not checked");
              }
          

          【讨论】:

            猜你喜欢
            • 2013-05-29
            • 1970-01-01
            • 2020-01-08
            • 2012-05-10
            • 1970-01-01
            • 2012-12-14
            • 2013-11-23
            • 1970-01-01
            • 2019-02-20
            相关资源
            最近更新 更多