Spring MVC 3.0 で非表示の値をフォームに渡す方法
を使用して隠しフィールドに値を割り当てることができません
<form:hidden path="test" />
。テスト フィールドの値を設定し、サーバー側でアクセスするにはどうすればよいですか。
ありがとう
Spring MVC 3.0 で非表示の値をフォームに渡す方法
を使用して隠しフィールドに値を割り当てることができません
<form:hidden path="test" />
。テスト フィールドの値を設定し、サーバー側でアクセスするにはどうすればよいですか。
ありがとう
<form:hidden path="test" style="display:none"/>
非表示のタグ以外に、次のようなフォームもあることに注意してください。
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<form:form action="/someAction" commandName="formBeanName" method="post">]
<%--
there you set needed properties
--%>
<form:hidden path="test" />
</form:form>
「formBeanName」は HttpServletRequest に格納された Java クラスの属性名なので、Bean として簡単に使用できます。また、秘密のプロパティにセッターとゲッターを追加することを忘れないでください。
<%--Set you secret property there--%>
<jsp:setProperty name="formBeanName" property="test" value="sercret"/>
<form:form action="/someAction" commandName="formBeanName" method="post">]
<%--
there you set needed properties
--%>
<form:hidden path="test" />
</form:form>
public class FormBean {
//other fileds
private String test;
public String getTest(){
return this.test;
}
public String setTest(Strign test){
return this.test = test;
}
}
PS私はSpring 3.1でこれをテストしました
更新:この例は不安定に動作します。理由はわかっていますが、プロパティを設定することもあれば、どこかに設定しないこともあります。1 つの jsp に 2 つのスプリング フォームがある場合、このアプローチでは最初のプロパティを設定して 2 番目のプロパティを設定しない、またはその逆を行うことができます。jsp:setProperty がスプリング フォーム タグの後に機能するため、機能しない可能性があります。
Often people wrongly pass some values as hidden to form, because they cannot otherwise set those fields in update to previous values. E.g If I don't pass some values while updating to the form, those fields become null. However this is wrong way to update values. There is
@SessionAttributes("Rules")
to do that. After you update, you can set the session to complete using (SessionStatus status) parameter and status.setComplete() after the update is done. If you want to get some values that is not in model you can always use request.getParameter("yourinputname"); You can use
input type="hidden"
to set some values if you want to use in some parts like javascript (if using
${somevalueIdontwanttoshow}
does not work).
And if you really want to access the hidden filed try using
request.getParameter("yourfiedl")
before looking at binding errors.