动态形式在春天3 [英] Dynamic Form in Spring 3

查看:105
本文介绍了动态形式在春天3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有自定义的问题对象,我渲染成HTML表单元素。我想要能够动态地创建这些对象并从中生成一个动态的html表单。问题对象具有一个String属性来保存窗体中的结果。我如何才能在春天工作?



我的工作方式(似乎不理想),我有一个bean来回溯自定义表单。该bean有两个属性:一个用于保存要显示的问题的列表和一个用于保存结果的列表。 JSP有一个标签,它告诉它使用bean作为模型属性。然后我有一个自定义JSP标签,它接受列表并将其呈现为表单元素。元素被赋予了答案[n]的ID,Spring将把表单的结果放在支持bean中的List属性中。



有谁知道我能做什么这个更好?

解决方案

我的问题是缺乏经验。



我结束了这个(很简单的解释):



我的bean有以下字段:String questionType,String questionText,String answer。
我的bean列表是列表问题。



当我从bean列表中生成HTML时,我只需要创建每个表单的id / name元素与列表的名称匹配,以及列表中的bean的位置。



所以,如果我的问题列表如下所示:

  [0] {questionType =TEXT,questionText =你叫什么名字?answerText = null} 
[1 ] {questionType =TEXT,questionText =你的任务是什么?answerText = null}
[2] {questionType =TEXT,questionText =你最喜欢的颜色是什么?answerText = null }

然后,当我循环浏览列表时,我需要生成以下HTML:

 < div class =question> 
< p class =questionText>你叫什么名字?< / p>
< input type =textid =questions [0] .answerTextname =questions [0] .answerText/>
< / div>
< div class =question>
< p class =questionText>你的任务是什么?< / p>
< input type =textid =questions [1] .answerTextname =questions [1] .answerText/>
< / div>
< div class =question>
< p class =questionText>你最喜欢的颜色是什么?< / p>
< input type =textid =questions [2] .answerTextname =questions [2] .answerText/>
< / div>

当提交表单时,Spring会找到这些bean,并在其上调用setAnswerText(String value)表单数据。



我希望这有助于别人在Spring MVC旅程开始时绊脚石。


I have custom Question objects which I render into html form elements. I want to be able to dynamically create these objects and generate a dynamic html form from them. The question object has a String property to hold the result from the form. How can I get this to work in Spring?

The way I have it working (which doesn't seem ideal), is I have a bean to back the custom form. This bean has two properties: a List to hold the questions to be displayed and a List to hold the results. The JSP has a tag which tells it to use the bean as a model attribute. Then I have a custom JSP tag that takes the List and renders them into form elements. The elements are given IDs of answer[n] and Spring will put the results of the form into the List property in the backing bean.

Does anyone know how I can do this better?

解决方案

My problem was a lack of experience.

I ended up with this (much simplified for explanation):

My bean has the following fields: String questionType, String questionText, String answer. My list of beans is List questions.

When I generate the HTML from the list of beans, I just need to make the id/name of each form element match up with the name of the list, and the position of the bean within the list.

So, if my list of questions looks like this:

[0] {questionType="TEXT", questionText="What is your name?", answerText=null}
[1] {questionType="TEXT", questionText="What is your quest?", answerText=null}
[2] {questionType="TEXT", questionText="What is your favorite color?", answerText=null}

Then I need to generate the following HTML when I loop through the list:

<div class="question">
    <p class="questionText">What is your name?</p>
    <input type="text" id="questions[0].answerText" name="questions[0].answerText" />
</div>
<div class="question">
    <p class="questionText">What is your quest?</p>
    <input type="text" id="questions[1].answerText" name="questions[1].answerText" />
</div>
<div class="question">
    <p class="questionText">What is your favorite color?</p>
    <input type="text" id="questions[2].answerText" name="questions[2].answerText" />
</div>

When the form is submitted, Spring will find those beans and call setAnswerText(String value) on them with the form data.

I hope this helps someone else stumbling at the beginning of their Spring MVC journey.

这篇关于动态形式在春天3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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