简体   繁体   中英

jsf passing of data to other page not working

I want to pass a data from datatable to another page using same backing bean with requestscope, is this possible without using datamodel? I'm using Servlet 3.0

here is my datatable page

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

and here is my backing bean

@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;
    }
}

and this is my update page (this is where I want to show the selected data)

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

It always return null .

The parameter you sent to your collegeController.action method will be lost because you have a @RequestScoped managed bean and the whole managed bean will be created on every request. Also, with your actual design, the list will be re-created on every collegeController.allCollege method invocation.

To solve your problem:

  1. Change your managed bean scope to a wider scope. In this case, @ViewScoped will be just fine. More info about managed bean scopes: Communication in JSF 2 - Managed Bean Scopes

  2. Move all your business logic outside the getter. Since you're working with JSF 2 and want that your list is loaded when creating the bean, you can use a @PostConstruct method and load the list there. When using managed bean in JSF, remember to not put any business logic in your getters/setters . More info: Why JSF calls getters multiple times

Taking these advices and applying them to your code, the managed bean will look similar to this:

@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;
    }
}

Still, note that this approach just helps you to send the entity parameter in your <p:commandLink action="#{collegeController.prepareEdit(entity)}"/> to the managed bean. It would be better if instead of redirecting the page you just forward it.

If you still prefer to do this using redirect, this approach won't help you to send the data to another page. It will be better if you use instead a <h:button> (or even better, a <h:link> ) that will redirect to your other page and you can handle the parameters there. This is better explained here: Communication in JSF 2 - Processing GET Request Parameters .

Try with f:setPropertyActionListener and @ViewScoped

Something like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM