Struts2 - 动态表单域和数据库数据检索 [英] Struts2 - Dynamic form fields and database data retrieval

查看:125
本文介绍了Struts2 - 动态表单域和数据库数据检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



背景:



关于我使用Struts2开发注册Web应用程序并需要一些指导。注册表单中有一组五个表单域:1个文本框和4个下拉选择框。这五个字段描述了一个人在教育环境中的主要地位:文本字段允许用户插入他们的职位,下拉菜单允许用户选择他们所属的学校,机构,部门和部门。下拉菜单使用存储在数据库中的选项进行初始化(在注册操作中,数组列表在显示表单之前使用这些值进行初始化)。例如:

 < s:select emptyOption =truekey =school1.schoolIdlist =schoolListlistKey =schoolIdlistValue =schoolNamerequired =true/> 

问题:

我需要提供具有该能力的用户添加X个次要位置。在登记表上,用户可以点击添加另一个联盟按钮,并且显示一组新的5个表单字段。这些字段还需要验证,并在用户单击表单的提交按钮时保存。



解决此问题的最佳方法是什么?



到目前为止,我只为每个表单字段声明了数组列表,如下所示:

 私人列表< String> jobTitles = new ArrayList< String>(); 
私人列表< School> schools = new ArrayList< School>();
私人列表< Institution> Institutions = new ArrayList< Institution>();
私人清单<部门> departments = new ArrayList< Department>();
私人列表<分部> divisions = new ArrayList< Division>();

但我不知道如何继续。我如何显示主要职位的最初5个领域?如果我使用Javascript来动态插入新的表单域,我该如何初始化存储在数据库中的选项的动态下拉菜单?如果页面重新加载,我该如何保留这些值?



感谢任何帮助 - 谢谢!

解决方案

你需要解决的基本问题是如何获取请求参数的索引列表到动作类中。这很简单,我认为你通过创建 List s输入参数来开始正确的轨道。我在这里发现了一些文档。基本上你可以使用名称为 jobTitles [0] jobTitles [1] 的表单域来填充 jobTitles 列表。



然而,我认为'Affiliation'的概念值得它自己的一类:

  class UserAffiliation {
private String title;
私人字符串schoolId;
private String institutionId;
private String departmentId;
private String divisionId;

//确保Struts有一个无参数构造函数(默认或显式)来创建实例。
//添加getters和setters
}

在你的动作类中:

 私人列表< UserAffiliation>隶属关系; 
...
//从属关系的getter和setter

足够了捕获用户输入。



您的jsp可能如下所示:

 < form action =..method =post> 
< div class =affiliation>
...
< / div>

...
< / s:iterator>
< / s:if>
....
< / form>

< div id =affilationTemplatestyle =display:none;>
< div class =affiliation>
< / div>
...
< / div>

请注意div affilationTemplate 。您可以使用JS获取此模板的html,用适当的索引替换 __ IDX __ ,并在用户单击添加其他联盟按钮时附加到表单内容。这可以确保新添加的选择框预先填充了适当的值。



迭代器块显示用户已经提交的值(除了'主要联盟',它已经显示在它上面)。



注意:当然,您应该尝试摆脱重复的表单元素,如果可能的话。我会尝试将它们解压缩为包含。


I am developing a registration web app using Struts2 and need some guidance.

Background:

On the registration form, there is a set of five form fields: 1 text box, and 4 drop down select boxes. The five fields describe a person's primary position in an educational setting: the text field allows the user to insert their job title, and the drop down menus allow the user to select what school, institution, department, and division they belong to. The drop down menus are initialized with options that are stored in a database (inside the registration action, array lists are initialized with these values before the form is displayed). For example:

<s:select emptyOption="true" key="school1.schoolId" list="schoolList" listKey="schoolId" listValue="schoolName" required="true" />

Problem:

I need to provide the user with the ability add an X number of secondary positions. On the registration form, a user can click an "add another affiliation" button, and a new set of the 5 form fields are displayed. These fields will also need to be validated, and saved when the user clicks the form's submit button.

What would be the best approach to tackling this problem?

So far, I have only declared array lists for each form field, like so:

private List<String> jobTitles = new ArrayList<String>();
private List<School> schools = new ArrayList<School>();
private List<Institution> institutions = new ArrayList<Institution>();
private List<Department> departments = new ArrayList<Department>();
private List<Division> divisions = new ArrayList<Division>();

But I do not know how to proceed. How do I display the initial 5 fields for the primary position? If I use Javascript to insert new form fields dynamically, how do I initialize the dynamic drop down menus with the options stored in the database? How do I retain these values if the page is reloaded?

Any help is appreciated - thanks!

解决方案

The basic problem you need to tackle is how to get an indexed list of request parameters into your action class. This is quite simple, and I think you are on the right track by starting off by creating Lists of input parameters. I found a bit of documentation on the subject here. Basically you can have form fields with names like jobTitles[0], jobTitles[1] which would be used to populate the jobTitles List.

However, I think the concept of 'Affiliation' deserves a class of it's own:

class UserAffiliation {
   private String title;
   private String schoolId;
   private String institutionId;
   private String departmentId;
   private String divisionId;

   // Make sure that there is a no-args constructor (default or explicit) for Struts to create instances. 
   // Add getters and setters
}

In your action class:

   private List<UserAffiliation> affiliations;
   ...
   // getter and setter for affiliations

Would be enough to capture the user input.

Your jsp could look something like:

<form action=".." method="post">
  <div class="affiliation">
     <s:textfield name="affiliations[0].title"/>
     <s:select name="affiliations[0].schoolId" list="schools" listKey="schoolId" listValue="schoolName"/>
     ...
  </div>

  <s:if test="affiliations != null && affiliations.size > 1">
    <s:iterator value="affiliations" begin="1" status="status">
        <s:textfield name="affiliations[%{#status.index + 1}].title"/>
        <s:select name="affiliations[%{#status.index + 1}].schoolId" list="schools" listKey="schoolId" listValue="schoolName"/>
            ...
    </s:iterator>
  </s:if>
  ....
</form>

<div id="affilationTemplate" style="display:none;">
   <div class="affiliation">
      <s:textfield name="affiliations[__IDX__].title"/>
      <s:select name="affiliations[__IDX__].schoolId" list="schools" listKey="schoolId" listValue="schoolName"/>
   </div>
   ...
</div>

Note the div affilationTemplate. You could use JS to get the html of this template, replace __IDX__ with the appropriate index, and append to the form contents when the user clicks on the 'add another affiliation' button. This makes sure that the newly added select boxes are pre-populated with appropriate values.

The iterator block displays what ever the values the user had already submitted (with the exception of the 'primary affiliation', which is already displayed above it).

NOTE: You should of course, try to get rid of the repeated form elements if possible. I would try with extracting them into an include.

这篇关于Struts2 - 动态表单域和数据库数据检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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