简体   繁体   English

Struts2预先选择了checkboxlist

[英]Struts2 preselected checkboxlist

I have tried all the sollutions in similar cases that I found, but with no luck. 我在类似的情况下尝试了所有的解决方案,但我没有运气。

My jsp. 我的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 . positionsMap是一个具有键positionId和值positionName的Hashmap。

selectedPositions is a list filled with the prechecked positions. selectedPositions是一个填充了预先检查位置的列表。 Tested with debugger and has the correct value taken from database. 使用调试器进行测试,并从数据库中获取正确的值。 positions is a list with id and name. positions是一个包含id和name的列表。 So my question is how can I show prechecked the checkboxes that are stored in selectedPositions . 所以我的问题是如何显示预先检查存储在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;
}

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: 看到代码,我改变了策略:使用OGNL的List Projection功能从List<Position>创建List<Long> List<Position> ,然后正确设置密钥:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM