预先填充的< form:input>字段(spring-form.tld标签库) [英] Pre-populated <form:input> field (spring-form.tld tag library)

查看:256
本文介绍了预先填充的< form:input>字段(spring-form.tld标签库)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的jsp页面中有一个<form:form>,其中包含几个<form:input>字段(因此,字段是数据绑定的).这些字段中的一些由用户填充,但是有些(而不是等待用户输入某个值)需要通过spring控制器预先填充从另一个jsp页面发送到该页面的参数值.如何将该参数写入<form:input>,以便用户不必这样做?

I have a <form:form> in my jsp page with several <form:input> fields (so, fields are databound). Some of those fields are populated by user, but some, instead of waiting for user to enter some value, need to pre-populated with the value of parameter sent to this page from another jsp page, through the spring controller. How to write that parametar into <form:input> so user doesn't have to?

推荐答案

如果我正确理解了您的问题,则希望使用先前已提交的值来预填充某些字段.

If I understand your question correctly, you want to prepopulate some fields with values already submitted previously.

您需要了解的是,Spring希望通过Spring表单标签库将命令对象放入模型中,并将该命令对象中的值绑定到表单字段.

All you need to understand is that with the Spring form taglib, Spring expects you to put a command object in the model and will bind the values in that command object to the form fields.

如果您未在模型中为此命令对象指定键,则标记库将使用键命令"查找它.您可以使用form标记上的commandName属性来指定其他名称,例如:

If you don't specify the key for this command object in the model, the taglib will look for it with the key "command". You can specify a different name though with the commandName attribute on the form tag, such as:

<form:form commandName="myModelObject">

Spring现在将在名为myModelObject的模型中寻找一个用于绑定表单标签的对象.

Spring would now look for an object in the model named myModelObject to use to bind the form tags.

您接下来需要做的另一件事是在Spring表单的表单标签中,引用命令对象中的字段.因此,例如,假设您的命令对象有一个firstName字段,您将有一个如下所示的输入标签:

The other thing you then need to do is in the form tags in your Spring form, reference the fields in your command object. So, for example, let's say your command object has a field firstName, you'd have an input tag like this:

<form:input path="firstName" />

path属性告诉该属性应与命令对象中的哪些字段绑定.

The path attribute tells the attribute what fields in the command object it should bind with.

因此,您只需将命令对象放入模型中,并预先填充适当的字段,相应的表单字段就会预先填充这些值.例如,如果您将一个名称为myModelObject的对象放入模型中,该对象具有一个firstName字段,则该对象将使用该对象中当前设置的firstName进行预填充.

So then you simply put the command object in your model with the appropriate fields prepopulated and the corresponding form fields will have those values prepopulated. Such as if you put an object in the model with the name myModelObject that has a field firstName, it will be prepopulated with whatever firstName is currently set to in that object.

这篇关于预先填充的&lt; form:input&gt;字段(spring-form.tld标签库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆