【问题标题】:How to navigate another JSF page based on "h:dataTable" data? [duplicate]如何根据“h:dataTable”数据导航另一个 JSF 页面? [复制]
【发布时间】:2015-08-06 21:19:57
【问题描述】:

我正在使用 JSF、Hibernate 和 mySQL。像这样简化概念和代码。 我有一个实体类

@Entity
public class CountryReview{
    public CountryReview(){}
    @Id
    @GeneratedValue
    private int pageId;
    private String countryName;
    private String capital;

    // setter and getters 
}

支持 bean 类

@ManagedBean
@SessionScoped
public class UserBean{
    private List<CountryReview> review;
    // setters & getters for review

    privater HibernateUtil helper;
    private Session session;

    @PostConstruct
    private void populateBean(){
        session = helper.getSessionFactory().openSession();
        session.beginTransaction();
        review = session.createQuery("from CountryReview").list();
        session.getTransaction().commit();
    }
}

我想使用“review”对象来填充数据表,并希望根据数据表的列单元格数据导航到另一个页面。像这样的预期代码

.
.
.
<h:body>
    <h:form>

        <h:dataTable value="#{userBean.review}" var="name">
            <h:column>
                #{name.capital}
            </h:column>
            <h:column>
                #{name.countryName}
            </h:column>
            <h:column>
                <h:commandButton value="Know More" action="#{name.countryName}"/>
             </h:column>
        </h:dataTable>

    </h:form>
</h:body>

有没有办法做到这一点?

【问题讨论】:

    标签: jsf master-detail


    【解决方案1】:
    <h:column>
    <h:commandButton value="Know More" action="#{name.countryName}"/>
    </h:column>
    

    action="#{name.countryName}"是错误的,因为它是实体类的getter方法。

    commandButton 或 commandLink 的动作应该是一个方法(通常在控制器类中),它返回导航规则的结果。

    commandButton 的操作方法示例:

    public String goToCountry(String selectedCountryId ){
        // do what ever you want 
    
        return NaviRule-Outcome which is usually defined in navigations.xml     
        // OR something like this:
        return yourTargetOutcome+"?faces-redirect=true&pageId="+selectedCountryId;
    }
    

    然后像这样在 JSF 页面中使用它:

    <h:form>
    
            <h:dataTable value="#{userBean.review}" var="review">
                <h:column>
                    #{review.capital}
                </h:column>
                <h:column>
                    #{review.countryName}
                </h:column>
                <h:column>
                    <h:commandButton value="Know More" action="#{usearBean.goToCountry(review.pageId)}"/>
                 </h:column>
            </h:dataTable>
    
        </h:form>
    

    或者更好使用:

    <h:outputLink value="countryReview.xhtml?pageId=#{review.pageId}"/>
    

    在这种情况下,您不需要用于导航的操作方法,而是将 pageId 作为 requestParameter 发送到目标视图,该视图处理选定的对象

    【讨论】:

      猜你喜欢
      • 2015-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-21
      • 1970-01-01
      • 2021-09-20
      相关资源
      最近更新 更多