- バージョンの確認・変更
- 文字化け回避
- コンテキストルート
- DI
- indexファイル
- 画面遷移
- Controller↔jsp間の値の受け渡し(modelAttributeを使用)
- Controller↔jsp間の値の受け渡し(modelAttributeを使用しない)
バージョンの確認・変更
<properties> <java-version>1.8</java-version> <org.springframework-version>5.3.0</org.springframework-version> <org.aspectj-version>1.6.10</org.aspectj-version> <org.slf4j-version>1.6.6</org.slf4j-version> </properties> <dependencies> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version> <exclusions> <!-- Exclude Commons Logging in favor of SLF4j --> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework-version}</version> </dependency> <!-- JDBC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${org.springframework-version}</version> </dependency> </dependencies>
文字化け回避
web.xmlに追記する
<filter> <filter-name>characterEncodingFilter</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
DI
SpringはDIを使用している。
インスタンスはシングルトンで使いまわしされるためメンバ変数が共有される。
メンバ変数を使用している場合は値が初期化されないため注意する。
詳しくは下の記事に記載
【Spring】DI - アルパカノフン
indexファイル
コンテキストルートのURLで開くファイル。
WEB-INFの配下に配置する。
例)http://localhost:8080/demo/
web.xmlで定義されている
画面遷移
コントローラー
・@RequestMappingとURLが紐づく
・returnに開くjsp名を指定する
例.http://localhost:8080/demo/offer_initDisplay
@Controller public class OfferController { @RequestMapping(value = "offer_initDisplay", method = RequestMethod.GET) public String initDisplay(Model model) { // webapp/WEB-INF/views/offer/offerInit.jspを開く return "offer/offerInit"; } }
【応用】
・クラスに@RequestMappingをつける
・@GetMapping/@PostMappingを使用する(Spring 4.3以降)
例.http://localhost:8080/demo/offer/offer_initDisplay
@Controller @RequestMapping("offer") public class OfferController { @GetMapping("offer_initDisplay") public String initDisplay(Model model) { return "offer/offerInit"; } @PostMapping("offer_confirm") public String confirm(Model model) { return "offer/confirmOffer"; } }
Controller↔jsp間の値の受け渡し(modelAttributeを使用)
modelAttributeを使用するとFormの値をFormでまとめて取得することができる
modelAttributeを使用したjspを呼び出すコントローラー
・jspのformでmodelAttributeを使用している場合、modelにつめて渡す必要がある
@Controller @RequestMapping("offer") public class OfferController { @GetMapping("offer_initDisplay") public String initDisplay(Model model) { // jspのmodelAttributeに渡す必要がある model.addAttribute("offerForm", new OfferForm()); return "offer/offer"; } }
jspの値渡し
・modelAttributeで格納先のFormを指定
・formactionで遷移先のコントローラーを指定
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <form:form modelAttribute="offerForm" method="post"> <label>ユーザーID:</label> <form:input path="userId"/><br> <label>名前:</label> <form:input path="name"/><br> <input type="submit" value="確認へ進む" formaction="offer_confirm"/> <input type="hidden" name="hiddenItem" value="hiddenValue"> </form:form> </body> </html>
コントローラーでの受け渡し
・@ModelAttributeでjspのformを受け取ることができる
・引数でjsp内のpathやnameを指定することでも受け取ることができる
・jspに渡す内容はmodelにつめる(model.addAttribute)
@Controller @RequestMapping("offer") public class ConfirmOffer { @PostMapping("offer_confirm") public String confirm(Model model, @ModelAttribute("offerForm") OfferForm form, // formの値を取得 @RequestParam(name="userId") String id, // これでも取得可能 String hiddenItem // これでも取得可能 ) { System.out.println(id); System.out.println(hiddenItem); // jspに値を渡す model.addAttribute("offerForm", form); return "offer/confirmOffer"; } }
jspの値受け取り
・コントローラーのmodelにつめた値を使用できる
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body > ${offerForm.userId}<br> ${offerForm.name} </body> </html>
画面イメージ
文字化けする場合は上記の「文字化け回避」の設定が必要
格納先がString型以外の場合、文字列を入力した場合はエラーになるので注意
Controller↔jsp間の値の受け渡し(modelAttributeを使用しない)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <form method="post"> <label>ユーザーID:</label> <input type="text" name="userId"/><br> <label>名前:</label> <input type="text" name="name"/><br> <input type="submit" value="確認へ進む" formaction="offer_confirm"/> </form> </body> </html>
【controller】
@RequestParamや引数を使用して値を受け取る
@Controller @RequestMapping("offer") public class ConfirmOffer { @PostMapping("offer_confirm") public String confirm(Model model, @RequestParam(name="userId") String id, @RequestParam(name="name") String name, String userId) { // @RequestParamなしでも取得可能。jspのnameと同じ名前にすること model.addAttribute("userId", id); model.addAttribute("name", name); return "offer/confirmOffer"; } }