如何在 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”是java类的属性名,存储在HttpServletRequest中,所以你可以简单的把它当成一个bean来使用!也不要忘记将 setter 和 getter 添加到您的秘密属性。
<%--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测试了这个
更新:这个例子工作不稳定。我知道为什么,但有时它设置属性,某处没有。如果您在一个 jsp 中有两个弹簧形式,则此方法可以为第一个设置属性而不为第二个设置属性,反之亦然。可能是因为 jsp:setProperty 在 spring 表单标签之后工作,可能不是。
人们经常错误地将某些值作为隐藏传递给表单,因为他们不能以其他方式将这些字段设置为更新到以前的值。例如,如果我在更新表单时没有传递一些值,这些字段就会变为空。但是,这是更新值的错误方法。有
@SessionAttributes("规则")
要做到这一点。更新后,您可以在更新完成后使用 (SessionStatus status) 参数和 status.setComplete() 设置会话完成。如果您想获得一些不在模型中的值,您可以随时使用 request.getParameter("yourinputname"); 您可以使用
输入类型="隐藏"
如果您想在 javascript 之类的某些部分中使用,请设置一些值(如果使用
${somevalueIdontwanttoshow}
不起作用)。
如果您真的想访问隐藏的文件,请尝试使用
request.getParameter("yourfiedl")
在查看绑定错误之前。