Struts2 preselected checkboxlist

一曲冷凌霜 提交于 2021-02-19 03:48:06

问题


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

回答1:


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" />


来源:https://stackoverflow.com/questions/36480387/struts2-preselected-checkboxlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!