我有一个场景,其中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>
我试着看看属性编辑器,但我无法理解如何使用属性编辑器获取键和值以及存储在地图中。或者他们可能不适合这里。
实现这一目标最干净的方法是什么?如果能够提供涉及步骤的详细解释,那将是一件好事。
答案 0 :(得分:0)
以下是连接域类的属性编辑器的一种方法概述:
package your.controller.package;
import org.springframework.web.bind.annotation.InitBinder;
import java.beans.PropertyEditorSupport;
...
public class YourController {
...
@InitBinder
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
binder.registerCustomEditor(Resolution.class, new ResolutionFormEditor());
}
private static class ResolutionFormEditor extends PropertyEditorSupport {
// convert a Resolution object to a string type
@Override public String getAsText() {
Resolution r = (Resolution) this.getValue();
return r != null ? r.resolutionId() : "";
}
// convert string representation to Resolution object
@Override public void setAsText(String text) {
Resolution r = resolutionService.findById(text);
this.setValue(r);
}
}
...
}
Spring property editor documentation有很好的描述。
修改强>
抱歉,我忘记了你问题的地图部分。请查看this forum post代码示例。