Spring-MVC/ステップ・バイ・ステップ/PDFを生成する
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]
開始行:
[[Spring-MVC/ステップ・バイ・ステップ]]
2008/04/08からのアクセス回数 &counter;
#contents
** PDFを生成するための準備 [#ve4ad8e0]
Spring-MVCでは、ビューの戻り値としてHTML以外にExcelシート...
サポートしています。
今回は、PDFの生成方法を例に、HTML以外のビュー出力の手順を...
*** 必要なライブラリの追加 [#ub78c1d4]
PDFの生成には、iTextを使用します(日本語を使用するには、...
MVN RepositoryでiTextを検索し、以下のdependecyタグをpom.x...
#pre{{
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>1.4</version>
</dependency>
}}
いつものように、クラスパスを変更します。
#pre{{
$ rm .project .classpath
$ mvn eclipse:eclipse -DdownloadSources=true
}}
** PDF出力用のViewの作成 [#d2f367bf]
PDFを生成するためには、AbstractPdfViewのサブクラスを作成...
定義します。
RecipeのリストをPDFに出力するRecipePDFViewクラスを以下の...
#pre{{
package org.springframework.showcase.coverc.web;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.showcase.coverc.domain.Recipe;
import org.springframework.web.servlet.view.document.Abst...
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class RecipePDFView extends AbstractPdfView {
protected void buildPdfDocument(Map model, Document docu...
PdfWriter pdfWriter, HttpServletRequest request,
HttpServletResponse response) throws Exception {
List recpeList = (List)model.get("recipeList");
for(int i = 0; i < recpeList.size(); i++) {
String name = ((Recipe)recpeList.get(i)).getName();
document.add(new Paragraph(name));
}
}
}
}}
プラグラムの処理としては、
- モデルからrecipeListを取得する
- recipeListの各recipeを取り出す
- recipeのname属性を取り出し、documentにParagraphとして追...
です。これでRecipePDFViewは完成です。
** コントローラのメソッドの追加 [#g21c79d8]
Recipeのリスト出力は、SwitchBoardControllerクラスで処理し...
listPDFメソッドを追加します。
#pre{{
public ModelAndView listPDF(HttpServletRequest reques...
throws Exception {
return new ModelAndView("listPDF").addObject(this...
}
}}
listRecipesメソッドでは、ModelAndViewの生成に引数を指定し...
使用するには、ビューを指定する必要があります。そこで、Mod...
引数として"listPDF"を渡します。
これで、listPDFメソッドで返されるModelAndViewのビューに"l...
モデルは、listRecipesと同じです。addObjectでモデル名を省...
クラス名がモデル名になります。それが、配列またはリストの...
名前で登録されます。
これで、先ほどのRecipePDFViewでモデルを取り出す時、"recip...
納得できたと思います。
** view名称とViewを処理するクラスのマッピング [#r1ae5509]
最後にviewの名前とviewを処理クラスの関連付けを行います。
Spring-MVCでは複数のViewResolverを使用することができます。
この場合、order属性を指定して、名前解析の順序を指定します。
coverc-servlet.xmlに、resourceBundleViewResolverを以下の...
#pre{{
<bean id="resourceBundleViewResolver" class="org.springf...
<property name="basename" value="views" />
<property name="order" value="1" />
</bean>
}}
viewとView処理クラスの関係を記述したリソースファイルviews...
#pre{{
listPDF.class=org.springframework.showcase.coverc.web.Rec...
}}
** ブラウザーでの確認 [#h0c0a466]
mavenのjettyプラグインを起動した後、
#pre{{
$ mvn jetty:run
}}
ブラウザーで
#pre{{
http://localhost:8080/mvc-convention/switchboard/listPDF....
}}
と入力してください。
以下のような画面が出力されます。
#ref(listPdf.jpg);
** コメント [#a7a3fffe]
この記事は、
#vote(おもしろかった[7],そうでもない[1],わかりずらい[1])
皆様のご意見、ご希望をお待ちしております。
#comment_kcaptcha
終了行:
[[Spring-MVC/ステップ・バイ・ステップ]]
2008/04/08からのアクセス回数 &counter;
#contents
** PDFを生成するための準備 [#ve4ad8e0]
Spring-MVCでは、ビューの戻り値としてHTML以外にExcelシート...
サポートしています。
今回は、PDFの生成方法を例に、HTML以外のビュー出力の手順を...
*** 必要なライブラリの追加 [#ub78c1d4]
PDFの生成には、iTextを使用します(日本語を使用するには、...
MVN RepositoryでiTextを検索し、以下のdependecyタグをpom.x...
#pre{{
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>1.4</version>
</dependency>
}}
いつものように、クラスパスを変更します。
#pre{{
$ rm .project .classpath
$ mvn eclipse:eclipse -DdownloadSources=true
}}
** PDF出力用のViewの作成 [#d2f367bf]
PDFを生成するためには、AbstractPdfViewのサブクラスを作成...
定義します。
RecipeのリストをPDFに出力するRecipePDFViewクラスを以下の...
#pre{{
package org.springframework.showcase.coverc.web;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.showcase.coverc.domain.Recipe;
import org.springframework.web.servlet.view.document.Abst...
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class RecipePDFView extends AbstractPdfView {
protected void buildPdfDocument(Map model, Document docu...
PdfWriter pdfWriter, HttpServletRequest request,
HttpServletResponse response) throws Exception {
List recpeList = (List)model.get("recipeList");
for(int i = 0; i < recpeList.size(); i++) {
String name = ((Recipe)recpeList.get(i)).getName();
document.add(new Paragraph(name));
}
}
}
}}
プラグラムの処理としては、
- モデルからrecipeListを取得する
- recipeListの各recipeを取り出す
- recipeのname属性を取り出し、documentにParagraphとして追...
です。これでRecipePDFViewは完成です。
** コントローラのメソッドの追加 [#g21c79d8]
Recipeのリスト出力は、SwitchBoardControllerクラスで処理し...
listPDFメソッドを追加します。
#pre{{
public ModelAndView listPDF(HttpServletRequest reques...
throws Exception {
return new ModelAndView("listPDF").addObject(this...
}
}}
listRecipesメソッドでは、ModelAndViewの生成に引数を指定し...
使用するには、ビューを指定する必要があります。そこで、Mod...
引数として"listPDF"を渡します。
これで、listPDFメソッドで返されるModelAndViewのビューに"l...
モデルは、listRecipesと同じです。addObjectでモデル名を省...
クラス名がモデル名になります。それが、配列またはリストの...
名前で登録されます。
これで、先ほどのRecipePDFViewでモデルを取り出す時、"recip...
納得できたと思います。
** view名称とViewを処理するクラスのマッピング [#r1ae5509]
最後にviewの名前とviewを処理クラスの関連付けを行います。
Spring-MVCでは複数のViewResolverを使用することができます。
この場合、order属性を指定して、名前解析の順序を指定します。
coverc-servlet.xmlに、resourceBundleViewResolverを以下の...
#pre{{
<bean id="resourceBundleViewResolver" class="org.springf...
<property name="basename" value="views" />
<property name="order" value="1" />
</bean>
}}
viewとView処理クラスの関係を記述したリソースファイルviews...
#pre{{
listPDF.class=org.springframework.showcase.coverc.web.Rec...
}}
** ブラウザーでの確認 [#h0c0a466]
mavenのjettyプラグインを起動した後、
#pre{{
$ mvn jetty:run
}}
ブラウザーで
#pre{{
http://localhost:8080/mvc-convention/switchboard/listPDF....
}}
と入力してください。
以下のような画面が出力されます。
#ref(listPdf.jpg);
** コメント [#a7a3fffe]
この記事は、
#vote(おもしろかった[7],そうでもない[1],わかりずらい[1])
皆様のご意見、ご希望をお待ちしております。
#comment_kcaptcha
ページ名:
SmartDoc