Announcement Announcement Module
Collapse
No announcement yet.
Problem of using list as command object Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem of using list as command object

    I have a list as command object. Each item of the list is an user object. I want to set a property of each item of the list from jsp, but i cannot do that...

    Here is the controller code >>>
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
    List list = new ArrayList();
    list = adminDao.getAgentUserList();

    return list;
    }

    The user object contains following properties >>>
    private String name;
    private String password;
    private double tierAmount;

    I want to set tierAmount from my jsp page. how can i do that.

  • #2
    I'm using the referenceData method, which is what someone suggested:
    Code:
    protected Map
    referenceData(HttpServletRequest req) {
        Map<String, Object> refData = new HashMap<String, Object>&#40;&#41;;
        refData.put&#40;"facils", facilityDao.getFacilities&#40;&#41;&#41;;
    
        return&#40;refData&#41;;
    &#125;
    Code:
    public List<Facility>
    getFacilities&#40;&#41; &#123;
        ...
    Code:
    public class
    Facility &#123;
        private String      fullname;
        private String      name;
    ...
    In my jsp:
    Code:
    <select name="facility">
    <c&#58;forEach items="$&#123;facils&#125;" var="facil">
    <c&#58;out value='$&#123;facil.name&#125;'/>, <c&#58;out value="$&#123;facil.fullname&#125;"/></br>
    <option value="<c&#58;out value='$&#123;facil.name&#125;'/>"
    <c&#58;if test="$&#123;status.value==facil.name&#125;">
    selected="true"
    </c&#58;if>
    >
    <c&#58;out value="$&#123;facil.fullname&#125;"/>
    </option>
    </c&#58;forEach>
    </select>
    Oops; that's just for getting data into the form page for the initial view; to get data back you need to use spring:bind like so:
    Code:
    <spring&#58;bind path="command.facility">
    <select name="facility">
    <c&#58;forEach items="$&#123;facils&#125;" var="facil">
    <c&#58;out value='$&#123;facil.name&#125;'/>, <c&#58;out value="$&#123;facil.fullname&#125;"/></br>
    <option value="<c&#58;out value='$&#123;facil.name&#125;'/>"
    <c&#58;if test="$&#123;status.value==facil.name&#125;">
    selected="true"
    </c&#58;if>
    >
    <c&#58;out value="$&#123;facil.fullname&#125;"/>
    </option>
    </c&#58;forEach>
    </select>
    </spring&#58;bind>
    Code:
    protected ModelAndView
    onSubmit&#40;Object cmd&#41; &#123;
        AdminSignInFrmData      sifd = &#40;AdminSignInFrmData&#41; cmd;
    
        String view = sifd.getFacility&#40;&#41;;
    And in the constructor for my SimpleFormController I have
    Code:
    setCommandClass&#40;AdminSignInFrmData.class&#41;;
    In the spring distribution is a docs directory and in it is a directory MVC-step-by-step which gives a good simple explanation of this.

    But the thing I'm using spring:bind on isn't a list so I don't think I've answered your question. :oops:

    Comment


    • #3
      Without help of bean class i cannot do that, so I just take a list in my bean class and set the bean as command object.

      Modified Code >>>
      Code:
      protected Object formBackingObject&#40;HttpServletRequest request&#41; throws Exception &#123;
             ...
      
             list = adminDao.getAgentUserList&#40;sortStr&#41;;
      
             JustBean bean = new JustBean&#40;&#41;;
             bean.setList&#40;list&#41;;
      
             return bean;
      &#125;
      Bean Object >>>
      Code:
      public class JustBean &#123;
             private List list;
             ...
      &#125;
      Jsp contains >>>
      Code:
      <c&#58;forEach var="user" items="$&#123;command.list&#125;" varStatus="varStatus">
           <spring&#58;bind path="command.list&#91;$&#123;varStatus.index&#125;&#93;.tierAmount">
             <input type="text" name="<c&#58;out value='$&#123;status.expression&#125;' />" />
           </spring&#58;bind>
      </c&#58;forEach>

      Comment

      Working...
      X