[Spring MVC]form:radiobuttonsのサンプル

1.Form側
public class UserForm {
private String rdA;
//get set 省略
}

public class ListLabel{
private String label;
private String value;
//get set 省略
}

2.Controller側

@Controller
public class UserController {
  
  private List<ListLabel>geInfotList(){

     private List<ListLabel>cftLst =new ArrayList<ListLabel>();

     cftLst.add(new ListLabel("AAA","11"));
     cftLst.add(new ListLabel("BBB","22"));
     cftLst.add(new ListLabel("CCC","33"));
     cftLst.add(new ListLabel("DDD","44"));

     return cftLst;
  }
  
  @RequestMapping(value="/display",method = RequestMethod.GET)
  public String display(Model model){

    UserForm userForm = new UserForm();
      userForm.setRdA("22");

      model.addAttribute("userForm",userForm);
      model.addAttribute("radioList",getInfoList());
      
      return "display";
      
  }
}

3.JSP

<form:form modelAttribute="userForm">
<tr><td>
<form:radiobuttons path="rdA" name="rdA"
  itesms="${radioList}" itemLabel="label" itemValue="value"/>
</td></tr>
<tr><td> <input type="submit" value="送信"></td></tr>
</form:form>

 

SpringMVC

Posted by arkgame