Spring MVC 4.1 No. 004 URLの一部をパラメータとして受け取る
リクエストされるURLの一部、例えば/hoge/fooというアクセスのfooの値をパラメータとして受け取る場合には、@PathVariableアノテーションを使って受け取ることができます。
package com.example.spring.controller.c004; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/c004") public class C004Controller { @RequestMapping(value = "/pathVar1/{var}", method = RequestMethod.GET) public String pathVar(@PathVariable String var) { return "c004/pathVar"; } @RequestMapping(value = "/pathVar2/{var1}", method = RequestMethod.GET) public String pathVar2(@PathVariable("var1") String var) { return "c004/pathVar"; } }
パラメータは{}で指定し、その中の名称がパラメータ名となります。受け取るパラメータはメソッドの引数で@PathVariableアノテーションを付けて指定します。この時パラメータ名と変数名が同じ場合には何も指定は必要ありません。pathVar2メソッドのようにパラメータ名と変数名が異なる場合には、アノテーションの属性としてパラメータ名が必要になります。
表示用のJSPは次のようになります。@PathVariableで指定されたパラメータは、Spring MVCによって自動的にリクエストスコープの同名の属性に割り当てられます。そのため、JSP側ではリクエストスコープから値を持ってくるため、コントローラでは何もしていません。
<%@page contentType="text/html; charset=utf-8" %><%-- --%><!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>サンプル</title> </head> <body> varの値は <c:out value="${var}" /><br> var1の値は <c:out value="${var1}" /> </body> </html>
確認用のテストケースは次のとおりです。
package com.example.spring.controller.c004; import static org.hamcrest.CoreMatchers.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.web.context.WebApplicationContext; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring/spring-context.xml" }) public class C004ControllerTest { @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Before public void setup() { mockMvc = webAppContextSetup(wac).build(); } @Test public void pathVar_12345へのGET() throws Exception { mockMvc.perform(get("/c004/pathVar1/12345")).andExpect(status().isOk()) .andExpect(view().name("c004/pathVar")) .andExpect(request().attribute("var", is("12345"))); } @Test public void pathVar2_abcdeへのGET() throws Exception { mockMvc.perform(get("/c004/pathVar2/abcde")).andExpect(status().isOk()) .andExpect(view().name("c004/pathVar")) .andExpect(request().attribute("var1", is("abcde"))); } }
ソースは https://github.com/kuwalab/spring-mvc41 にあります。タグ004が今回のサンプルです。
まとめ http://kuwalab.hatenablog.jp/entry/spring
最初 http://kuwalab.hatenablog.jp/entry/spring_mvc41/001
前回 http://kuwalab.hatenablog.jp/entry/spring_mvc41/003
次回 http://kuwalab.hatenablog.jp/entry/spring_mvc41/005