Spring MVC Redirectでデータ保持
今回は、Flashを利用し、リダイレクト時のデータ保持をやります。リダイレクトで画面遷移を行うとデータが保持されませんが、今回のFlashを使用するか、Sessionを使用するとデータ保持を行うことができます。
Sessionでデータ保持すると破棄するかタイムアウトするまでデータを保持し続けるため、一時的にデータを保持したい場合は
Flashを使用します。
それではサンプルを作っていきます。 ベース部分は前回を参照して下さい。
設定ファイル
web.xml
前回と同様です。applicationContext.xml
前回と同様です。モデル
package spring.test;
public class UserModel {
private String id;
private String name;
private String message;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
今回用に少し内容を変更しています。
コントローラ
package spring.test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import spring.test.UserModel;
@Controller
public class LoginControler {
@ModelAttribute("UM")
public UserModel init() {
return new UserModel();
}
@RequestMapping(value = "/message", method = RequestMethod.GET)
public String message1() {
return "message";
}
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String loginGet() {
return "login";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String loginPost(@ModelAttribute("UM") UserModel userModel, Model model,
RedirectAttributes redirectAttributes) {
if (userModel.getId().equals("test")) {
userModel.setMessage("ログイン成功M");
model.addAttribute("msg", "ログイン成功A");
redirectAttributes.addFlashAttribute("msgR", "ログイン成功R");
return "login";
} else {
userModel.setMessage("ログイン失敗M");
model.addAttribute("msg", "ログイン失敗A");
redirectAttributes.addFlashAttribute("msgR", "ログイン失敗R");
return "redirect:/message";
}
}
}
ログイン成功時はログイン画面にメッセージを出し、ログイン失敗時はメッセージ画面にリダイレクトしています。
Jsp
login.jsp
<%@page import="spring.test.controller.LoginControler"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<head>
<title>ログイン</title>
</head>
<body>
<spring:url value="/login" var="url" htmlEscape="true" />
<form:form action="${url}" method="POST" modelAttribute="UM">
ID:<form:input path="id" />
<BR />
<input type="submit" value="ログイン" />
<BR />
<BR />
Message:<form:input path="message" />
</form:form>
ModelのAttribute:${msg}
<BR />
<BR /> RedirectのFlashAttribute:${msgR}
</body>
</html>
ModelAttribute、Model、Redirectのそれぞれでメッセージを出しています。
message.jsp
<%@page import="spring.test.controller.LoginControler"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<head>
<title>ログイン</title>
</head>
<body>
<a href="login">戻る</a>
<br />
<spring:url value="/login" var="url" htmlEscape="true" />
<form:form action="${url}" method="POST" modelAttribute="UM">
Message:
<form:input path="message" />
</form:form>
ModelのAttribute:${msg}
<br />
<br /> RedirectのFlashAttribute:${msgR}
</body>
</html>
こちらもModelAttribute、Model、Redirectのそれぞれでメッセージを出しています。
実行
ログイン失敗時
Redirectのみメッセージが取得できています。
ログイン成功時
ModelAttribute、Modelのメッセージが取得できています。
ページのトップへ戻る