简体   繁体   English

jsf将数据传递到其他页面不起作用

[英]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? 我想使用带有请求范围的相同后备bean将数据从数据表传递到另一个页面,这可以在不使用数据模型的情况下进行吗? I'm using Servlet 3.0 我正在使用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 . 它总是返回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. 发送到collegeController.action方法的参数将丢失,因为您拥有@RequestScoped托管Bean,并且将在每个请求上创建整个托管Bean。 Also, with your actual design, the list will be re-created on every collegeController.allCollege method invocation. 同样,根据您的实际设计,该列表将在每次collegeController.allCollege方法调用时重新创建。

To solve your problem: 解决您的问题:

  1. Change your managed bean scope to a wider scope. 将托管bean范围更改为更大的范围。 In this case, @ViewScoped will be just fine. 在这种情况下, @ViewScoped就可以了。 More info about managed bean scopes: Communication in JSF 2 - Managed Bean Scopes 有关托管Bean范围的更多信息: JSF 2中的通信-托管Bean范围

  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. 由于您正在使用JSF 2,并且希望在创建bean时加载列表,因此可以使用@PostConstruct方法并将列表加载到那里。 When using managed bean in JSF, remember to not put any business logic in your getters/setters . 在JSF中使用托管bean时,请记住不要在getters / setter中放置任何业务逻辑 More info: Why JSF calls getters multiple times 更多信息: 为什么JSF多次调用getters

Taking these advices and applying them to your code, the managed bean will look similar to this: 采纳这些建议并将其应用于您的代码,托管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;
    }
}

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. 不过,请注意,这种方法仅有助于您将<p:commandLink action="#{collegeController.prepareEdit(entity)}"/>entity参数发送到托管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. 如果改用<h:button> (甚至更好的是<h:link> ),它将重定向到另一个页面,您可以在那里处理参数,那会更好。 This is better explained here: Communication in JSF 2 - Processing GET Request Parameters . 在这里可以对此进行更好的解释: JSF 2中的通信-处理GET请求参数

Try with f:setPropertyActionListener and @ViewScoped 尝试使用f:setPropertyActionListener@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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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