selectOne菜单,查找选定的值和动态创建

时间:2015-11-29 03:06:21

标签: java jsf netbeans glassfish selectonemenu

我正在尝试修改Duke的书店示例,以根据州税率表计算购买税。通过下面提交的示例,我将它们基于在应用程序中定义书籍的文件(Book.java,BookRequestBean.java,ConfigBean.java)。我已经搜索了很多个小时但是没有看到类似于下面的内容,因此我认为我可能做错了。请注意,这是家庭作业,我只是希望在正确的方向上轻推(或表明我完全偏离基础)去做我想要完成的事情。我的问题:

  • 如何从selectOneMenu获取所选状态并从StateTaxConfigBean中的项目中查找正确的税率?
  • 如何通过从StateTaxConfigBean中的相同项目填充来创建更好的selectOneMenu?解决了这个
  • itemLabels正在被翻译,如果我从50个selectItems更加动态地填充,是否可以拥有相同的功能,如果是这样,我将如何进行?不重要但是要做什么

任何建议都非常感谢。以下是我所涉及的内容:

selectOne菜单(已编辑并已解决)

<h:outputLabel for="state" value="#{bundle.State}"/>
                <h:selectOneMenu id="state" 
                                 value="#{cashierBean.selectedItem}"
                                 required="true"
                                 requiredMessage="#{bundle.ReqState}">                    
                    <f:selectItem itemValue="#{null}" itemLabel="#{bundle.stateSelect}"/>
                    <f:selectItems value="#{cashierBean.states}" 
                                   var="state" 
                                   itemLabel="#{state.statename}" 
                                   itemValue="#{state.stateId}"/>
                </h:selectOneMenu>
                <h:message styleClass="error-message" for="state"/>  

CashierBean.java添加了此

public List<State> getStates() {
        try {
            return stateTaxRequestBean.getStates();
        } catch (StatesNotFoundException e) {
            throw new FacesException("Exception: " + e);
        }
    }

State.java

@Entity
@Table(name = "WEB_BOOKSTORE_STATES")
@NamedQuery(
        name = "findStates",
        query = "SELECT s FROM State s ORDER BY s.stateId")

public class State implements Serializable {
    private static final long serialVersionUID = -4146681491856848089L;
    @Id    
    @NotNull
    private String stateId;    
    @NotNull
    private String statename;    
    @NotNull
    private Double tax;    
    public State() {        
    }

    public State(String stateId, String statename, Double tax) {
        this.stateId = stateId;
        this.statename = statename;
        this.tax = tax;
    }


    // Getters and Setters
    ... 

    @Override
    public String toString() {
        return "bookstore.entities.State[ stateId=" + stateId + " ]";
    }    

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (stateId != null ? stateId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof State)) {
            return false;
        }
        State other = (State) object;

        return this.stateId != null || other.stateId == null 
                && this.stateId == null || this.stateId.equals(other.stateId);
    }
}

StateTaxRequestBean.java

@Stateful
public class StateTaxRequestBean {      
    @PersistenceContext
    private EntityManager em;
    private static final Logger logger =
            Logger.getLogger("dukesbookstore.ejb.StateTaxRequestBean");

    public StateTaxRequestBean() throws Exception {
    }

    public void createState(String stateId, String statename, Double tax) {
        try {
            State state = new State(stateId, statename, tax);

            logger.log(Level.INFO, "Created state {0}", stateId);
            em.persist(state);
            logger.log(Level.INFO, "Persisted state {0}", stateId);
        } catch (Exception ex) {
            throw new EJBException(ex.getMessage());
        }
    }

    public List<State> getStates() throws StatesNotFoundException {
        try {
            return (List<State>) em.createNamedQuery("findStates").getResultList();
        } catch (Exception ex) {
            throw new StatesNotFoundException(
                    "Could not get states: " + ex.getMessage());
        }        
    }   

    public State getState(String stateId) throws StateNotFoundException {
        State requestedState = em.find(State.class, stateId);

        if (requestedState == null) {
            throw new StateNotFoundException("Couldn't find state: " + stateId);
        }

        return requestedState;
    }    
}

StateTaxConfigBean.java

@Singleton
@Startup
public class StateTaxConfigBean {

    @EJB
    private StateTaxRequestBean request;

    @PostConstruct
    public void createData() {
        request.createState("AL", "Alabama", 0.04);
        request.createState("AK", "Alaska", 0.0);
        request.createState("AZ", "Arizona", 0.056);
        etc...
    }
}

2 个答案:

答案 0 :(得分:0)

你可以使用ajax。 select事件将是侦听器的触发器。

答案 1 :(得分:0)

你可以使用ajax。 select事件将是侦听器的触发器。