web.xmlメモ用紙
内容を覚えられない・・・本に書いてある通りに書きたくてもクラス名が長くて書けない・・・ググるのが面倒くさい・・・だからメモしちゃう。 書き溜めておけばいつか役に立つって期待してる。
web-app
<?xml version="1.0" encoding="UTF-8"?> <web-app {ここの書き方のメモ}> </web-app>
Servlet 3.0
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
Servlet 4.0
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
servlet
<web-app ...> <servlet> {ここの書き方のメモ} </servlet> <servlet-mapping> {ここの書き方のメモ} </servlet-mapping> </web-app>
Spring MVCのフロントコントローラを利用するための設定
<!-- DispatcherServletクラスをサービレットコンテナに登録する --> <servlet> <servlet-name>api</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!-- サーブレットのcontextClassパラメータにAnnotationConfigWebApplicationContextクラスを指定する --> <param-name>contextClass</param-name> <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> </init-param> <init-param> <!-- サーブレットのcontextConfigLocationパラメータに作成した設定クラスを指定する --> <param-name>contextConfigLocation</param-name> <!-- AppConfigの内容は下記参照 --> <param-value>example.config.AppConfig</param-value> </init-param> </servlet> <!-- 定義したDispatcherServletを使用してリクエストをハンドリングするURLのパターンを指定する --> <servlet-mapping> <servlet-name>api</servlet-name> <!-- 全てのリクエストを定義したDispatcherServletを使用してハンドリングする --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
package example.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @EnableWebMvc @ComponentScan("example") public class AppConfig implements WebMvcConfigurer { }
filter
<?xml version="1.0" encoding="UTF-8"?> <web-app ...> <filter> {ここの書き方のメモ} </filter> <filter-mapping> {ここの書き方のメモ} </filter-mapping> </web-app>
入力値の日本語が文字化けしないようにするための設定
<!-- CharacterEncodingFilterクラスをサーブレットコンテナに登録する(入力値の日本語の文字化け対策用) --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <!-- サーブレットのencodingパラメータにリクエストパラメータの文字エンコーディング(UTF-8)を指定する --> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <!-- サーブレットのforceEncodingパラメータにリクエストおよびレスポンスのエンコーディング上書きするかを指定する --> <param-name>forceEncoding</param-name> <!-- true:encodingへ強制的に上書きされる --> <param-value>true</param-value> </init-param> </filter> <!-- CharacterEncodingFilterを適用するリクエストのURLパターンを指定する --> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <!-- 全てのリクエストを適用対象にする --> <url-pattern>/*</url-pattern> </filter-mapping>
ISO 8859-1(ISO Latin 1)以外の文字を扱う必要がある場合は、CharacterEncodingFilterを使用して適切な文字エンコーディングの指定が必要です。 また、サーブレットフィルタを複数登録する場合は、リクエストパラメータから値を取得するサーブレットフィルタより前にフィルタ処理が適用されるように登録してください。 順番が逆転すると文字化けしてしまいます。
第4章Spring MVC - Spring徹底入門 Spring FrameworkによるJavaアプリケーション開発
HiddenHttpMethodFilter
HiddenHttpMethodFilterを使用すると、クライアントとの物理的な通信はPOSTメソッドを使用しますが、サーブレットコンテナ内ではリクエストパラメータで送られてきた値に置き換えて処理を行うことができます。
第6章RESTful Webサービスの開発 - Spring徹底入門 Spring FrameworkによるJavaアプリケーション開発
<!-- HiddenHttpMethodFilterクラスをサーブレットコンテナに登録する --> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <!-- HiddenHttpMethodFilterを適用するとサーブレットコンテナ内でリクエストパラメータで送られてきた値に置き換えて処理を行うことができる --> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>