日韩精品欧美激情国产一区_中文无码精品一区二区三区在线_岛国毛片AV在线无码不卡_亞洲歐美日韓精品在線_使劲操好爽好粗视频在线播放_日韩一区欧美二区_八戒八戒网影院在线观看神马_亚洲怡红院在线色网_av无码不卡亚洲电影_国产麻豆媒体MDX

SpringMVC接收checkbox傳值

時(shí)間:2020-02-28 16:37:09 類型:JAVA
字號(hào):    

  SpringMVC接收checkbox傳值

  Controller方法形參接收checkbox的值,既可以用String,也可以用String[]。


  字符串?dāng)?shù)組接收的測(cè)試代碼如下:

@Controller
@RequestMapping("/mycontroller")
public class MyController {

    @RequestMapping(method = RequestMethod.GET)
    public String form() {
        return "mycontroller";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String form1(@RequestParam("interest") String[] interest, Model model) {
        String a = Arrays.toString(interest);
        model.addAttribute("ins", a);
return "ok";
    }

}

  字符串接收的測(cè)試代碼如下(測(cè)試完數(shù)組接收后 修改即可):

@Controller
@RequestMapping("/mycontroller")
public class MyController {

    @RequestMapping(method = RequestMethod.GET)
    public String form() {
        return "mycontroller";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String form1(@RequestParam("interest") String interest, Model model) {
        model.addAttribute("ins", interest);
        
        return "ok";
    }

}

  小知識(shí):

  如果checkbox都留空(不選擇),那么Controller會(huì)報(bào)錯(cuò)。解決辦法:①前端js判斷;②前端加一個(gè)hidden的checkbox。

  補(bǔ)充方法:

  @RequestParam(value = "interest", required = false)


<