选择一个菜单,查找选定的值和动态创建



我正试图修改杜克书店的例子,以根据州税率表计算购买税。通过下面提交的示例,我已经根据应用程序中定义书籍的文件(Book.java、BookRequestBean.java、ConfigBean.java)对它们进行了分析。我搜索了很多小时,但没有看到任何与下面类似的内容,因此我认为我可能做错了。请注意,这是家庭作业,我只是希望能朝着正确的方向前进(或者表明我完全偏离了底线),去做我想要完成的事情。我的问题:

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

非常感谢任何建议。以下是我的相关信息:

选择一个菜单(已编辑并已解决)

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

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

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

相关内容

  • 没有找到相关文章

最新更新