将表单输入值作为对象列表从JSP页面传递到Servlet [英] Passing form input values as a List of objects from a JSP page to a Servlet

查看:192
本文介绍了将表单输入值作为对象列表从JSP页面传递到Servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简化(并非真实世界)的示例.假设有一个域模型-一个 Movie 类,它具有一个参与者列表.类 Actor 具有三个字段(名称出生日期 rolesNumber ).以下代码说明了这种情况:

Here is a simplified (NOT real world) example. Suppose there is a domain model - a class Movie, which has a List of actors. A class Actor has three fields (name, birthDate, rolesNumber). The following code is an illustration of this scenario:

Movie.java

public class Movie {

    // some fields

    private List<Actor> actors;

    // getters and setters
}

Actor.java

public class Actor {

    private String name;

    private Date birthDate;

    private int rolesNumber;

    // getters and setters
} 


还有一个JSP页面,我们在该页面中循环输出有关在具体电影中播放的每个演员的信息,用户可以更新相应的文本字段值并将更改提交到servlet:


There is also a JSP page where we output in a loop the information about every actor that plays in a concrete movie and the user can update the corresponding text field values and submit changes to a servlet:

actorsUpdate.jsp

...
<c:forEach items="${movie.actors}" var="actor">
    <table>
        <tr>
            <th>Name</th>
            <td><input type="text" value="${actor.name}" /></td>
        </tr>
        <tr>
            <th>Birth Date</th>
            <td><input type="text" value="${actor.birthDate}" /></td>
        </tr>
        <tr>
            <th>Number of Previous Roles</th>
            <td><input type="text" value="${actor.rolesNumber}" /></td>
        </tr>
    </table>
    <hr />
</c:forEach>
...


众所周知,为了检索servlet中的文本字段,可以使用 ServletRequest的方法,例如


It is known that in order to retrieve text fields in a servlet, one can use ServletRequest's methods like getParameter() or getParameterValues() etc. But how to retrieve the updated input fields as a List of objects (so that every three related values were grouped)?

如果这是Spring项目,我们可以使用Spring的<form:form modelAttribute="modelName">
标签,并在 modelAttribute 中定义了后备对象.但是纯JSP/Servlet项目呢?

If it were Spring project we could use Spring's <form:form modelAttribute="modelName">
tag and have a backing object defined in the modelAttribute. But how about pure JSP/Servlet project?

一种可能的解决方案是在文本输入的" name "属性中分配名称,并附加 varStatus.index ,如下所示:

One of the possible solutions is to assign names in the "name" attribute of the text inputs and append varStatus.index, like this:

<c:forEach items="${movie.actors}" var="actor" varStatus="counter">
    <table>
        <tr>
            <th>Name</th>
            <td><input type="text" name="name${counter.index}" value="${actor.name}" /></td>
        </tr>
        ...
    </table>
    <hr />
</c:forEach>

因此,该索引将使我们能够识别与一个对象相关的值.我们还可以生成一些隐藏的输入字段,在其中可以存储循环计数(许多参与者),然后在servlet中,我们可以像这样检索与一个对象相关的值:

So this index would allow us to identify values related to ONE object. And we could also generate some hidden input field where we could store a loop count (a number of actors), and then in a servlet we could retrieve the values related to one object like this:

List<Actor> actors= new ArrayList<Actor>();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
for (int i = 0; i < count; i++) {
    String name= request.getParamater("name" + i);
    Date birthDate = dateFormat.parse(request.getParamater("birthDate" + i));
    int rolesNumber = Integer.parseInt(request.getParamater("rolesNumber" + i));

    Actor actor = new Actor(name, birthDate, rolesNumber);
    actors.add(actor);
}


我的问题是:

  1. 是否存在另一种更优雅,更有效的方法,将更新的文本字段值作为对象列表?

在纯JSP/JSTL/EL/Servlet的世界中,是否有与Spring的<form:form>标记相似或等效的解决方案?

Is there any solution similar or equivalent to that of the Spring's <form:form> tag in a world of pure JSP/JSTL/EL/Servlets?


更新

好像没有人知道上述问题的答案.接受的答案并不完全是我要求的(请参阅我对它的评论).


UPDATE

Looks like nobody knows the answer to the above questions. The accepted answer is not exactly what I asked for (see my comments to it).

在纯Servlets/JSP的世界中,似乎没有与Spring的<form:form modelAttribute>标记等效的东西.

There seems to be no equivalent of Spring's <form:form modelAttribute> tag or something similar in the world of pure Servlets/JSP.

完整的 Java EE Spring框架网络应用程序框架

Well, full Java EE, or Spring Framework, Apache Struts or another powerful web application framework to the rescue!

推荐答案

您可以对所有输入使用相同的名称(revisionNumber)并使用

you can use the same name(revisionNumber) for all the inputs and use

String[] revisionNumber = request.getParamaterValues("revisionNumber")获取数组.

这篇关于将表单输入值作为对象列表从JSP页面传递到Servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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