如何在h:inputHidden中为JSF页面或其他方式设置默认值

时间:2013-10-25 11:24:40

标签: jsf-2

抱歉我的英文。如何在JSF页面中以其他方式设置h:inputHidden中的默认值?

我需要设置:

#{orderBean.instance.task} = #{taskBean.instance}

要设置OrderBean它不合适,我正在使用CRUD进行bean。我想在JSF页面中设置。

==更新==

BaseCrudBean OrderBeanTaskBean

abstract class BaseCrudBean<Id extends Serializable, Entity extends com.otv.model.entity.Entity, DAO extends CrudDAO> implements CrudBean {

    private static final long serialVersionUID = 1L;
    private static final String SUCCESS = "success";
    private static final String ERROR   = "error";

    @ManagedProperty(value="#{param.id}")
    public Long id;

    List<Entity> entityList;

    Entity instance;

    private Class<Entity> entityType;

    @SuppressWarnings ("unchecked")
    BaseCrudBean(){
        ParameterizedType genericSuperclass = (ParameterizedType) getClass()
                .getGenericSuperclass();
        entityType = (Class<Entity>) genericSuperclass.getActualTypeArguments()[1];
    }

    public List<Entity> getEntityList() {

        if(entityList == null){
            entityList=getDao().findAll();
        }

        return entityList;
    }

    public String delete(Entity task) {
        getDao().delete(task);
        return SUCCESS;
    }

    public String update() {
        getDao().update(getInstance());
        return SUCCESS;
    }

    public String save() {

        if (isManaged()) {
            getDao().update(getInstance());
        } else {
            getDao().save(getInstance());
        }

        return SUCCESS;
    }

    public boolean isManaged() {
        return getInstance().getId() != null;
    }

    public Entity getInstance() {
        if (instance == null) {
            if (id != null) {
                instance = loadInstance();
            } else {
                instance = createInstance();
            }
        }
        return instance;
    }

    public Entity loadInstance() {
        return (Entity) getDao().findOne(getId());
    }

    public Entity createInstance() {
        try {
            return entityType.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    abstract DAO getDao();

    abstract void setDao(DAO bo);
}

OrderBean

public class OrderBean extends BasePagingBean<Long, Order, OrderDAO> implements Serializable {

    private static final long serialVersionUID = 1L;
    private static final String SUCCESS = "success";
    private static final String ERROR   = "error";

    @ManagedProperty(value="#{OrderDAO}")
    OrderDAO dao;

    public OrderDAO getDao() {
        return this.dao;
    }

    public void setDao(OrderDAO dao){
        this.dao = dao;
    }
}

==更新2 ==

我需要h:selectOneMenu(我正在使用转换器):

    <h:selectOneMenu value="#{orderBean.instance.task}" class="form-control"         id="taskList" converter="#{taskConverterBean}">
        <f:selectItems value="#{taskBean.entityList}" var="task"
                       itemLabel="#{task.id}" itemValue="#{task}"/>
    </h:selectOneMenu>

但只有没有选择。

标签f:setPropertyActionListener是一个很好的解决方案,但如果我使用它:

<h:commandLink action="#{orderBean.save}" value="Add">
                <f:setPropertyActionListener value="#{taskBean.instance}" target="#{orderBean.instance.task}" />
            </h:commandLink>

我有错误:

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.demo.model.entity.Task

0 个答案:

没有答案