Spring MVC 4.1 No. 003 GETパラメータを受け取る(Optional)
Spring MVC 4.1から@RequestParamのパラメータにOptionalが使用できるようになりました。Optionalを使用することで、事実上パラメータは必須ではなくなります。例えば、fooパラメータを設定した場合を例にします。Stringで受けた場合にはfooパラメータが、ない場合にはメソッドが呼ばれません。対してOptionalにした場合にはメソッドが呼び出されます。次の例で確認します。
package com.example.spring.controller.c003; import java.util.Optional; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller @RequestMapping("/c003") public class C003Controller { @RequestMapping(value = "/getParam") public String getParam(@RequestParam Optional<String> foo, @RequestParam Optional<Integer> bar, Model model) { model.addAttribute("modelFoo", foo.orElse("nullです")); model.addAttribute("modelBar", bar.orElse(-9999)); return "c003/getParam"; } }
この例の場合には、Optional#orElseメソッドを使用してデフォルト値を設定しています。fooやbarパラメータが指定されなかった場合には、デフォルト値(orElse)が使用されます。
JSPは特別なことはありません。
<%@page contentType="text/html; charset=utf-8" %><%-- --%><!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>サンプル</title> </head> <body> fooの値は <c:out value="${foo}" /><br> barの値は <c:out value="${bar}" /><br> modelFooの値は <c:out value="${modelFoo}" /><br> modelBarの値は <c:out value="${modelBar}" /> </body> </html>
確認用のテストケースは次のとおりです。
package com.example.spring.controller.c003; 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 C003ControllerTest { @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Before public void setup() { mockMvc = webAppContextSetup(wac).build(); } @Test public void getParam_foo$abc_bar$123のGET() throws Exception { mockMvc.perform(get("/c003/getParam?foo=abc&bar=123")) .andExpect(status().isOk()) .andExpect(view().name("c003/getParam")) .andExpect(model().hasNoErrors()) .andExpect(model().attribute("modelFoo", is("abc"))) .andExpect(model().attribute("modelBar", is(123))) .andExpect(request().attribute("foo", is(nullValue()))) .andExpect(request().attribute("bar", is(nullValue()))); } @Test public void getParam_foo$null_bar$nullのGET() throws Exception { mockMvc.perform(get("/c003/getParam")).andExpect(status().isOk()) .andExpect(view().name("c003/getParam")) .andExpect(model().hasNoErrors()) .andExpect(model().attribute("modelFoo", is("nullです"))) .andExpect(model().attribute("modelBar", is(-9999))) .andExpect(request().attribute("foo", is(nullValue()))) .andExpect(request().attribute("bar", is(nullValue()))); } }
ソースは https://github.com/kuwalab/spring-mvc41 にあります。タグ003が今回のサンプルです。
まとめ http://kuwalab.hatenablog.jp/entry/spring
最初 http://kuwalab.hatenablog.jp/entry/spring_mvc41/001
前回 http://kuwalab.hatenablog.jp/entry/spring_mvc41/002
次回 http://kuwalab.hatenablog.jp/entry/spring_mvc41/004