manu
manu

Reputation: 241

Struts2 preselected checkboxlist

I have tried all the sollutions in similar cases that I found, but with no luck.

My jsp.

<s:checkboxlist list = "positionsMap"
             listKey = "%{key.toString()}"
           listValue = "%{value}"
                name = "selectedPositions"
               value = "positionName"
               label = "Position" />

positionsMap is a Hashmap with key positionId and value positionName.

selectedPositions is a list filled with the prechecked positions. Tested with debugger and has the correct value taken from database. positions is a list with id and name. So my question is how can I show prechecked the checkboxes that are stored in selectedPositions. The result I have is all checkboxes unselected. If something is not clear please ask me. Thanks in advance!

More Info from the action:

private Object1 object= new Object1();
private List<Position> positionList = new ArrayList<>();
private List<Position> selectedPositions = new ArrayList<Position>();
private String positionName = new String();
private Map<Long,String> positionsMap = new HashMap<Long, String>();

//getters, setters

@Inject
transient ObjectDAO objectDAO;
@Inject
transient PositionDAO positionDAO;

public String edit() {
    HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);    
    object= objectDAO.listById(Long.parseLong(request.getParameter("id")));
    positionList = positionDAO.listPositions();
    selectedPositions = object.getPositions();
    for (int i = 0; i < positionList.size(); i++) {
        Position row = (Position) positionList.get(i);
        Long id = row.getPositionId();
        positionName = row.getPositionName();
        positionsMap.put(id, positionName);         
    }       
    return SUCCESS;
}

Upvotes: 3

Views: 1142

Answers (1)

Andrea Ligios
Andrea Ligios

Reputation: 50281

Seen the code, I'd change strategy: use OGNL's List Projection feature to create a List<Long> from the List<Position> and then set the key correctly:

<s:checkboxlist list = "positionsMap"
                name = "selectedPositions"
               value = "selectedPositions.{positionId}"
               label = "Position" />

Upvotes: 4

Related Questions