Forums: Open Discussion (Thread #24092)

BLogicで設定したフォームをJSPでjavaオブジェクトとして取得したい (2009-10-01 10:16 by Anonymous #46175)

BLogicで設定したフォームの値をカスタムタグの属性に使用したいのですが、
javaのオブジェクトとして取得する方法が分からなかったので、
宜しければアドバイスをお願い致します。

各ファイルは下記のようにしています(抜粋)。

struts-config.xml
<form-beans>
<form-bean name="_form"
type="jp.terasoluna.fw.web.struts.form.DynaValidatorActionFormEx">
<form-property name="param1" type="java.lang.String" />
<form-property name="param2" type="java.lang.String" />
<form-property name="result1" type="java.lang.String" />
<form-property name="result2" type="java.lang.String" />
</form-bean>
</form-beans>

blogic-io.xml
<blogic-io>
<action path="/sampleTagBL">
<blogic-params bean-name="sample.SampleTagInput">
<set-property property="param1" blogic-property="param1" source="form" />
<set-property property="param2" blogic-property="param2" source="form" />
</blogic-param>
<blogic-result>
<set-property property="result1" blogic-property="result1" dest="form" />
<set-property property="result2" blogic-property="result2" dest="form" />
</blogic-result>
</action>
</blogic-io>

input.html
<form action="/sampleTagBL.do>
<input type="text" name="param1"><br>
<input type="text" name="param2"><br>
<input type="submit" name="submit" value="sabmit">
</form>

BLogicではSampleTagOutputを生成して
result1, result2をセットしてBLogicResultに設定しています。

jspの方では下記のような取得は成功しています。
<bean:write name="_form" property="result1" scope="session" />

単純に画面に値を表示する場合は上記でよいのですが、カスタムタグの属性に設定
したい場合、上記ではできないので下記のように取得しようと考えました。
※最初行ってしまい、beanタグがそのまま属性として渡されてしまいました。
<jsp:useBean id="_from" class="sample.SampleTagOutput" />
<%= _form.getResult1(); %>

getResult1()の結果はnullになります。
useBeanのscopeをpage, request, sessionなどにしたり、blogic-io.xmlのdestも
同様に変更したりしてみたのですが、BLogicクラスに設定した値が取得できません。

jsp/サーブレットは5年振りでいろいろ思い出しつつ進めているのですが、
もしよろしければ間違いを指摘して頂ければと思います。

Reply to #46175×

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login

RE: BLogicで設定したフォームをJSPでjavaオブジェクトとして取得したい (2009-10-01 10:37 by Anonymous #46176)

sessionから下記のように抜き出したところ値が取れるようになりました。

<%@ page import="org.apache.struts.action.DynaActionForm" %>
<%= ((DynaActionForm) session.getAttribute("_form")).getString("result1") %>

取れたは取れたのですが、こういったとり方で本当に良いのかどうか、もっとうまく取れる方法があれば
ぜひご指摘頂ければと思います。
Reply to #46175

Reply to #46176×

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login

RE: BLogicで設定したフォームをJSPでjavaオブジェクトとして取得したい (2009-10-01 11:26 by taromaru #46179)

strutsで提供されているタグであれば、
大抵、タグの属性に「name="_form" property="result1"」をセットすれば、
アクションフォーム(_form)に設定されたString(result1)をタグに渡せます。

この設定方法をサポートしていないタグに、
アクションフォーム(_form)に設定されたString(result1)を渡す必要がある場合は、
「<bean:define id="result1" name="_form" property="result1" type="java.lang.String" />」
を利用します。

上記記述をすると、
ページスコープとJSPのローカル変数に、
result1(←id属性で指定)というString(←type属性で指定)の変数が定義され、
アクションフォーム(_form)に設定されたString(result1)が格納されます。

bean:defineでresult1を定義した後であれば、
「<%= result1 %>」で参照できるようになります。
Reply to #46176

Reply to #46179×

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login

RE: BLogicで設定したフォームをJSPでjavaオブジェクトとして取得したい (2009-10-01 18:06 by Anonymous #46181)

アドバイス有難うございます。助かります。
私の記述よりもスマートですね。

試したところうまく取る事ができたのですが、1点気になる箇所があります。
<bean:define id="result1" name="_form" property="result1" type="java.lang.String" />
<bean:define id="result2" name="_form" property="result2" type="java.lang.String" />
<s:sampletag
param1='<%= new String(result1.getBytes("iso-8859-1"), "Windows-31J") %'
param1='<%= new String(result2.getBytes("iso-8859-1"), "Windows-31J") %' />
※sampletagは自作のカスタムタグです。

eclipseでJSPを作成しているのですが、result1, 2を使用する箇所でエラーが発生します。
ぐぐってみたところ、実行はできるとのことで試してみました。

エラーは特に気にしなくてもよいのでしょうか。
※jspのバリデーションチェックをオフにすれば消えるとのことですが・・・。

もうひとつ、beanからそのままカスタムタグの属性に設定すると日本語が
文字化けするため、上記のようにエンコーディングしていますが、
カスタムタグ内でエンコーディングするべきでしょうか。

テラソルナのカスタムタグではもらった属性をそのまま使用している
ようですが、JSP側で正しい文字コードを属性に設定するという方針で
考えてよいものでしょうか。

いろいろと質問ばかりで申し訳ありません。ご回答いただければ助かります。
Reply to #46179

Reply to #46181×

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login

RE: BLogicで設定したフォームをJSPでjavaオブジェクトとして取得したい (2009-10-02 10:36 by taromaru #46194)

> eclipseでJSPを作成しているのですが、result1, 2を使用する箇所でエラーが発生します。
私が使用しているEclipseではエラーは発生しませんが、
昔Eclipse2系を使用していたときは、確か
bean:defineで定義した変数をローカル変数として使用すると、
エディタがエラーとして認識していたと思います。

実機で動作するのであれば、問題ありません。

> もうひとつ、beanからそのままカスタムタグの属性に設定すると日本語が
> 文字化けするため、上記のようにエンコーディングしていますが、
> カスタムタグ内でエンコーディングするべきでしょうか。
カスタムタグの役割が、画面のHTMLの一部を出力するものであるという前提では、
カスタムタグでエンコーディングを操作することは無いはずです。

> new String(result1.getBytes("iso-8859-1"), "Windows-31J")
これは、
・Windows-31Jでエンコードされた文字列データが誤ってiso-8859-1でデコードされたものを、Windows-31Jでデコードし直す
・Windows-31Jでエンコードされる経路に、あえてiso-8859-1のデータを流す
のいずれかのときのコード(可能性が高いのは前者)です。

ここからは私の勘です。
・BLogicでは、param1かparam2のデータをresult1にセットしている
・input.htmlはWindows-31Jで記述されている、または、ブラウザがWindows-31J(IEなら「シフトJIS」)と認識している
・requestにsetCharacterEncodingでWindows-31Jをセットしていない
この時は、サーブレットフィルタで、
requestにsetCharacterEncodingメソッドでWindows-31Jをセットすれば解決すると思います。

勘が外れていれば、
Stringがどの時点で化けたのかを切り分ける必要があります。
Reply to #46181

Reply to #46194×

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login

RE: BLogicで設定したフォームをJSPでjavaオブジェクトとして取得したい (2009-10-02 13:50 by Anonymous #46198)

ご回答ありがとうございます。

> eclipseでJSPを作成しているのですが、result1, 2を使用する箇所でエラーが発生します。
3.4.1のeclipseを使用しています。
あまり気にしないことにします。

> もうひとつ、beanからそのままカスタムタグの属性に設定すると日本語が
はい、requestやresponseが自動変換でiso-8859-1に変換されていると認識しているので、
iso-8859-1からWindows-31Jに明示的に変換しました。

Blogicのメソッドが下記のため、requestやresponseのオブジェクトの取得
方法がわからなかったので、setCharacterEndoding()が行えませんでした。

public BLogicResult execute(SampleTagInput input)

・BLogicでは、param1かparam2のデータをresult1にセットしている
→引数でもらったinput.htmlのparam1, param2をSampleTagInputのbeanのresult1, result2に設定してBLogicResultに設定しています。
 またBLogic内で定数でも試してみましたが同様でした(ソースのエンコードがMS932になっていますが、request, responseにエンコード指定をしていないのでiso-8859-1二デフォルト変換されている認識です)。

・input.htmlはWindows-31Jで記述されている、または、ブラウザがWindows-31J(IEなら「シフトJIS」)と認識している
→はい、上記の通りです。

・requestにsetCharacterEncodingでWindows-31Jをセットしていない
この時は、サーブレットフィルタで、
requestにsetCharacterEncodingメソッドでWindows-31Jをセットすれば解決すると思います。
→先に書いたようにBlogic内でrequest, responseのオブジェクトの取得方法がわからなかったため、設定することができませんでした。
Reply to #46194

Reply to #46198×

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login

RE: BLogicで設定したフォームをJSPでjavaオブジェクトとして取得したい (2009-10-02 14:29 by taromaru #46199)

> またBLogic内で定数でも試してみましたが同様でした
これだけ予想外でした。
以下を試してみてください。

[JSP]
----------------------
<%@ page contentType="text/html; charset=Windows-31J"%>
----------------------

もし、JSPに直接日本語を書く場合は、以下
----------------------
<%@ page contentType="text/html; charset=Windows-31J" pageEncoding="Windows-31J"%>
----------------------
contentTypeのcharsetがレスポンスに使用するエンコードで、
pageEncodingがJSPをコンパイルするときのエンコード(=JSPファイルのエンコード)です。
(手元のtomcat5.5ではpageEncoding="Windows-31J"を書かなくても、
うまく動いてくれましたが、
pageEncodingのデフォルトは、"ISO-8859-1"です。)


>> この時は、サーブレットフィルタで、
>> requestにsetCharacterEncodingメソッドでWindows-31Jをセットすれば解決すると思います。
> →先に書いたようにBlogic内でrequest, responseのオブジェクトの取得方法がわからなかったため、設定することができませんでした。
個々のBLogicではなく、
サーブレットフィルタ(javax.servlet.Filterの実装クラス)を使用します。
全リクエストWindows-31J固定という簡易実装なら、以下のようになります。

[web.xml]
----------------------
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
hoge.SetCharacterEncodingFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
----------------------

[SetCharacterEncodingFilter.java]
----------------------
package hoge;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class SetCharacterEncodingFilter implements Filter {
private static final String THRU_FILTER_KEY = "THRU_FILTER_ENCODING";
private static final String ENCODING = "Windows-31J";
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (!this.moreFlag && request.getAttribute(THRU_FILTER_KEY) != null) {
chain.doFilter(request, response);
return;
}
request.setCharacterEncoding(ENCODING);
request.setAttribute(THRU_FILTER_KEY, THRU_FILTER_KEY);

chain.doFilter(request, response);
}
public void destroy() {
}
}
----------------------
Reply to #46198

Reply to #46199×

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login

RE: BLogicで設定したフォームをJSPでjavaオブジェクトとして取得したい (2009-10-02 16:01 by Anonymous #46200)

申し訳ありません。再度確認したところBLogic内で直に記述した日本語は文字化けしていませんでした。

いろいろと確認しながらだったため、確認ミスをしてしまったようです。
もしかしたらJUnit + cactus + tomcatでも確認をしているのですが、beginメソッドでrequestに
ソースに直に記述した日本語を設定しているのですが、この動きと混同してしまったのかもしれません。

filterの事も理解できました。
試しに上記の設定とクラスを作成して実行してみたところ、getBytesの処理を行わなくても
文字ばけしなくなりました。

最後に1点確認させてください。
上記のクラスにthis.moreFlagの変数がありますが、変数宣言が見当たらないのですが、
どういった場合にtrueになるような変数でしょうか(とりあえずコメントアウトして
実行しました)。

色々とありがとうございました。
Reply to #46199

Reply to #46200×

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login

RE: BLogicで設定したフォームをJSPでjavaオブジェクトとして取得したい (2009-10-02 16:14 by taromaru #46201)

>this.moreFlag
すみません。
手元にあるクラスから説明に必要な部分だけ抜き取ったつもりでしたが、
余計なものが混ざってしまいました。

これは端折り忘れたものなので、無視してください。
Reply to #46200

Reply to #46201×

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login

RE: BLogicで設定したフォームをJSPでjavaオブジェクトとして取得したい (2009-10-02 16:23 by Anonymous #46202)

すみません。ありがとうございます。
本当に色々教えて頂けて助かりました。
Reply to #46201

Reply to #46202×

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login