質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Q&A

0回答

1538閲覧

RequestContextListener#requestInitializedのチェックの意味が理解できない。

退会済みユーザー

退会済みユーザー

総合スコア0

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

0グッド

0クリップ

投稿2018/08/02 10:59

Springのリスナークラスの処理が分かりません。

Java

1/* 2 * Copyright 2002-2015 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package org.springframework.web.context.request; 18 19import javax.servlet.ServletRequestEvent; 20import javax.servlet.ServletRequestListener; 21import javax.servlet.http.HttpServletRequest; 22 23import org.springframework.context.i18n.LocaleContextHolder; 24 25/** 26 * Servlet listener that exposes the request to the current thread, 27 * through both {@link org.springframework.context.i18n.LocaleContextHolder} and 28 * {@link RequestContextHolder}. To be registered as listener in {@code web.xml}. 29 * 30 * <p>Alternatively, Spring's {@link org.springframework.web.filter.RequestContextFilter} 31 * and Spring's {@link org.springframework.web.servlet.DispatcherServlet} also expose 32 * the same request context to the current thread. In contrast to this listener, 33 * advanced options are available there (e.g. "threadContextInheritable"). 34 * 35 * <p>This listener is mainly for use with third-party servlets, e.g. the JSF FacesServlet. 36 * Within Spring's own web support, DispatcherServlet's processing is perfectly sufficient. 37 * 38 * @author Juergen Hoeller 39 * @since 2.0 40 * @see javax.servlet.ServletRequestListener 41 * @see org.springframework.context.i18n.LocaleContextHolder 42 * @see RequestContextHolder 43 * @see org.springframework.web.filter.RequestContextFilter 44 * @see org.springframework.web.servlet.DispatcherServlet 45 */ 46public class RequestContextListener implements ServletRequestListener { 47 48 private static final String REQUEST_ATTRIBUTES_ATTRIBUTE = 49 RequestContextListener.class.getName() + ".REQUEST_ATTRIBUTES"; 50 51 52 @Override 53 public void requestInitialized(ServletRequestEvent requestEvent) { 54 if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) { 55 throw new IllegalArgumentException( 56 "Request is not an HttpServletRequest: " + requestEvent.getServletRequest()); 57 } 58 HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest(); 59 ServletRequestAttributes attributes = new ServletRequestAttributes(request); 60 request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes); 61 LocaleContextHolder.setLocale(request.getLocale()); 62 RequestContextHolder.setRequestAttributes(attributes); 63 } 64 65 @Override 66 public void requestDestroyed(ServletRequestEvent requestEvent) { 67 ServletRequestAttributes attributes = null; 68 Object reqAttr = requestEvent.getServletRequest().getAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE); 69 if (reqAttr instanceof ServletRequestAttributes) { 70 attributes = (ServletRequestAttributes) reqAttr; 71 } 72 RequestAttributes threadAttributes = RequestContextHolder.getRequestAttributes(); 73 if (threadAttributes != null) { 74 // We're assumably within the original request thread... 75 LocaleContextHolder.resetLocaleContext(); 76 RequestContextHolder.resetRequestAttributes(); 77 if (attributes == null && threadAttributes instanceof ServletRequestAttributes) { 78 attributes = (ServletRequestAttributes) threadAttributes; 79 } 80 } 81 if (attributes != null) { 82 attributes.requestCompleted(); 83 } 84 } 85 86} 87

このコードの

if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) { throw new IllegalArgumentException( "Request is not an HttpServletRequest: " + requestEvent.getServletRequest()); }

の部分は、HttpServletRequest以外のリクエストが来るということだと思うのですが、具体的にどのようなリクエストがあるのでしょうか?
また、それをどうしてチェックしなければならないのでしょうか?

Spring理解してる人、教えてください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問