<form method="post" action="/"> <div> <input type="checkbox" id="check1" name="check1"> <label for="check1">チェック</label> </div> <div> <input type="radio" id="radioA" name="radio1" value="male"> <label for="radioA">男性</label> </div> <div> <input type="radio" id="radioB" name="radio1" value="femail"> <label for="radioB">女性</label> </div> <div> <select id="select1" name="select1" size="4"> <option value="Windows">Windows</option> <option value="Mac">Mac</option> <option value="Linux">Linux</option> </select> </div> <div> <select id="select2" name="select2" size="4" multiple="multiple"> <option value="Android">Android</option> <option value="iphone">iPhone</option> <option value="Winfone">Windows Phone</option> </select> </div> <input type="submit" value="Click"> </form>
Controller
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 HelloController { @RequestMapping(value="/", method=RequestMethod.GET) public ModelAndView index(ModelAndView mav) { mav.setViewName("index"); mav.addObject("msg", "フォームを送信してください。"); return mav; } @RequestMapping(value="/", method=RequestMethod.POST) public ModelAndView send( @RequestParam(value="check1",required=false) boolean check1, @RequestParam(value="radio1",required=false) String radio1, @RequestParam(value="select1",required=false) String select1, @RequestParam(value="select2",required=false) String[] select2, ModelAndView mav) { String res = ""; try { res = "check:" + check1 + " radio:" + radio1 + " select:" + select1 + "\nselect2:"; } catch(NullPointerException e) {} try { res += select2[0]; for(int i = 1; i < select2.length; i++) res += ", " + select2[i]; } catch (NullPointerException e) { res += "null"; } mav.addObject("msg", res); mav.setViewName("index"); return mav; } }
そこそこ来てる? 学習途中はどこら辺にいるかさっぱり見当がつかないなー