Announcement Announcement Module
Collapse
No announcement yet.
Using java.util.Map as command object Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using java.util.Map as command object

    Hi,
    has anyone successfully used a Map as a command object? I don't want to have to create a custom bean for some of the forms in my app, and since the values are only simple, I would like to put them in a Map. I've tried the following in my servlet context xml file:

    <bean name="/loginUser.do" class="mp.demo.web.controller.LoginUser">
    <property name="formView" value="loginUserForm"/>
    <property name="successView" value="home"/>
    <property name="facade" ref="facade"/>
    <property name="commandName" value="loginDetails"/>
    <property name="commandClass" value="java.util.Map"/>
    </bean>

    In my Controller class (SimpleFormController), I have the following methods:

    protected Object formBackingObject(HttpServletRequest arg0) throws Exception {
    Map details = new HashMap();
    details.put("userName", "");
    details.put("password", "");
    return details;
    }

    protected void onBindAndValidate(HttpServletRequest arg0, Object arg1, BindException arg2) throws Exception {
    Map details = (Map) arg1;
    if(((String) details.get("userName")).length() < 6) {
    arg2.rejectValue("userName", "errors.userName.tooShort", new Object[] {details.get("userName")}, "Username too short");
    }
    super.onBindAndValidate(arg0, arg1, arg2);
    }

    protected ModelAndView onSubmit(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, BindException arg3) throws Exception {
    Map details = (Map) arg2;
    String userName = (String) details.get("userName");
    String password = (String) details.get("password");
    try {
    User user = facade.getUserByUserName(userName);
    if(user == null) {
    arg3.addError(ExceptionUtil.getObjectError("loginD etails", "errors.userName.notFound", new Object[] {userName}, "Username not found in database"));
    return showForm(arg0, arg1, arg3);
    }
    if(!user.getPassword().equals(password)) {
    arg3.addError(ExceptionUtil.getObjectError("loginD etails", "errors.password.incorrect", new Object[] {userName}, "Incorrect password."));
    }
    arg0.getSession(true).setAttribute("user", user);
    }
    catch(Exception e) {
    arg3.addError(ExceptionUtil.getObjectError("loginD etails", e));
    return showForm(arg0, arg1, arg3);
    }
    return new ModelAndView(getSuccessView());
    }

    In my JSP page, I'm using the following code to try to access the model data:

    <spring:bind path="loginDetails.userName">
    <input type="text" name="userName" value="<c:out value="${status.value}"/>"/>
    <c:if test="${!empty status.errorMessage}">
    <span class="errorSpan"><c:out value="${status.errorMessage}"/></span>
    </c:if>
    </spring:bind>

    But Spring does not seem to be binding the values correctly. Is there a different syntax for accessing map parameters in JSPs?

  • #2
    ahem

    i see there are plenty of answers to this question already. guess i should try those first....

    Comment

    Working...
    X