如何在rich:column中使用href?

时间:2017-01-27 11:08:49

标签: jsf richfaces href

我是xhtml的新手,我试图通过某个名称(在我的情况下是拍卖名称)在表格的单元格中使用href。当我按下拍卖的名称时,我希望在另一页内发送以添加出价,看到最高出价等等。它只显示了引用页面中的最后一个面板组(其中显示:此拍卖没有出价)。

有人能帮助我吗?

来自rich:column的代码是auctionList.xhtml:

<composite:implementation>
    <rich:dataTable id="auctionsTable" rows="6" value="#{cc.attrs.auctions}" var="auct" border="1" styleClass="flat list auctions" rendered="#{cc.attrs.rendered and not empty cc.attrs.auctions}">
        <f:facet name="header">
            <rich:dataScroller forComponent="auctionsTable" />
        </f:facet>

        <h:link outcome="/destination" value="link" />


        <rich:column sortBy="#{auct.name}" sortOrder="ascending" >
            <a href="detail.xhtml">
            #{auct.name}
            </a>                
        </rich:column>

和detail.xhml是:

<ui:composition>

    <h2>Details</h2>
    <h:panelGroup layout="div"
        rendered="#{auctionManager.currentAuction != null}">
        <a4j:poll interval="3000" render="highestBidOutput, bidHistory" action="#{auctionManager.refreshAuction(currentAuction)}" />
        <h3>#{currentAuction.name}</h3>
        <h:panelGrid>
        <h:column>
            <h:outputLabel value="ID:" />
            <h:outputText value="#{currentAuction.id}" />
            </h:column>

        <h:column>
            <h:outputLabel value="Owner:" />
            <h:outputText value="#{currentAuction.owner.name}" />
        </h:column>

        <h:column>
            <h:outputLabel value="Original price:" />
            <h:outputText value="#{currentAuction.originalPrice}" />
        </h:column>

        <h:column>
            <h:outputLabel value="Description:" />
            <h:outputText value="#{currentAuction.description}" />
        </h:column>

        <h:column>
            <h:outputLabel value="Location:" />
            <h:outputText value="#{currentAuction.location}" />
        </h:column>

            </h:panelGrid>
            <h:panelGrid columns="2">
            <h:outputLabel value="Highest bid:"
                rendered="#{not empty currentAuction.highestBid}" />
            <h:outputText id="highestBidOutput"
                value="#{currentAuction.highestBid.amount} (#{currentAuction.highestBid.bidder.name})"
                rendered="#{not empty currentAuction.highestBid}" />

            <h:outputLabel value="Bid" rendered="#{loginManager.logged}" />
            <h:form rendered="#{loginManager.logged}">
                <h:inputText id="bidAmountInput" value="#{bidAmount}"
                    validator="#{bidValidator.validateBid}" />
                <h:commandButton value="Add"
                    action="#{auctionManager.addBid(bidAmount)}" />
                <h:messages style="color: red" />
            </h:form>

        </h:panelGrid>

        <h:panelGroup id="bidHistory" rendered="#{not empty currentAuction.bids}">
            <h3>Bids</h3>
            <h:dataTable var="offer" value="#{currentAuction.bids}" border="1"
                styleClass="flat">
                <h:column>#{offer.bidder.name}</h:column>
                <h:column>#{offer.amount}</h:column>
            </h:dataTable>
        </h:panelGroup>

    </h:panelGroup>

    <h:panelGroup layout="div"
        rendered="#{auctionManager.currentAuction == null}">
        <p>No bids for given auction.</p>
    </h:panelGroup>

和AuctionManagerImpl.java在detail.xhtml中使用:

private static final long serialVersionUID = 1L;

private Auction currentAuction = null;

@PersistenceContext(type=PersistenceContextType.EXTENDED)
private EntityManager em;

@Inject
private LoginManager loginManagerBean;

@Resource SessionContext sessionContext;

@Produces
@Named
@Dependent
@CurrentAuction
@PermitAll
public Auction getCurrentAuction() {
    if (currentAuction != null && !em.contains(currentAuction)) {
        currentAuction = em.merge(currentAuction);
    }
    return currentAuction;
}

@PermitAll
public Long getCurrentAuctionId() {
    return (currentAuction == null) ? null : currentAuction.getId();
}

@PermitAll
public void setCurrentAuctionId(Long currentId) {
    this.currentAuction = em.find(Auction.class, currentId);
}

@PermitAll
public List<Auction> getAll() {
    return em.createQuery("SELECT a FROM Auction a", Auction.class)
            .getResultList();
}

@PermitAll
public List<Auction> getAuctionsWinningByUser(User user) {
    String jql = "SELECT auction FROM Auction auction, User user "
            + "WHERE user=:user AND auction.highestBid member of user.bids "
            + "ORDER BY auction.id";
    TypedQuery<Auction> query = em.createQuery(jql, Auction.class);
    query.setParameter("user", user);
    List<Auction> auctions = query.getResultList();
    return auctions;
}

@PermitAll
public List<Auction> getAuctionLoosingByUser(User user) {
    String jql = "SELECT DISTINCT auction FROM User user "
            + "JOIN user.bids bid JOIN bid.auction auction "
            + "WHERE user=:user AND auction.highestBid.bidder != user "
            + "ORDER BY auction.id";
    TypedQuery<Auction> query = em.createQuery(jql, Auction.class);
    query.setParameter("user", user);
    List<Auction> auctions = query.getResultList();
    return auctions;
}

@PermitAll
public void refreshAuction(Auction auction) {
    em.refresh(auction);
}

//@PermitAll
public void addBid(long bidAmount) {
    if (sessionContext.getCallerPrincipal() == null) {
        throw new IllegalStateException(
                "user must be logged in order to add bid");
    }
    if (currentAuction == null) {
        throw new IllegalStateException(
                "currentAuction have to be selected in order to add bid");
    }

    Bid bid = new Bid(loginManagerBean.getCurrentUser(), currentAuction, bidAmount);
    em.persist(bid);
    em.flush();
    em.refresh(bid);
}

}

同样非常重要的是list.xhtml,我使用auctionList.xhtml:

<ui:composition template="templates/home.xhtml">
<ui:param name="activeTab" value="list" />

<ui:define name="content">
    <h2>Auction List</h2>
    <a:auctionList auctions="#{auctionManager.all}" />
</ui:define>

1 个答案:

答案 0 :(得分:0)

Basicaly你需要在请求中放一个值并重定向到目标页面,使用JSF,一个有String的方法,隐式返回一个导航,你可以这样做:

public String sendToPageAndPutValue(){
 FacesContext.getCurrentInstance().getExternalContext().getFlash().put(paramName, paramValue);
   return "pageDestination.xhtml";
}   

要在目标Bean中恢复此值,您可以执行以下操作:

FacesContext.getCurrentInstance().getExternalContext().getFlash().get("paramName")

检查你的bean。:

@Named -> javax.inject.Named
@ViewScoped -> org.omnifaces.cdi.ViewScoped
public class YourBean

在您的xhtml中,您需要使用属性操作在h:commandButton或a4j:commandButton中调用此方法

<h:commanButton id="idButton" value="Send" execute="@this" action="#{yourBean.sendToPageAndPutValue()}"/>