Struts2预选复选框 [英] Struts2 preselected checkboxlist

查看:55
本文介绍了Struts2预选复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在发现的类似案例中尝试了所有解决方案,但是没有运气.

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

我的jsp.

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

positionsMap是具有键positionId和值positionName的哈希图.

positionsMap is a Hashmap with key positionId and value positionName.

selectedPositions是一个列表,其中填充了预先检查的位置.经过调试程序测试,并具有从数据库中获取的正确值. positions是具有ID和名称的列表. 因此,我的问题是如何显示预选中存储在selectedPositions中的复选框. 我得到的结果是未选中所有复选框. 如果不清楚,请问我. 预先感谢!

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!

该操作的更多信息:

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

推荐答案

看到代码,我将更改策略:使用OGNL的列表投影功能从List<Position>创建List<Long>,然后正确设置密钥:

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

这篇关于Struts2预选复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆