Spring Portlet MVC中的一系列重复形式 [英] Series of repeated forms in Spring Portlet MVC

查看:113
本文介绍了Spring Portlet MVC中的一系列重复形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当简单的用例.演员是经理,负责向员工列表提供反馈.

I have a rather simple use case. The actor is a manager who provides feedback to a list of employees.

该视图显示员工列表.经理(用户)可以单击每个员工,然后打开(内联)反馈表单以捕获反馈.

The view displays a list of employees. The manager (user) can click on each employee upon which a feedback form opens up (inline) to capture the feedback.

我有一个List<Employee>,用于构造页面上的员工列表.我无法理解如何组织和获取反馈.我打算有一个单独的bean,Feedback,它对应于特定Employee的反馈.

I have a List<Employee> which I am using to construct the listing of employees on the page. I am not able to understand how to structure and capture the feedback. I intend to have a separate bean, Feedback which correspond to the feedback of a particular Employee.

我从循环构建<form:form>开始,并且做到了:

I started by building <form:form> in a loop and did this:

<c:forEach var="employee" items="${employees}" varStatus="stat">
    <form:form action="${saveURL}" method="post" modelAttribute="feedback-${stat.index + 1}">
        <input type="submit" value="Submit Feedback"/>
    </form:form>
</c:forEach>

我正在尝试保留processAction方法的签名,如下所示:

I am trying to keep the signature of my processAction method as follows:

@RequestMapping(params = "action=save")
public void saveFeedback(ActionRequest request, ActionResponse response, @ModelAttribute("feedback") Feedback feedback, Model model)

不幸的是,我无法继续进行此操作,因为我感觉这里缺少一些重要的设计作品.

Unfortunately, I am not able to proceed with this as I feel I am missing some important design piece here.

我应该如何构造我的<form:form>saveFeedback方法,以实现我想做的事情?

How should I be structuring my <form:form> or saveFeedback method in order to achieve what I am trying to do?

推荐答案

这就是我解决问题的方式.我不必创建用于保存每次迭代特定数据的反馈表单的列表,而必须创建一个包含Feedback域对象列表的单个反馈表单.

This is how I solved the problem. Instead of having a list of feedback forms for holding data specific to each iteration, I had to create a single feedback form with a list of Feedback domain objects.

我的FeedbackForm现在看起来像这样:

My FeedbackForm looks like this now:

public class FeedbackForm {
    private List<Feedback> feedbacks;

    ...

    getters and setters
}

通常,Feedback类如下所示:

public class Feedback {
    private String feedback;
    private int rating;

    ...

    getters and setters
}

现在我的JSP如下:

<portlet:actionURL var="saveURL">
    <portlet:param name="action" value="save" />
    <portlet:param name="index" value="${stat.index}" />
</portlet:actionURL>

<c:forEach var="employee" items="${employees}" varStatus="stat">
    <form:form action="${saveURL}" method="post" modelAttribute="feedbackForm">
        <form:input path="feedbacks[${stat.index}].feedback" />
        <form:input path="feedbacks[${stat.index}].rating" />
        <input type="submit" value="Submit Feedback"/>
    </form:form>
</c:forEach>

最后,我的控制器方法如下:

And lastly, my controller method looks like:

@RequestMapping(params = "action=save")
public void saveFeedback(ActionRequest request, ActionResponse response,
    @RequestParam("index") int index,
    @ModelAttribute("feedbackForm") FeedbackForm feedbackForm, Model model)
    throws PortletException {

    Feedback feedback = feedbackForm.getFeedbacks().get(index);

    logger.debug("Submitted feedback is {}", feedback);
}

这个想法是不可能同时提交多个表单,尽管它们可能同时出现在页面上.基本上,每个表单只能单击一个提交按钮.

The idea is that it is impossible to have multiple forms being submitted at the same time, although they may appear on the page at the same time. Basically, one can only click one submit button per form.

希望这对处于类似情况的人有所帮助.

Hope this helps someone in a similar situation.

这篇关于Spring Portlet MVC中的一系列重复形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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