首页 > 解决方案 > HahsMap 和 primefces 视图

问题描述

我在显示 HahMap 的键和值时遇到了很多麻烦。我的愿望是用键/值制作一个 selecteOneMenu 和 selecteItems 。这是我的代码和我的显示。我无法分别显示键和值。先感谢您

   @ManagedBean(name = "autoComplete")
@Component
@SessionScoped
public class AutoComplete extends ManageUIBean implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private final static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(AutoComplete.class);
    
    private  Map<String, Object> listChoices = new LinkedHashMap<String, Object>();
    
    private List<String> keyList;

    public Map<String, Object> choicesList() {
    

        Calendar mCalendar = Calendar.getInstance();

        Calendar mCalendar2 = Calendar.getInstance();

        mCalendar2.add(Calendar.MONTH, -6);

        Calendar mCalendar3 = Calendar.getInstance();
        
        mCalendar3.add(Calendar.MONTH, -11);

        String month = mCalendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());

        listChoices.put("Le mois en cours  ", month);
        
        listChoices.put("Les 6 derniers mois  ",
                mCalendar2.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault()));
        
        listChoices.put("Historique", mCalendar3.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault()));
        
        Map.Entry<String, Object> valuesofMonth= listChoices.entrySet().iterator().next();
        
        
        for (String key : listChoices.keySet()) {
            
         key = valuesofMonth.getKey();
            
        }
        
        
        for (Object  value : listChoices.values()) {
            
        value = valuesofMonth.getValue();
        }
        logger.info("List of choice statistique month:"+ valuesofMonth);
    

        for (Map.Entry<String, Object> entry : listChoices.entrySet()) {
            
            String choiceskey= entry.getKey();
            Object choicesvalues=  entry.getValue();
            
            logger.debug(choiceskey + ":" +choicesvalues);

            
            return listChoices;
        }
        return null;
    }


    public List<String> getKeyList() {
        
        return new ArrayList<>( choicesList().keySet());
    }

    
    public void setKeyList(List<String> keyList) {
        keyList.add( choicesList().keySet().stream().findFirst().get());
        this.keyList = keyList;
    }


    public Map<String, Object> getListChoices() {
        
        return listChoices;
    }

    public void setListChoices(Map<String, Object> listChoices) {
        this.listChoices = listChoices;
    }

    
    //--Listener---------------------------------------
    public void versionChanged(ValueChangeEvent event) {

        String newListChoices = event.getNewValue().toString();
        for (Map.Entry<String, Object> entry : listChoices.entrySet()) {

            if (entry.getValue().toString().equals(newListChoices)) {
                FacesContext.getCurrentInstance().getViewRoot().setLocale((Locale) entry.getValue());
                break;

            }
        }

    }
    
     public void submit() {
            String selectedLabel = (String) listChoices.get(keyList);
           logger.debug("selectedLabel"+  selectedLabel);
        }

     

        /**
         * @return first and last date of current Month
         */
        public Pair<Date, Date> getDateRange() {

            Date begining, end;

            {
                java.util.Calendar calendar = getCalendareForNow();
                
                
                calendar.set(java.util.Calendar.DAY_OF_MONTH, calendar.getActualMinimum(java.util.Calendar.DAY_OF_MONTH));
                setTimeToEndOfDay(calendar);
                begining = calendar.getTime();

            }

            {
                java.util.Calendar calendar = getCalendareForNow();
                calendar.set(java.util.Calendar.DAY_OF_MONTH, calendar.getActualMaximum(java.util.Calendar.DAY_OF_MONTH));
                setTimeToEndOfDay(calendar);
                end = calendar.getTime();

            }
            return Pair.of(begining, end);

        }

        /**
         * @param calendar
         */
        private static void setTimeToEndOfDay(java.util.Calendar calendar) {

            calendar.set(java.util.Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(java.util.Calendar.SECOND, 0);
            calendar.set(java.util.Calendar.MILLISECOND, 0);

        }

        /**
         * @return
         * 
         * For other month we can modify calendar last or preview  to add or retrieve the number of months
         */
        private java.util.Calendar getCalendareForNow() {
            java.util.Calendar calendar = GregorianCalendar.getInstance();

            calendar.setTime(new Date());
            
            int month= calendar.get(Calendar.MONTH);

            return calendar;
        }
 

这是一个jsf代码:

<p:outputLabel value="#{msg['practis-web.statistiques.mensuel.mois']}" /> 
                                <p:selectOneMenu     value="#{autoComplete.keyList}">
                                <f:selectItem itemLabel="#{msg['practis-web.content.creerDemande.selectionner.label']} " itemValue="" noSelectionOption="true" />
                                        <f:selectItems var="entry" value="#{autoComplete.choicesList()}"  itemLabel="#{autoComplete.choicesList().keySet()}" itemValue="#{autoComplete.choicesList().values()}" >
        
                                            
                                         </f:selectItems>
                                
                                
                                </p:selectOneMenu>

我的显示是: 带有 Hasmap entrySet 值的 jsp 视图 **在视图中显示键和值,并重复**

标签: primefacesjsf-2

解决方案


推荐阅读