Spring MVC 4.1 No. 028 セッションスコープにデータを格納
セッションにデータを格納する方法もリクエスト同様に複数あります。Servlet APIのHttpSessionを使用する方法、WebRequestを使用する方法などです。
WebRequestはリクエストスコープと同様にデータを格納する際にスコープを指定します。
今回は同一の画面に遷移する3つのメソッドを用意しています。最初のものはセッションにデータを格納するもの、2番目は何もしないもの、3番目はセッションをクリアするものになります。
package com.example.spring.controller.c028; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.context.request.WebRequest; @Controller @RequestMapping("/c028") public class C028Controller { @RequestMapping("/sessionStart") public String sessionScope1(HttpSession session, WebRequest webRequest) { session.setAttribute("session1", "httpSession"); webRequest.setAttribute("session2", "webRequest", WebRequest.SCOPE_SESSION); return "c028/sessionScope"; } @RequestMapping("/sessionScope") public String sessionScope2() { return "c028/sessionScope"; } @RequestMapping("/sessionClear") public String sessionScope3(HttpSession session) { session.invalidate(); return "c028/sessionScope"; } }
表示用のsessionScop.jspです。
<%@page contentType="text/html; charset=utf-8" %><%-- --%><!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>サンプル</title> </head> <body> HttpSession: <c:out value="${sessionScope.session1}" /><br> WebRequest: <c:out value="${sessionScope.session2}" /><br> <a href="sessionScope">セッションをクリアせず再表示</a><br> <a href="sessionClear">セッションをクリアして再表示</a> </body> </html>
確認用のテストケースは次のとおりです。
package com.example.spring.controller.c028; import static org.hamcrest.corematchers.*; import static org.junit.Assert.*; 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 java.util.UUID; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mock.web.MockHttpSession; 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 C028ControllerTest { @Autowired private WebApplicationContext wac; private MockMvc mockMvc; private MockHttpSession mockHttpSession; @Before public void setup() { mockMvc = webAppContextSetup(wac).build(); mockHttpSession = new MockHttpSession(wac.getServletContext(), UUID .randomUUID().toString()); } @Test public void requestScopeのGET() throws Exception { assertThat(mockHttpSession.getAttribute("session1"), is(nullValue())); assertThat(mockHttpSession.getAttribute("session2"), is(nullValue())); mockMvc.perform(get("/c028/sessionStart").session(mockHttpSession)) .andExpect(status().isOk()) .andExpect(view().name("c028/sessionScope")); assertThat(mockHttpSession.getAttribute("session1"), is("httpSession")); assertThat(mockHttpSession.getAttribute("session2"), is("webRequest")); // セッションは維持される mockMvc.perform(get("/c028/sessionScope").session(mockHttpSession)) .andExpect(view().name("c028/sessionScope")); assertThat(mockHttpSession.getAttribute("session1"), is("httpSession")); assertThat(mockHttpSession.getAttribute("session2"), is("webRequest")); // セッションは破棄される mockMvc.perform(get("/c028/sessionClear").session(mockHttpSession)) .andExpect(view().name("c028/sessionScope")); assertThat(mockHttpSession.isInvalid(), is(true)); } }
ソースは https://github.com/kuwalab/spring-mvc41 にあります。タグ028が今回のサンプルです。
まとめ http://kuwalab.hatenablog.jp/entry/spring
最初 http://kuwalab.hatenablog.jp/entry/spring_mvc41/001
前回 http://kuwalab.hatenablog.jp/entry/spring_mvc41/027
次回 http://kuwalab.hatenablog.jp/entry/spring_mvc41/029