Results 1 to 1 of 1
  1. #1
    soul_killer is offline Member
    Join Date
    Feb 2012
    Rep Power

    Default How to use Java Map between controller and DTO?

    i want to ask about java Map. Basically i'm trying to do a server side validation.
    i have this portion inside my method here:

    Java Code:
    	@RequestMapping(value = "addCountry", method = RequestMethod.POST)
    	public String addStep(@ModelAttribute("countryDTO")
    	CountryDTO countryDTO, BindingResult result, Model model,
    		HttpServletRequest request)
    validator.validate(countryDTO, result);
    			if (result.hasErrors())
    ResourceBundle rb = ResourceBundle.getBundle("messages");
    Map<String, String> ERRORS = new HashMap<String, String>();
    for (ObjectError errorobj : result.getAllErrors())
    String code = errorobj.getCode();
    String fieldName = code.substring(code.indexOf('.') + 1, code.indexOf('['));
    String message = rb.getString(code);
    model.addAttribute(fieldName + "error", message);
    } } }
    My CountryDTO:
    Java Code:
    public class CountryDTO extends CoreDTO implements Serializable 
    	private static final long serialVersionUID = 1L;
    	@NotBlank(message="code is blank")
    	@Length(max=4, message="code max length is 4")
    	private String code;
    	@NotBlank(message="description is blank")
    	@Length(max=4, message="description max length is 4")
    	private String description;
    	public String getCode()
    		return code;
    	public void setCode(String code)
    		this.code = code;
    	public String getDescription()
    		return description;
    	public void setDescription(String description)
    		this.description = description;
    And i have a CoreDTO that has these:

    Java Code:
    public class CoreDTO implements Serializable {
    private String errMsg;
    private Map<String, String> formErrors;
    	public String getErrMsg()
    		return errMsg;
    	public void setErrMsg(String errMsg)
    		this.errMsg = errMsg;
    	public Map<String, String> getFormErrors()
    		return formErrors;
    	public void setFormErrors(Map<String, String> formErrors)
    		this.formErrors = formErrors;
    My form:
    Java Code:
    <td >
    Code: <input name="code" id="code" type="text"
    title="Country"><span><c:out value="${codeerror}" /></span></td>
    <td>Description: <input name="description"
    id="description" type="text"
    value="${descriptionerror}" /></span></td>
    My question is:
    1) how can i map the formErrors to the errorObjects? So, at the end how will i pass the error messages appropriately to my jsp using the mapped formErrors instead of simply passing it from message? I need all my errors collected to the CoreDTO, and countryDTO will getErrMsg from the CoreDTO.

    additional info: the CoreDTO is extended to CountryDTO
    Last edited by soul_killer; 02-23-2012 at 05:14 AM.

Similar Threads

  1. Need help for controller
    By powerpravin in forum New To Java
    Replies: 0
    Last Post: 04-12-2011, 06:07 AM
  2. Model View Controller with Java
    By chopram in forum New To Java
    Replies: 8
    Last Post: 06-07-2008, 09:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts