如何在rich:column里面使用href?

How to use href inside rich:column?

我是 xhtml 的新手,我正在尝试通过某个名称(在我的例子中是拍卖名称)在 table 的单元格中使用 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>

基本上你需要在请求中放入一个值并重定向到目标页面,使用 JSF,一种具有字符串的方法,隐式 return 导航,你可以通过这种方式实现:

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()}"/>