Strutsでリンクの作成方法はいろいろなやり方があります。
<html:form action="/execute">
<html:select property="hoge">
<html:options name="menu.pulldown"/>
</html:select><html:errors property="hoge"/>
<br/>
<bean:define id="title" name="executeForm"
property="hoge" type="java.lang.String"/>
<%-- 変数名がexecuteFormのhogeというpropertyを
titleという変数名で参照。(型はString) --%>
<html:submit/><html:cancel/>
</html:form>
とするとformが作成できる
<html:link action="/execute">実行</html:link>
<html:link linkName="Anchor" />
<html:link page="hoge" >aaaa</html:link>
とすると
<a href="[contextpath]hoge">aaaa</a>
となる。つまりそのまま(doの補完や、/とかの補完すらしない)。
<html:rewrite page="hoge" />
とするとhtml上に
[contextpath]hoge
が出力されます。
struts-config.xmlで
<!-- ========== Global Forward Definitions =============================== --> <global-forwards type="org.apache.struts.action.ActionForward"> <forward name="hoge" path="/execute.do" /> </global-forwards>
と定義し、
<html:link forward="hoge" >aaaa</html:link> と記述すると <a href="[contextpath]/execute.do">aaaa</a>
となる。*1
ちなみにこのグローバルフォワードという機能は
<html:rewrite forward="hoge" />
と記述して、htmlに
[contextpath]/execute.do
と出力することもできます。リンクでなくてスタイルシートファイルの指定とかに使用しますね。
そのまま
<html:link href="hoge" >aaaa</html:link>
が
<a href="hoge">aaaa</a>
となる。
<%
Map map=new HashMap();
map.put("data1","value1");
map.put("data2","value2");
map.put("data3","value3");
map.put("data4","value4");
request.setAttribute("map",map);
%>
<html:link page="/execute.do" name="map">aaaa</html:link>
みたく、mapを指定すると、パラメータとなる。(nameでBeanを指定、propertyでmapを指定してもよい。)
RequestやSession,PageContext,Applicationなどあるスコープ内にのインスタンスが保持するフィールドの値をリンクのパラメータにしたいときがあります*2が、こうやれば出来ます。以下の例ではinstanceというキーであるスコープにインスタンスがあるときですが、
<html:link paramName="instance"
paramId="fieldParam"
paramProperty="field"
page="/execute.do?action=edit">編集</html:link>
とすると
/execute.do?action=edit&fieldParam=[instanceインスタンスのfieldというフィールドの値]
となります。
まずBeanのプロパティに変数名を付けて <bean:define id="userid" name="record" property="userId" type="java.lang.String"/> リンクタグに渡す <html:link action="/hoge" paramId="userid" paramName="userid"> (うえは/hoge.do?[userid<-paramIdの文字列]=beanのプロパティ値となる) <bean:write name="record" property="userId" /> </html:link>
これでOK!これより上のやり方の方がスマートですね。
この記事は
現在のアクセス:75422