0

I was working with JSF, Hibernate & mySQL. Simplified concept and code like this. I have an entity class

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

    // setter and getters 
}

A backing bean class

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

i want to use "review" object to populate a dataTable and want to navigate to another page based on dataTable's a column cell's data. Expected code like this

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

Is there any way to do this??

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Ali Ashik
  • 9
  • 1

1 Answers1

0
<h:column>
<h:commandButton value="Know More" action="#{name.countryName}"/>
</h:column>

the action="#{name.countryName}" is wrong, because it is a getter methode of the entity class.

and action of a commandButton or commandLink should be a methode (usually in the controller class) which returns an outcome of Navigation Rule.

example of an action method of a 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;
}

use it then in the JSF Page like this:

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

OR MUCH BETTER use:

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

in this case, you don't need an action methode for navigation, but you send the pageId as a requestParameter tio the target View, which handles the selected Object

Rami.Q
  • 2,486
  • 2
  • 19
  • 30