【发布时间】:2015-06-11 23:05:59
【问题描述】:
我有一个场景,其中来自 jsp 表单的输入字段列表将映射到支持对象 Resolution 中的 HashMap 字段。
@Controller
@RequestMapping("/rms")
class ResolutionManagementController{
private static final String ISSUE_FORM_PATH="/view/resolutionForm";
private static final String SHOW_RESOLUTION_PATH="/view/showResolution";
@Autowired
IIssueManagementService issueService;
@Autowired
ResolutionManagementService resolutionService;
@RequestMapping(value="/resolution/form",method=RequestMethod.GET)
String resolutionForm(Model model){
List<Issue> issues = issueService.listIssue();
model.addAttribute("issues",issues);
model.addAttribute("resolution", new Resolution());
return ISSUE_FORM_PATH;
}
@RequestMapping(value="/resolution",method=RequestMethod.POST)
String resolve(Resolution resolution,Model model){
List<SupportExecutive> executives = resolutionService.addResolution(resolution);
model.addAttribute("executives",executives);
return SHOW_RESOLUTION_PATH;
}
}
支持表单对象
class Resolution{
private String resolutionId;
private String categoryId;
private Map<Issue,SupportExecutive> allotments;
//getters and setters
}
resolutionForm.jsp
<form:form action="/rms/resolution" commandName="resolution">
<c:forEach var="issue" items="${issues}">
<tr>
<td>
${issue.issueTitle}
</td>
<td>
<!-- What should i do so that issueTitle is converted
to Issue Object and stored as map key in allotments Map in resolution
object and the value entered by the user is converted to SupportExecutive
object and stored as corresponding value object of the allotments map-->
<input type="text" name="${issue.issueTitle}>"
</td>
</tr>
</c:forEach>
</form:form>
我试图查看属性编辑器,但我无法理解如何使用属性编辑器获取键和值并存储在地图中。或者他们可能不适合这里。
实现这一目标的最干净的方法是什么?如果能提供涉及步骤的详细说明就好了。
【问题讨论】:
-
为什么不使用更合适的对象(例如带有问题数据和分配列表的 DTO)。避免jsp“代码”总是更好
-
你能解释一下,你希望我如何在这里使用 DTO。它如何帮助简化将表单数据绑定到分配地图?
-
我明白你想说什么。我个人避免使用 DTO,因为它们不是域模型的一部分。在请求处理资源(域对象,例如:add/student、update/student、add/employee、update/employee)时,我尝试尽可能遵循 Rest 原则,但我们从不添加/dto 或 update/ dto。那是一种复杂的事情。更多的是个人选择。
标签: java spring jsp spring-mvc