Rag*_*kla 5 java spring jsp spring-mvc
我有一个场景,其中 jsp 表单中的输入字段列表将映射到支持对象解析中的 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;
}
}
Run Code Online (Sandbox Code Playgroud)
支持表单对象
class Resolution{
private String resolutionId;
private String categoryId;
private Map<Issue,SupportExecutive> allotments;
//getters and setters
}
Run Code Online (Sandbox Code Playgroud)
解决表单.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>
Run Code Online (Sandbox Code Playgroud)
我尝试查看属性编辑器,但我无法理解如何使用属性编辑器获取键和值并存储在地图中。或者可能他们不适合这里。
实现这一目标最干净的方法是什么?如果能提供详细的步骤说明就好了。
以下是为域类连接属性编辑器的一种方法的概述:
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);
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
Spring属性编辑器文档有很好的描述。
编辑:
抱歉,我忘记了您问题中的地图部分。查看此论坛帖子的示例代码。
| 归档时间: |
|
| 查看次数: |
2938 次 |
| 最近记录: |