お世話になります。
初めてSpring MVC、Thymeleafを使用するので色々お聞きしてしまっていますが、識者の方々、ご教授いただければ幸いと思います。
実現したいこと
Thymeleafのフォームから送信した値をJavaで参照したい。
該当のソースコード
main.html
html
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4<meta charset="UTF-8"> 5<title>タイトル</title> 6</head> 7<body> 8 <form th:action="@{send.xhtml}" th:object="${MainFormData}" action="#" method="post" enctype="multipart/form-data"> 9 <select th:field="*{bumon}"> 10 <option th:each="element : ${bumonMap}" th:value="${element.key}">[[${element.value}]]</option> 11 </select> 12 <select th:field="*{tenpo}"> 13 <option th:each="element : ${tenpoMap}" th:value="${element.key}">[[${element.value}]]</option> 14 </select> 15 <input th:field="*{image}" type="file" accept="image/*"> 16 <input type="submit" value="送信"> 17 </form> 18</body> 19</html>
MainCtrl.java
java
1@Controller 2public class MainCtrl { 3 4 @Autowired 5 private MainFormIf mainFormIf; 6 7 @ModelAttribute("MainFormData") 8 public MainFormIf setupBind() { 9 return this.mainFormIf; 10 } 11 12 @RequestMapping("/send") 13 public String send(@ModelAttribute("MainFormData") MainFormIf mainFormData) { 14 System.out.println(mainFormData.getBumon()); 15 System.out.println(mainFormData.getTenpo()); 16 17 return "main"; 18 } 19}
MainForm.java
java
1@Component 2public class MainForm implements MainFormIf { 3 4 private String bumon = ""; 5 6 private String tenpo = ""; 7 8 private MultipartFile image; 9 10 /* 以下、accessorが続きます */ 11}
MainFormIf.java
java
1public interface MainFormIf { 2 3 String getBumon(); 4 5 void setBumon(String bumon); 6 7 String getTenpo(); 8 9 void setTenpo(String tenpo); 10 11 MultipartFile getImage(); 12 13 void setImage(MultipartFile image); 14}
web.xml
xml
1<?xml version="1.0" encoding="UTF-8"?> 2<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://xmlns.jcp.org/xml/ns/javaee" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 5 id="WebApp_ID" version="3.1"> 6 7 <!-- 一部のみ抜粋 --> 8 9 <servlet-mapping> 10 <servlet-name>dispatcherServlet</servlet-name> 11 <url-pattern>*.xhtml</url-pattern> 12 </servlet-mapping> 13 14</web-app>
発生している問題・エラーメッセージ
MainCtrl.javaの@RequestMapping("/send")の箇所でMainFormから送信されてきたmainFormDataに何も入っていない状態です。(MainForm.htmlの時点ではth:valueに値はセットされているのですが)
###開発環境
- OS Windows10 professional x64
- IDE Eclipse 4.8 Photon
- Thymeleaf 3.0.2.RELEASE
- 言語 Java
- TOMCAT 9.0.30
- Java 12
- プロジェクト形式 Marvenプロジェクト
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/14 02:06
2020/01/14 02:21
2020/01/14 02:51
2020/01/14 03:33
2020/01/14 04:35
2020/01/15 01:38
2020/01/15 05:19