gpt4 book ai didi

java - Spring MVC : Mapping Form input to complex backing object containing HashMap

转载 作者:搜寻专家 更新时间:2023-11-01 03:50:40 25 4
gpt4 key购买 nike

我有一个场景,其中来自 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>

我试图查看属性编辑器,但我无法理解如何使用属性编辑器获取键和值并将其存储在 map 中。或者他们可能不适合这里。

实现此目的最简洁的方法是什么?如果能提供详细的步骤说明就好了。

最佳答案

这是为您的域类连接属性编辑器的一种方法的概述:

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有很好的描述。

编辑:

抱歉,我忘记了您问题中的 map 部分。看看this forum post例如代码。

关于java - Spring MVC : Mapping Form input to complex backing object containing HashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29490108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com