简体   繁体   中英

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

I have scenario where a list of input fields from the jsp form would map to a HashMap field within the backing object Resolution.

    @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;
        }   

    }

Backing Form Object

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>

I tried to take a look at property editors but i am unable to understand how would i obtain the key and value and store in the map using property editor. Or may be they are not suited here.

What is the most cleanest way of achieving this ? It would be good if a detail explanation involving steps can be provided.

Here is an outline of one way to hook up a property editor for your domain class:

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 has a good description.

Edit:

Sorry, I forgot about the Map part of your question. Take a look at this forum post for example code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM