【状況】
spring frameworkで提供されているformタグライブラリのラジオボタン(form:radiobutton)。
path属性にはプロパティ名が入るわけだが、しかし一方でJavaクラスの方でモデルをリスト化して、その個々のプロパティを参照する場合うまくいきません。以下がその実装しようとしたソース
モデルクラス
java
1 2import java.io.Serializable; 3 4@SuppressWarnings("serial") 5public class User implements Serializable { 6 7 //ユーザID 8 private Integer id; 9 10 //ユーザ名 11 private String name; 12 13 //ユーザの年 14 private Integer age; 15 16 //ユーザの権限(1:無し、2:回覧、3:承認) 17 private Integer authority; 18 19 public User() { 20 super(); 21 } 22 23 public User(Integer id, String name, Integer age,Integer authority) { 24 this.id = id; 25 this.name = name; 26 this.age = age; 27 this.authority = authority; 28 } 29 30 public Integer getId() { 31 return id; 32 } 33 34 public void setId(Integer id) { 35 this.id = id; 36 } 37 38 public String getName() { 39 return name; 40 } 41 42 public void setName(String name) { 43 this.name = name; 44 } 45 46 public Integer getAge() { 47 return age; 48 } 49 50 public void setAge(Integer age) { 51 this.age = age; 52 } 53 54 public Integer getAuthority() { 55 return authority; 56 } 57 58 public void setAuthority(Integer authority) { 59 this.authority = authority; 60 } 61} 62
コントローラクラス
java
1package com.minutes.test; 2 3import java.util.ArrayList; 4import java.util.Collection; 5import java.util.List; 6 7import org.springframework.stereotype.Controller; 8import org.springframework.ui.Model; 9import org.springframework.web.bind.annotation.RequestMapping; 10import org.springframework.web.bind.annotation.RequestMethod; 11 12import com.minutes.test.model.User; 13 14@Controller 15public class HomeController { 16 17 @RequestMapping(value = "/input", method = RequestMethod.GET) 18 public String getHome(Model model) { 19 20 //リストを使う場合うまくプロパティが取得できない 21 List<User> userList = new ArrayList<User>(); 22 23 // リストじゃなしにモデルクラス単体で宣言したらうまくいった。 24 User user = new User(7,"name06", 55, 1); 25 26 // ユーザ情報追加 27 userList.add(new User(1,"name00", 23, 1)); 28 userList.add(new User(2,"name01", 44, 1)); 29 userList.add(new User(3,"name02", 23, 3)); 30 userList.add(new User(4,"name03", 43, 2)); 31 userList.add(new User(5,"name04", 22, 3)); 32 userList.add(new User(6,"name05", 21, 1)); 33 34 Collection<Object> collection = new ArrayList<>(); 35 36 collection.add(userList); 37 collection.add(user); 38 39 model.addAllAttributes(collection); 40 41 return "input"; 42 } 43 44 @RequestMapping(value = "/input", method = RequestMethod.POST) 45 public String postHome() { 46 return "input"; 47 } 48}
JSPファイル
jsp
1<%@ page contentType="text/html; charset=UTF-8"%> 2<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 3<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 4<%@ page session="false"%> 5<c:set var="contextPath" value="${pageContext.request.contextPath}" /> 6<html> 7<head> 8<title>input</title> 9<meta charset="utf-8"> 10</head> 11<body> 12 <div align="center"> 13 <h2>権限設定</h2> 14 <form method="POST" action="${contextPath}/input"> 15 <table border="1"> 16 <tr> 17 <th>ユーザID</th> 18 <th>ユーザ名</th> 19 <th>なし</th> 20 <th>回覧</th> 21 <th>承認</th> 22 </tr> 23 <c:forEach var="user" items="${userList}"> 24 <tr> 25 <td>${user.id}</td> 26 <td>${user.name}</td> 27 <!-- 権限情報追加 --> 28 29 <!-- モデルクラス単体で宣言したプロパティならうまくいった --> 30 <td><form:radiobutton path="user.authority" value="1" /> 31 32 <!-- こんな風に取得しようとしたがやはりうまくいかない… --> 33 <td><form:radiobutton path="userList.get(${user.id}).getAuthority()" value="2" /> 34 <td><form:radiobutton path="userList.get(${user.id}).authority" value="3" /> 35 </tr> 36 </c:forEach> 37 </table> 38 <input type="submit" /> 39 </form> 40 </div> 41</body> 42</html>
以上のことからform:radiobuttonのpath属性は普通にnewしたモデルクラスのプロパティは取得できることがわかります。しかし、Listを使って宣言したモデルクラスのプロパティの参照はうまくできなかったです。どなたかListを宣言したプロパティの参照の仕方が分かる人教えて下さい。(今のところできないように思える)
回答1件
あなたの回答
tips
プレビュー