簡體   English   中英

jsf將數據傳遞到其他頁面不起作用

[英]jsf passing of data to other page not working

我想使用帶有請求范圍的相同后備bean將數據從數據表傳遞到另一個頁面,這可以在不使用數據模型的情況下進行嗎? 我正在使用Servlet 3.0

這是我的數據表頁面

    <p:dataTable var="entity" value="#{collegeController.allCollege}">
        <p:column headerText="Code">
            #{entity.collegeCode}
        </p:column>
        <p:column headerText="Description">
            #{entity.collegeDesc}
        </p:column>
        <p:column headerText="">
            <p:commandLink value="Edit" action="#{collegeController.prepareEdit(entity)}"/>
        </p:column>
    </p:dataTable>

這是我的后援豆

@ManagedBean
@RequestScoped
public class CollegeController implements Serializable {

    private String redirect = ".jsf?faces-redirect=true";
    private CollegeCatalog entity;

    public CollegeController() {
    }

    public String prepareEdit(CollegeCatalog selectedEntity) {
        Session s = NewHibernateUtil.getSessionFactory().getCurrentSession();
        s.beginTransaction();

        entity = (CollegeCatalog) s.load(CollegeCatalog.class, selectedEntity.getCollegeKey());

        return "update" + redirect;
    }

    public List getAllCollege() {
        Session s = NewHibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = s.beginTransaction();

        String query = ""
                + "FROM CollegeCatalog entity "
                + "WHERE entity.deleted = FALSE";

        Query q = s.createQuery(query);

        List l = q.list();

        tx.commit();

        return l;
    }

    /**
     * @return the entity
     */
    public CollegeCatalog getEntity() {
        if (entity == null) {
            entity = new CollegeCatalog();
        }
        return entity;
    }

    /**
     * @param entity the entity to set
     */
    public void setEntity(CollegeCatalog entity) {
        this.entity = entity;
    }
}

這是我的更新頁面(這是我要顯示所選數據的地方)

<h:form>
    <p:outputLabel value="Code:" for="txtCode"/>
    <p:inputText id="txtCode" value="#{collegeController.entity.collegeCode}"/>
    <br/>
    <p:outputLabel value="Description:" for="txtDesc"/>
    <p:inputText id="txtDesc" value="#{collegeController.entity.collegeDesc}"/>
    <br/><br/>
    <p:commandButton value="Update" action="#{collegeController.update()}"/>
    <p:commandButton value="Back" action="index.jsf?faces-redirect=true"/>
</h:form>

它總是返回null

發送到collegeController.action方法的參數將丟失,因為您擁有@RequestScoped托管Bean,並且將在每個請求上創建整個托管Bean。 同樣,根據您的實際設計,該列表將在每次collegeController.allCollege方法調用時重新創建。

解決您的問題:

  1. 將托管bean范圍更改為更大的范圍。 在這種情況下, @ViewScoped就可以了。 有關托管Bean范圍的更多信息: JSF 2中的通信-托管Bean范圍

  2. 將所有業務邏輯移到吸氣劑之外。 由於您正在使用JSF 2,並且希望在創建bean時加載列表,因此可以使用@PostConstruct方法並將列表加載到那里。 在JSF中使用托管bean時,請記住不要在getters / setter中放置任何業務邏輯 更多信息: 為什么JSF多次調用getters

采納這些建議並將其應用於您的代碼,托管bean看起來將類似於以下內容:

@ManagedBean
@ViewScoped
public class CollegeController implements Serializable {

    private String redirect = ".jsf?faces-redirect=true";
    private CollegeCatalog entity;
    private List<CollegeCatalog> collegeCatalogList;

    public CollegeController() {
    }

    @PostConstruct
    public void init() {
        Session s = NewHibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = s.beginTransaction();
        String query = ""
                + "FROM CollegeCatalog entity "
                + "WHERE entity.deleted = FALSE";
        Query q = s.createQuery(query);
        collegeCatalogList = (List<CollegeCatalog>)q.list();
        tx.commit();

        entity = new CollegeCatalog();
    }

    public String prepareEdit(CollegeCatalog selectedEntity) {
        Session s = NewHibernateUtil.getSessionFactory().getCurrentSession();
        s.beginTransaction();

        entity = (CollegeCatalog)
            s.load(CollegeCatalog.class, selectedEntity.getCollegeKey());

        return "update" + redirect;
    }

    public List<CollegeCatalog> getAllCollege() {
        return collegeCatalogList;
    }

    /**
     * @return the entity
     */
    public CollegeCatalog getEntity() {
        return entity;
    }

    /**
     * @param entity the entity to set
     */
    public void setEntity(CollegeCatalog entity) {
        this.entity = entity;
    }
}

不過,請注意,這種方法僅有助於您將<p:commandLink action="#{collegeController.prepareEdit(entity)}"/>entity參數發送到托管bean。 如果只轉發頁面而不是重定向頁面,那會更好。

如果您仍然希望使用重定向來執行此操作,則此方法將無法幫助您將數據發送到另一個頁面。 如果改用<h:button> (甚至更好的是<h:link> ),它將重定向到另一個頁面,您可以在那里處理參數,那會更好。 在這里可以對此進行更好的解釋: JSF 2中的通信-處理GET請求參數

嘗試使用f:setPropertyActionListener@ViewScoped

像這樣

<p:column headerText="">
    <p:commandLink value="Edit" action="#{collegeController.action" >
        <f:setPropertyActionListener value="#{collegeController.entity}"  target="#{collegeController.targetEntity}">
    </p:commandLink>
</p:column>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM