私はSpring 3.1の新しいFlash Attributeサポートを使用して、コントローラー内のRedirectAttributes
オブジェクトのフラッシュ属性を設定し、リダイレクトを呼び出します。そのリダイレクト要求はフィルタによって捕捉され、フィルタはそれを意図したJSPにメリットのある方法で送信します。問題:フィルタのdoFilter()
メソッドまたはJSPからのフラッシュ属性が表示されません。非フラッシュ(URL)属性は、それを正常にします。リダイレクトを行うリダイレクトされたJSPで表示されないSpring MVC 3.1のFlash属性
コントローラー:
@RequestMapping("/pages/login")
public String login (HttpServletRequest request, Map<String, Object> model, RedirectAttributes redirectAttributes) {
model.put("userId", "batman");
String redirectUrl = request.getParameter("redirectUrl");
if (redirectUrl != null) {
redirectAttributes.addAttribute("attr1","ababab");
redirectAttributes.addFlashAttribute("flashAttr1", "flashflash");
for (Iterator<String> iterator = model.keySet().iterator(); iterator.hasNext();) {
String key = iterator.next();
redirectAttributes.addFlashAttribute(key, model.get(key));
}
return "redirect:"+redirectUrl;
} else {
return "pages/login";
}
}
この場合には面白い何もしないリダイレクトを拾っフィルタ:フィルタを次のようにリダイレクトされます
public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
//if (httpRequest.getSession().getAttribute("userId") == null) {
//...do some stuff here which invokes controller above as well as the redirect
//} else {
chain.doFilter(request, response);
//}
}
ページ:
...
<title>Test Web App 1</title>
</head>
<body>
<p>Flash attribute: <c:out value="${flashAttr1}"/></p>
<p>Welcome <c:out value="${userId}"/>!</p>
</body>
</html>
flashAttr1
もどちらもはページに表示されます。コントローラーセットがページのURLパラメーターに表示されるので、正常に動作しているように見えます(attr1
)。ここで
springfamework.web
を設定した後
log4j
からいくつかの出力である:私はURLのページに取られています、私は上に示してきたフィルタを簡単に停止後
19:15:44,406 DEBUG http-8080-1 view.ContentNegotiatingViewResolver:494 - Returni
ng redirect view [org.springframework.web.servlet.view.RedirectView: name 'redir
ect:http://my_hostname:8080/test-webapp-1/protected/protected_page.jsp';
URL [http://my_hostname:8080/test-webapp-1/protected/protected_page.jsp]]
19:15:44,406 DEBUG http-8080-1 servlet.DispatcherServlet:1155 -
Rendering view [org.springframework.web.servlet.view.RedirectView: name
'redirect:http://my_hostname:8080/test-webapp-1/protected/protected_page.jsp';
URL [http://my_hostname:8080/test-webapp-1/protected/protected_page.jsp]] in
DispatcherServlet with name 'dispatcher'
19:15:44,421 DEBUG http-8080-1 support.DefaultFlashMapManager:199 - Saving Flash
Map=[Attributes={userId=batman, flashAttr1=flashflash}, targetRequestPath=/test-
webapp-1/protected/protected_page.jsp, targetRequestParams={attr1=[ababab]}]
19:15:44,421 DEBUG http-8080-1 servlet.DispatcherServlet:913 - Successfully comp
leted request
http://my_hostname:8080/test-webapp-1/protected/protected_page.jsp?attr1=ababab
しかし、私が期待するJSPの属性は表示されません。上記のdoFilter()
メソッドでもデバッグして、リクエストのセッションでフラッシュ属性を見つけることができませんでした。
この時点で何が間違っているのか正確にはわかりません。これらのフラッシュ属性を除き、すべてが期待通りに機能します。状況をより明確にするために私が提供しなければならないことが他にもあるなら、私は満足しています。
そしてもちろん、私が持っている ' ' –
:-)(プロパティと一緒にモデルがURLのparamsとして属性を設定しないように)設定することは、あなたが削除できますフィルタリングして見ますか? – Abhi
これについて少し考えた後、私はこの問題の核心は実際にここに関わるリダイレクトがあるという事実に由来すると考えています。つまり、2つのHTTPセッションが存在します。自然に属性を1つに追加することは、他のものに影響を与えません。したがって、実際にこのアプローチを「修正」する方法は実際にはありません。むしろ、コントローラから別のドメインのページに属性を取得するには別の方法を考えなければなりませんが、URLクエリのパラメータを使って属性を取得する必要はありません。 –