Spring-MVC/ステップ・バイ・ステップ/ソース解説
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]
開始行:
[[Spring-MVC/ステップ・バイ・ステップ]]
2008/04/14からのアクセス回数 &counter;
#contents
mvc-conventionのサンプルプログラムは、とてもシンプルで素...
規約をどのように使用したかを解説していないので、ソースを...
しにくいので、ここで説明します。
** 設定ファイルの説明 [#bcc9406e]
サンプルの設定ファイルは、
- web.xml
- applicationContext.xml
- coverc-servlet.xml
最後のcoverc-servlet.xmlは、「サーブレット名-servlet.xml...
デフォルトファイル名です。DsipatcherServletの初期化で使用...
*** web.xmlの設定 [#k104e4bd]
web.xmlでSpring-MVC特有の定義は、
#pre{{
<listener>
<listener-class>org.springframework.web.util.Log4...
</listener>
<listener>
<listener-class>org.springframework.web.context.C...
</listener>
<servlet>
<servlet-name>coverc</servlet-name>
<servlet-class>org.springframework.web.servlet.Di...
</servlet>
<servlet-mapping>
<servlet-name>coverc</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
}}
- ContextLoaderListenerは、WebアプリケーションのContextを...
です
- サーブレットの定義では、DispatcherServletを指定します
- サーブレット・マッピングは、xxxx.htmという要求をcoverc...
サーブレット名称は、Servlet関連Bean定義ファイルを読み込む...
サーブレット関連のBean定義ファイルを細分して定義する場合...
contextConfigLocationに以下のように定義ファイルを列記しま...
#pre{{
<servlet>
<servlet-name>cart</servlet-name>
<servlet-class>org.springframework.web.servlet.Di...
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servlet-def.xml,/WEB-INF/serv...
</init-param>
</servlet>
}}
*** applicationContext.xml [#b9d80f43]
applicationContext.xmlの定義は至って簡単です。
webアプリケーションで共通に利用されるrecipeManagerの定義...
#pre{{
<bean id="recipeManager" class="org.springframework.showc...
}}
*** coverc-servlet.xml [#ub9f2ead]
coverc-servlet.xmlでは、
- ControllerClassNameHandlerMappingの定義
- SwitchBoardControllerの定義
- EditRecipeControllerの定義
- viewNameTranslatorの定義
- viewResolverの定義
をしています。
ControllerClassNameHandlerMappingは、コントローラのクラス...
#pre{{
<bean class="org.springframework.web.servlet.mvc.supp...
}}
DefaultRequestToViewNameTranslatorは、ModelAndViewオブジ...
されていない場合に、デフォルトのview名称を返してくれるク...
詳しくは、[[Spring-MVC/ステップ・バイ・ステップ/Conventio...
#pre{{
<bean id="viewNameTranslator" class="org.springframew...
}}
InternalResourceViewResolverは、論理ビュー名称から、実際...
ここでは、xxxという論理ビュー名に対するビューファイルとし...
- prefixに/WEB-INF/jspを指定
- suffixに.jspを指定
しています。
#pre{{
<bean id="viewResolver" class="org.springframework.we...
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
}}
Spring2.0からBean定義ファイルで継承が使えるようになりまし...
baseRecipeControllerは、SwitchBoardController、EditRecipe...
この定義で、共通に使用する属性recipeManagerの定義をしてい...
親Bean定義では、クラスを指定する必要がなく、abstract="tru...
必要な属性のみを定義することができます。
#pre{{
<bean id="baseRecipeController" abstract="true">
<property name="recipeManager" ref="recipeManager...
</bean>
}}
SwitchBoardControllerの定義は、
#pre{{
<bean class="org.springframework.showcase.coverc.web....
parent="baseRecipeController"/>
}}
EditRecipeControllerは、SimpleFormControllerのサブクラス...
が必要です。
- commandNameは、コマンドオブジェクトの名称を指定します
- commandClassは、コマンドオブジェクトのクラス名を指定し...
- formViewは、フォームビューの論理名を指定します
- successViewは、成功時のビューを指定します、ここではswit...
#pre{{
<bean class="org.springframework.showcase.coverc.web....
parent="baseRecipeController">
<property name="commandName" value="recipe"/>
<property name="commandClass" value="org.springfr...
<property name="formView" value="editRecipe"/>
<property name="successView" value="redirect:swit...
</bean>
}}
** javaソースファイルの説明 [#rce5b505]
*** モデルクラス [#h1f7c550]
ドメインモデルクラスは、Recipe.java1個のみです。
属性にid, nameを持ち、それぞれのgetter/setterを定義し、cl...
きわめて簡素なものです。
#pre{{
public class Recipe implements Cloneable {
private Long id;
private String name;
// getter/setterは省略
public Object clone() throws CloneNotSupportedExcepti...
return super.clone();
}
}
}}
*** サービスクラス [#g38f3e78]
サービスクラスは、RecipeManagerインタフェースとその実装例...
あります。
RecipeManagerインタフェースは、
- findAll: すべてのRecipeを取り出すメソッド
- findById: 指定されたIdのRecipeを取り出すメソッド
- save: 指定されたRecipeを保存するメソッド
を定義しています。
#pre{{
public interface RecipeManager {
Collection findAll();
Recipe findById(Long id);
void save(Recipe user);
}
}}
StubRecipeManagerでは、TreeMapを使ってRecipeをメモリ上で...
loadRecipesメソッドでTreeMapに3個のRecipeをセットしていま...
- findAllでは、TreeMapから取りだしたRecipeをArrayListにセ...
従ってfindAllで戻されたオブジェクトの名前は、recipeListに...
詳しくは、[[Spring-MVC/ステップ・バイ・ステップ/Conventio...
#pre{{
public class StubRecipeManager implements RecipeManager {
private Map recipes = new TreeMap();
public StubRecipeManager() {
loadRecipes();
}
public void save(Recipe recipe) {
// passed in should be a clone - simply replace
putRecipe(recipe);
}
public Recipe findById(Long id) {
Recipe recipe = (Recipe) this.recipes.get(id);
if (recipe != null) {
return cloneRecipe(recipe);
}
return null;
}
public Collection findAll() {
List recipeList = new ArrayList();
Iterator itr = this.recipes.values().iterator();
while (itr.hasNext()) {
Recipe recipe = (Recipe) itr.next();
recipeList.add(cloneRecipe(recipe));
}
return recipeList;
}
}
}}
*** コントローラクラス [#wbd60333]
SwitchBoardControllerは、recipeManagerを属性に持ち、
- listRecipesメソッド
を定義しています。
ModelAndView(). addObject(findAllの戻り値);
としている部分が、規約の使い方を示すための例です。
findAllの戻り値はRecipeを要素に持つArrayListですので、そ...
詳しくは、[[Spring-MVC/ステップ・バイ・ステップ/Conventio...
#pre{{
public class SwitchBoardController extends MultiActionCon...
private RecipeManager recipeManager;
// setRecipeManagerは省略
public ModelAndView listRecipes(HttpServletRequest re...
return new ModelAndView().addObject(this.recipeMa...
}
}
}}
EditRecipeControllerも、recipeManagerを属性に持ち、
- formBackingObject : フォームにオブジェクトをセットする...
- doSubmitAction : submit要求によって呼び出されるメソッド
#pre{{
public class EditRecipeController extends SimpleFormContr...
private RecipeManager recipeManager;
// setRecipeManagerは省略
protected Object formBackingObject(HttpServletRequest...
long id = ServletRequestUtils.getRequiredLongPara...
Recipe recipe = this.recipeManager.findById(new L...
return recipe;
}
protected void doSubmitAction(Object object) throws E...
Recipe recipe = (Recipe) object;
this.recipeManager.save(recipe);
}
}
}}
本当にこれだけで、よいのかと思うくらい少ない量のソースで...
作られています。
** コメント [#qec290b0]
この記事は、
#vote(おもしろかった[17],そうでもない[0],わかりずらい[4])
皆様のご意見、ご希望をお待ちしております。
#comment_kcaptcha
終了行:
[[Spring-MVC/ステップ・バイ・ステップ]]
2008/04/14からのアクセス回数 &counter;
#contents
mvc-conventionのサンプルプログラムは、とてもシンプルで素...
規約をどのように使用したかを解説していないので、ソースを...
しにくいので、ここで説明します。
** 設定ファイルの説明 [#bcc9406e]
サンプルの設定ファイルは、
- web.xml
- applicationContext.xml
- coverc-servlet.xml
最後のcoverc-servlet.xmlは、「サーブレット名-servlet.xml...
デフォルトファイル名です。DsipatcherServletの初期化で使用...
*** web.xmlの設定 [#k104e4bd]
web.xmlでSpring-MVC特有の定義は、
#pre{{
<listener>
<listener-class>org.springframework.web.util.Log4...
</listener>
<listener>
<listener-class>org.springframework.web.context.C...
</listener>
<servlet>
<servlet-name>coverc</servlet-name>
<servlet-class>org.springframework.web.servlet.Di...
</servlet>
<servlet-mapping>
<servlet-name>coverc</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
}}
- ContextLoaderListenerは、WebアプリケーションのContextを...
です
- サーブレットの定義では、DispatcherServletを指定します
- サーブレット・マッピングは、xxxx.htmという要求をcoverc...
サーブレット名称は、Servlet関連Bean定義ファイルを読み込む...
サーブレット関連のBean定義ファイルを細分して定義する場合...
contextConfigLocationに以下のように定義ファイルを列記しま...
#pre{{
<servlet>
<servlet-name>cart</servlet-name>
<servlet-class>org.springframework.web.servlet.Di...
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servlet-def.xml,/WEB-INF/serv...
</init-param>
</servlet>
}}
*** applicationContext.xml [#b9d80f43]
applicationContext.xmlの定義は至って簡単です。
webアプリケーションで共通に利用されるrecipeManagerの定義...
#pre{{
<bean id="recipeManager" class="org.springframework.showc...
}}
*** coverc-servlet.xml [#ub9f2ead]
coverc-servlet.xmlでは、
- ControllerClassNameHandlerMappingの定義
- SwitchBoardControllerの定義
- EditRecipeControllerの定義
- viewNameTranslatorの定義
- viewResolverの定義
をしています。
ControllerClassNameHandlerMappingは、コントローラのクラス...
#pre{{
<bean class="org.springframework.web.servlet.mvc.supp...
}}
DefaultRequestToViewNameTranslatorは、ModelAndViewオブジ...
されていない場合に、デフォルトのview名称を返してくれるク...
詳しくは、[[Spring-MVC/ステップ・バイ・ステップ/Conventio...
#pre{{
<bean id="viewNameTranslator" class="org.springframew...
}}
InternalResourceViewResolverは、論理ビュー名称から、実際...
ここでは、xxxという論理ビュー名に対するビューファイルとし...
- prefixに/WEB-INF/jspを指定
- suffixに.jspを指定
しています。
#pre{{
<bean id="viewResolver" class="org.springframework.we...
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
}}
Spring2.0からBean定義ファイルで継承が使えるようになりまし...
baseRecipeControllerは、SwitchBoardController、EditRecipe...
この定義で、共通に使用する属性recipeManagerの定義をしてい...
親Bean定義では、クラスを指定する必要がなく、abstract="tru...
必要な属性のみを定義することができます。
#pre{{
<bean id="baseRecipeController" abstract="true">
<property name="recipeManager" ref="recipeManager...
</bean>
}}
SwitchBoardControllerの定義は、
#pre{{
<bean class="org.springframework.showcase.coverc.web....
parent="baseRecipeController"/>
}}
EditRecipeControllerは、SimpleFormControllerのサブクラス...
が必要です。
- commandNameは、コマンドオブジェクトの名称を指定します
- commandClassは、コマンドオブジェクトのクラス名を指定し...
- formViewは、フォームビューの論理名を指定します
- successViewは、成功時のビューを指定します、ここではswit...
#pre{{
<bean class="org.springframework.showcase.coverc.web....
parent="baseRecipeController">
<property name="commandName" value="recipe"/>
<property name="commandClass" value="org.springfr...
<property name="formView" value="editRecipe"/>
<property name="successView" value="redirect:swit...
</bean>
}}
** javaソースファイルの説明 [#rce5b505]
*** モデルクラス [#h1f7c550]
ドメインモデルクラスは、Recipe.java1個のみです。
属性にid, nameを持ち、それぞれのgetter/setterを定義し、cl...
きわめて簡素なものです。
#pre{{
public class Recipe implements Cloneable {
private Long id;
private String name;
// getter/setterは省略
public Object clone() throws CloneNotSupportedExcepti...
return super.clone();
}
}
}}
*** サービスクラス [#g38f3e78]
サービスクラスは、RecipeManagerインタフェースとその実装例...
あります。
RecipeManagerインタフェースは、
- findAll: すべてのRecipeを取り出すメソッド
- findById: 指定されたIdのRecipeを取り出すメソッド
- save: 指定されたRecipeを保存するメソッド
を定義しています。
#pre{{
public interface RecipeManager {
Collection findAll();
Recipe findById(Long id);
void save(Recipe user);
}
}}
StubRecipeManagerでは、TreeMapを使ってRecipeをメモリ上で...
loadRecipesメソッドでTreeMapに3個のRecipeをセットしていま...
- findAllでは、TreeMapから取りだしたRecipeをArrayListにセ...
従ってfindAllで戻されたオブジェクトの名前は、recipeListに...
詳しくは、[[Spring-MVC/ステップ・バイ・ステップ/Conventio...
#pre{{
public class StubRecipeManager implements RecipeManager {
private Map recipes = new TreeMap();
public StubRecipeManager() {
loadRecipes();
}
public void save(Recipe recipe) {
// passed in should be a clone - simply replace
putRecipe(recipe);
}
public Recipe findById(Long id) {
Recipe recipe = (Recipe) this.recipes.get(id);
if (recipe != null) {
return cloneRecipe(recipe);
}
return null;
}
public Collection findAll() {
List recipeList = new ArrayList();
Iterator itr = this.recipes.values().iterator();
while (itr.hasNext()) {
Recipe recipe = (Recipe) itr.next();
recipeList.add(cloneRecipe(recipe));
}
return recipeList;
}
}
}}
*** コントローラクラス [#wbd60333]
SwitchBoardControllerは、recipeManagerを属性に持ち、
- listRecipesメソッド
を定義しています。
ModelAndView(). addObject(findAllの戻り値);
としている部分が、規約の使い方を示すための例です。
findAllの戻り値はRecipeを要素に持つArrayListですので、そ...
詳しくは、[[Spring-MVC/ステップ・バイ・ステップ/Conventio...
#pre{{
public class SwitchBoardController extends MultiActionCon...
private RecipeManager recipeManager;
// setRecipeManagerは省略
public ModelAndView listRecipes(HttpServletRequest re...
return new ModelAndView().addObject(this.recipeMa...
}
}
}}
EditRecipeControllerも、recipeManagerを属性に持ち、
- formBackingObject : フォームにオブジェクトをセットする...
- doSubmitAction : submit要求によって呼び出されるメソッド
#pre{{
public class EditRecipeController extends SimpleFormContr...
private RecipeManager recipeManager;
// setRecipeManagerは省略
protected Object formBackingObject(HttpServletRequest...
long id = ServletRequestUtils.getRequiredLongPara...
Recipe recipe = this.recipeManager.findById(new L...
return recipe;
}
protected void doSubmitAction(Object object) throws E...
Recipe recipe = (Recipe) object;
this.recipeManager.save(recipe);
}
}
}}
本当にこれだけで、よいのかと思うくらい少ない量のソースで...
作られています。
** コメント [#qec290b0]
この記事は、
#vote(おもしろかった[17],そうでもない[0],わかりずらい[4])
皆様のご意見、ご希望をお待ちしております。
#comment_kcaptcha
ページ名:
SmartDoc