使用将表单作为集合提交Struts1.3中的标签

发布于 2024-12-15 12:35:48 字数 2157 浏览 2 评论 0原文

使用Struts 1.3。

使用 标签将表单作为集合提交 我想格式化多个用户记录以编辑值。

当数据提交回Action时,它如何处理Action表单中的表单列表?有没有办法将结果作为表单集合提交?

例如,我有 List 并且我正在 jsp 页面上迭代这些记录,并且工作正常。但记录在 JSP 页面上是可编辑的,因此在修改记录并按下提交按钮后,我需要在操作类中包含更新记录的 List 来更新数据库中的记录。

更新,我的jsp页面如下:

<html:form action="modify.do" styleId="LogicIterateForm" method="post">
            <table style="font-weight:bold">
            <tr><td>Employee ID</td><td>Employee Name</td></tr>
            <logic:iterate id="employee" name="LogicIterateForm" property="emp" indexId="i">

           <tr>

在此处输入代码

           </tr>
        </logic:iterate>
<tr>
    <html:submit onclick="submitForm()">Modify</html:submit>
</tr>
    </table>
        </html:form>

操作:单击修改按钮即可执行

LogicIterateForm logicIterateForm=(LogicIterateForm)form;
        List<Employee> empList=logicIterateForm.getEmp();
        System.out.println("Size of emp:::::"+empList.size());
        if(empList!=null && empList.size()>0)
        {
            for(Employee emp:empList)
            {
                if(emp!=null)
                {
                    System.out.println("EmployeeID:::::::::::"+emp.getEmpId());
                    System.out.println("EmployeeName:::::::::::"+emp.getEmpName());
                }
            }
        }

,在操作内发送员工列表工作正常,但不幸的是我无法在操作内获取更新的表单字段值。请帮助我,我在哪里做错了。

下面是我的 ActionForm

public class LogicIterateForm extends org.apache.struts.action.ActionForm {


    private List<Employee> emp=new ArrayList<Employee>();


    public List<Employee> getEmp() {
        return emp;
    }

    public void setEmp(List<Employee> emp) {
        this.emp = emp;
    }

,Employee 类是一个计划 java 类,其中包含 empId 和 empName 的 setter 和 getter

Using Struts 1.3.

Submitting forms as a collection using the <logic:iterate> tag I'd like to format multiple records of user to edit values.

When the data is submitted back to the Action how can it handle a list of form in an Action form? Is there a way to submit the results as a collection of forms?

For example, I have List<EmployeeForm> and I am iterating these records on my jsp page and it's working fine. But the records are editable on the JSP page, so after modifying the records and pressing the submit button I need the List<EmployeeForm> with updated records inside my action class to update the records inside the DB.

update, my jsp page is below given:

<html:form action="modify.do" styleId="LogicIterateForm" method="post">
            <table style="font-weight:bold">
            <tr><td>Employee ID</td><td>Employee Name</td></tr>
            <logic:iterate id="employee" name="LogicIterateForm" property="emp" indexId="i">

           <tr>

enter code here

           </tr>
        </logic:iterate>
<tr>
    <html:submit onclick="submitForm()">Modify</html:submit>
</tr>
    </table>
        </html:form>

action: to perform on clicking of Modify button

LogicIterateForm logicIterateForm=(LogicIterateForm)form;
        List<Employee> empList=logicIterateForm.getEmp();
        System.out.println("Size of emp:::::"+empList.size());
        if(empList!=null && empList.size()>0)
        {
            for(Employee emp:empList)
            {
                if(emp!=null)
                {
                    System.out.println("EmployeeID:::::::::::"+emp.getEmpId());
                    System.out.println("EmployeeName:::::::::::"+emp.getEmpName());
                }
            }
        }

and it is working fine to send the list of employees inside the action but unfortunately i am unable to get the updated form fields value inside my action. please help me where i am doing mistake.

and below is my ActionForm

public class LogicIterateForm extends org.apache.struts.action.ActionForm {


    private List<Employee> emp=new ArrayList<Employee>();


    public List<Employee> getEmp() {
        return emp;
    }

    public void setEmp(List<Employee> emp) {
        this.emp = emp;
    }

and Employee class is a plan java class with setter and getter of empId and empName

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以可爱出名 2024-12-22 12:35:49

简而言之,生成的 HTML 将具有不正确的 name 属性。 ActionForm 列表属性名为 emp,但您将其称为 employee

较长的版本包括一些其他杂项,从长远来看,这些内容应该会让您的事情变得更容易一些。

首先,这是我用来显示表单的 Action。请注意,我没有明确地将任何内容纳入范围 - 框架为我们做了这件事。看起来好像您明确地将表单设置为 使用的范围,但这是多余的。

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    Employee emp1 = new Employee("1", "Dave");
    Employee emp2 = new Employee("2", "Subodh");
    ((LogicIterateForm) form).setEmp(Arrays.asList(emp1, emp2));
    return mapping.findForward("success");
}

其次,由于上述原因,JSP页面可以变得更简单。请注意,输入元素的名称必须与表单中的名称emp 匹配,否则Struts 将不知道如何处理输入值,并且将被忽略。

<logic:iterate name="empForm" property="emp" id="emp">
  <tr>
    <td><html:text name="emp" value="${emp.empId}" property="empId" indexed="true"/></td>
    <td><html:text name="emp" value="${emp.empName}" property="empName" indexed="true"/></td>
  </tr>
</logic:iterate>

然后,在提交到的操作内,所有数据都将采用预期的形式。

List<Employee> emps = ((LogicIterateForm) form).getEmp();
for (Employee emp : emps) {
    System.out.printf("%s: %s%n", emp.getEmpId(), emp.getEmpName());
}

应该可以做到这一点。

The nutshell version is that the generated HTML will have an incorrect name attribute. The ActionForm list property is named emp but you're calling it employee.

The longer version includes some other miscellaneous stuff that should make things a little easier for you in the long run.

First, here's the Action I used to display the form. Note that I am not putting anything into scope explicitly--the framework does this for us. It appears as though you're explicitly setting a form into scope for use by <logic:iterator>, but that's redundant.

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    Employee emp1 = new Employee("1", "Dave");
    Employee emp2 = new Employee("2", "Subodh");
    ((LogicIterateForm) form).setEmp(Arrays.asList(emp1, emp2));
    return mapping.findForward("success");
}

Second, the JSP page can be made simpler because of the above. Note that the name of the input element must match the name in the form, emp, otherwise Struts won't know what to do with the input value, and it will be ignored.

<logic:iterate name="empForm" property="emp" id="emp">
  <tr>
    <td><html:text name="emp" value="${emp.empId}" property="empId" indexed="true"/></td>
    <td><html:text name="emp" value="${emp.empName}" property="empName" indexed="true"/></td>
  </tr>
</logic:iterate>

Then inside the action that's being submitted to all the data will be in the form as expected.

List<Employee> emps = ((LogicIterateForm) form).getEmp();
for (Employee emp : emps) {
    System.out.printf("%s: %s%n", emp.getEmpId(), emp.getEmpName());
}

That should do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文