質問編集履歴
4
エラーメッセージに機密情報があったため削除した
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,170 +1,128 @@
|
|
1
|
-
##やりたいこと
|
2
|
-
SpringBootとThymeleafでWEBアプリを開発しております。
|
3
|
-
テーブルの端にチェックボックスを用意し、ボタンを押すとチェックした項目をすべてDBから削除するような処理を作ろうとしています。
|
4
|
-
テーブルの表示まではできたのですが、Contorollerに受け渡すことができず質問させていただきました。
|
5
|
-
Excelのスクリーンショットですが完成イメージです↓
|
6
|
-
※削除ボタンを押すとDBから「tanaka」と「yamada」が削除されるイメージです。
|
7
|
-

|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
##ソースコード
|
12
|
-
```Java
|
13
|
-
import lombok.Data;
|
14
|
-
|
15
|
-
@Data
|
16
|
-
public class Person {
|
17
|
-
private int id;
|
18
|
-
private String name;
|
19
|
-
private int age;
|
20
|
-
private boolean checked;
|
21
|
-
|
22
|
-
public Person(int id, String name, int age, boolean checked) {
|
23
|
-
this.setId(id);
|
24
|
-
this.setName(name);
|
25
|
-
this.setAge(age);
|
26
|
-
this.setChecked(checked);
|
27
|
-
}
|
28
|
-
}
|
29
|
-
|
30
|
-
```
|
31
|
-
|
32
|
-
```Java
|
33
|
-
import java.util.List;
|
34
|
-
import lombok.Data;
|
35
|
-
|
36
|
-
@Data
|
37
|
-
public class PersonForm {
|
38
|
-
private List<Person> personList;
|
39
|
-
}
|
40
|
-
|
41
|
-
```
|
42
|
-
|
43
|
-
```Java
|
44
|
-
//Controller
|
45
|
-
@RequestMapping("/editPersonList")
|
46
|
-
public String editPersonList(Model model) {
|
47
|
-
Person person1 = new Person(1, "tanaka", 30, false);
|
48
|
-
Person person2 = new Person(2, "tanaka", 40, false);
|
49
|
-
Person person3 = new Person(3, "tanaka", 50, false);
|
50
|
-
|
51
|
-
List<Person> personList = new ArrayList<Person>();
|
52
|
-
personList.add(person1);
|
53
|
-
personList.add(person2);
|
54
|
-
personList.add(person3);
|
55
|
-
|
56
|
-
PersonForm personForm = new PersonForm();
|
57
|
-
personForm.setPersonList(personList);
|
58
|
-
|
59
|
-
model.addAttribute("personForm", personForm);
|
60
|
-
|
61
|
-
return "/editPersonList";
|
62
|
-
}
|
63
|
-
|
64
|
-
@PostMapping("/updatePersonList")
|
65
|
-
public String updatePersonList(@ModelAttribute PersonForm personForm) {
|
66
|
-
// DB更新処理
|
67
|
-
System.out.println(personForm);
|
68
|
-
|
69
|
-
return "redirect:/top_page";
|
70
|
-
}
|
71
|
-
```
|
72
|
-
|
73
|
-
```html
|
74
|
-
<!DOCTYPE html>
|
75
|
-
<html lang="jp">
|
76
|
-
<html xmlns:th="http://www.thymeleaf.org">
|
77
|
-
|
78
|
-
<head>
|
79
|
-
<title>ユーザー編集</title>
|
80
|
-
<meta charset="UTF-8">
|
81
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
82
|
-
<link rel="stylesheet" type="text/css" href="/css/new_common.css">
|
83
|
-
</head>
|
84
|
-
|
85
|
-
<body>
|
86
|
-
<div th:replace="header_design :: header_design"></div>
|
87
|
-
<div style="margin-top: 100px;">テスト用画面</div>
|
88
|
-
<p>ユーザ一覧</p>
|
89
|
-
<form th:action="@{/updatePersonList}" th:method="post" th:object="${personForm}">
|
90
|
-
<table>
|
91
|
-
<thead>
|
92
|
-
<tr>
|
93
|
-
<td>ID</td>
|
94
|
-
<td>名前</td>
|
95
|
-
<td>年齢</td>
|
96
|
-
<td>削除フラグ</td>
|
97
|
-
</tr>
|
98
|
-
</thead>
|
99
|
-
<tbody>
|
100
|
-
<th:block th:each="person,stat : *{personList}">
|
101
|
-
<tr>
|
102
|
-
<td><input type="text" th:field="*{personList[__${stat.index}__].id}"></td>
|
103
|
-
<td><input type="text" th:field="*{personList[__${stat.index}__].name}"></td>
|
104
|
-
<td><input type="text" th:field="*{personList[__${stat.index}__].age}"></td>
|
105
|
-
<td><input type="checkbox" th:field="*{personList[__${stat.index}__].checked}"></td>
|
106
|
-
<!-- ↓試したけどエラーが発生した-->
|
107
|
-
<!-- <td><input type="text" th:field="${person.id}"></td>
|
108
|
-
<td><input type="text" th:field="${person.name}"></td>
|
109
|
-
<td><input type="text" th:field="${person.age}"></td>
|
110
|
-
<td><input type="checkbox" th:field="${person.checked}"></td> -->
|
111
|
-
</tr>
|
112
|
-
</th:block>
|
113
|
-
</tbody>
|
114
|
-
</table>
|
115
|
-
<input type="submit" value="更新">
|
116
|
-
</form>
|
117
|
-
</body>
|
118
|
-
|
119
|
-
</html>
|
120
|
-
```
|
121
|
-
|
122
|
-
##エラーメッセージ
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
There was an unexpected error (type=Internal Server Error, status=500).
|
129
|
-
Invalid property 'personList[0]' of bean class [com.example.mistuto.model.PersonForm]: Illegal attempt to get property 'personList' threw exception; nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property 'personList' of bean class [com.example.mistuto.model.PersonForm]: Could not instantiate property type [com.example.mistuto.model.Person] to auto-grow nested property path; nested exception is java.lang.NoSuchMethodException: com.example.mistuto.model.Person.<init>()
|
130
|
-
org.springframework.beans.InvalidPropertyException: Invalid property 'personList[0]' of bean class [com.example.mistuto.model.PersonForm]: Illegal attempt to get property 'personList' threw exception; nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property 'personList' of bean class [com.example.mistuto.model.PersonForm]: Could not instantiate property type [com.example.mistuto.model.Person] to auto-grow nested property path; nested exception is java.lang.NoSuchMethodException: com.example.mistuto.model.Person.<init>()
|
131
|
-
at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:707)
|
132
|
-
at org.springframework.beans.AbstractNestablePropertyAccessor.getNestedPropertyAccessor(AbstractNestablePropertyAccessor.java:839)
|
133
|
-
at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyAccessorForPropertyPath(AbstractNestablePropertyAccessor.java:816)
|
134
|
-
at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:256)
|
135
|
-
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:97)
|
136
|
-
at org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:848)
|
137
|
-
at org.springframework.validation.DataBinder.doBind(DataBinder.java:744)
|
138
|
-
at org.springframework.web.bind.WebDataBinder.doBind(WebDataBinder.java:197)
|
139
|
-
at org.springframework.web.bind.ServletRequestDataBinder.bind(ServletRequestDataBinder.java:107)
|
140
|
-
at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.bindRequestParameters(ServletModelAttributeMethodProcessor.java:158)
|
141
|
-
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:160)
|
142
|
-
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
|
143
|
-
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167)
|
144
|
-
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)
|
145
|
-
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
|
146
|
-
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888)
|
147
|
-
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
|
148
|
-
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
|
149
|
-
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
|
150
|
-
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
|
151
|
-
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
|
152
|
-
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
|
153
|
-
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
|
154
|
-
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
|
155
|
-
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
|
156
|
-
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
|
157
|
-
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
|
158
|
-
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
|
159
|
-
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
|
160
|
-
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
|
161
|
-
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320)
|
162
|
-
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:126)
|
163
|
-
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90)
|
164
|
-
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
|
165
|
-
```
|
166
|
-
|
167
|
-
参考にさせて頂いたサイト
|
168
|
-
https://area-b.com/blog/2015/02/04/2009/
|
169
|
-
|
1
|
+
##やりたいこと
|
2
|
+
SpringBootとThymeleafでWEBアプリを開発しております。
|
3
|
+
テーブルの端にチェックボックスを用意し、ボタンを押すとチェックした項目をすべてDBから削除するような処理を作ろうとしています。
|
4
|
+
テーブルの表示まではできたのですが、Contorollerに受け渡すことができず質問させていただきました。
|
5
|
+
Excelのスクリーンショットですが完成イメージです↓
|
6
|
+
※削除ボタンを押すとDBから「tanaka」と「yamada」が削除されるイメージです。
|
7
|
+

|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
##ソースコード
|
12
|
+
```Java
|
13
|
+
import lombok.Data;
|
14
|
+
|
15
|
+
@Data
|
16
|
+
public class Person {
|
17
|
+
private int id;
|
18
|
+
private String name;
|
19
|
+
private int age;
|
20
|
+
private boolean checked;
|
21
|
+
|
22
|
+
public Person(int id, String name, int age, boolean checked) {
|
23
|
+
this.setId(id);
|
24
|
+
this.setName(name);
|
25
|
+
this.setAge(age);
|
26
|
+
this.setChecked(checked);
|
27
|
+
}
|
28
|
+
}
|
29
|
+
|
30
|
+
```
|
31
|
+
|
32
|
+
```Java
|
33
|
+
import java.util.List;
|
34
|
+
import lombok.Data;
|
35
|
+
|
36
|
+
@Data
|
37
|
+
public class PersonForm {
|
38
|
+
private List<Person> personList;
|
39
|
+
}
|
40
|
+
|
41
|
+
```
|
42
|
+
|
43
|
+
```Java
|
44
|
+
//Controller
|
45
|
+
@RequestMapping("/editPersonList")
|
46
|
+
public String editPersonList(Model model) {
|
47
|
+
Person person1 = new Person(1, "tanaka", 30, false);
|
48
|
+
Person person2 = new Person(2, "tanaka", 40, false);
|
49
|
+
Person person3 = new Person(3, "tanaka", 50, false);
|
50
|
+
|
51
|
+
List<Person> personList = new ArrayList<Person>();
|
52
|
+
personList.add(person1);
|
53
|
+
personList.add(person2);
|
54
|
+
personList.add(person3);
|
55
|
+
|
56
|
+
PersonForm personForm = new PersonForm();
|
57
|
+
personForm.setPersonList(personList);
|
58
|
+
|
59
|
+
model.addAttribute("personForm", personForm);
|
60
|
+
|
61
|
+
return "/editPersonList";
|
62
|
+
}
|
63
|
+
|
64
|
+
@PostMapping("/updatePersonList")
|
65
|
+
public String updatePersonList(@ModelAttribute PersonForm personForm) {
|
66
|
+
// DB更新処理
|
67
|
+
System.out.println(personForm);
|
68
|
+
|
69
|
+
return "redirect:/top_page";
|
70
|
+
}
|
71
|
+
```
|
72
|
+
|
73
|
+
```html
|
74
|
+
<!DOCTYPE html>
|
75
|
+
<html lang="jp">
|
76
|
+
<html xmlns:th="http://www.thymeleaf.org">
|
77
|
+
|
78
|
+
<head>
|
79
|
+
<title>ユーザー編集</title>
|
80
|
+
<meta charset="UTF-8">
|
81
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
82
|
+
<link rel="stylesheet" type="text/css" href="/css/new_common.css">
|
83
|
+
</head>
|
84
|
+
|
85
|
+
<body>
|
86
|
+
<div th:replace="header_design :: header_design"></div>
|
87
|
+
<div style="margin-top: 100px;">テスト用画面</div>
|
88
|
+
<p>ユーザ一覧</p>
|
89
|
+
<form th:action="@{/updatePersonList}" th:method="post" th:object="${personForm}">
|
90
|
+
<table>
|
91
|
+
<thead>
|
92
|
+
<tr>
|
93
|
+
<td>ID</td>
|
94
|
+
<td>名前</td>
|
95
|
+
<td>年齢</td>
|
96
|
+
<td>削除フラグ</td>
|
97
|
+
</tr>
|
98
|
+
</thead>
|
99
|
+
<tbody>
|
100
|
+
<th:block th:each="person,stat : *{personList}">
|
101
|
+
<tr>
|
102
|
+
<td><input type="text" th:field="*{personList[__${stat.index}__].id}"></td>
|
103
|
+
<td><input type="text" th:field="*{personList[__${stat.index}__].name}"></td>
|
104
|
+
<td><input type="text" th:field="*{personList[__${stat.index}__].age}"></td>
|
105
|
+
<td><input type="checkbox" th:field="*{personList[__${stat.index}__].checked}"></td>
|
106
|
+
<!-- ↓試したけどエラーが発生した-->
|
107
|
+
<!-- <td><input type="text" th:field="${person.id}"></td>
|
108
|
+
<td><input type="text" th:field="${person.name}"></td>
|
109
|
+
<td><input type="text" th:field="${person.age}"></td>
|
110
|
+
<td><input type="checkbox" th:field="${person.checked}"></td> -->
|
111
|
+
</tr>
|
112
|
+
</th:block>
|
113
|
+
</tbody>
|
114
|
+
</table>
|
115
|
+
<input type="submit" value="更新">
|
116
|
+
</form>
|
117
|
+
</body>
|
118
|
+
|
119
|
+
</html>
|
120
|
+
```
|
121
|
+
|
122
|
+
##エラーメッセージ
|
123
|
+
諸事情により削除させていただきました
|
124
|
+
|
125
|
+
参考にさせて頂いたサイト
|
126
|
+
https://area-b.com/blog/2015/02/04/2009/
|
127
|
+
|
170
128
|
よろしくお願いいたします。
|
3
文言の修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -103,7 +103,7 @@
|
|
103
103
|
<td><input type="text" th:field="*{personList[__${stat.index}__].name}"></td>
|
104
104
|
<td><input type="text" th:field="*{personList[__${stat.index}__].age}"></td>
|
105
105
|
<td><input type="checkbox" th:field="*{personList[__${stat.index}__].checked}"></td>
|
106
|
-
<!-- ↓エラーが発生
|
106
|
+
<!-- ↓試したけどエラーが発生した-->
|
107
107
|
<!-- <td><input type="text" th:field="${person.id}"></td>
|
108
108
|
<td><input type="text" th:field="${person.name}"></td>
|
109
109
|
<td><input type="text" th:field="${person.age}"></td>
|
2
タイトルの修正
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
テーブルに表示したデータを編集し、全行一括で更新したい
|
body
CHANGED
File without changes
|
1
タイトルの修正
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
フォームをリスト状にして
|
1
|
+
フォームをリスト状にしてテーブルに表示し、編集したい
|
body
CHANGED
File without changes
|