0

参考Oracle 文档, h:outputText 是否需要转换器?我应该使用f:setPropertyActionListener而不是f:actionListener吗?

单击 id 2564 确实显示在日志末尾:

INFO: SingletonNNTP..only once...
INFO: NNTP.loadMessages...
INFO: SingletonNNTP.connect..
INFO: SingletonNNTP.setIndex..2,562
INFO: SingletonNNTP.page..2,572
INFO: SingletonNNTP.setIndex..2,562
INFO: Initializing Mojarra 2.1.6 (SNAPSHOT 20111206) for context '/NNTPjsf'
INFO: WEB0671: Loading application [NNTPjsf] at [/NNTPjsf]
INFO: NNTPjsf was successfully deployed in 7,612 milliseconds.
INFO: Messages..
INFO: Messages..
INFO: MessageBean..
INFO: Messages.postConstruct..
INFO: SingletonNNTP..only once...
INFO: NNTP.loadMessages...
INFO: SingletonNNTP.connect..
INFO: SingletonNNTP.setIndex..2,562
INFO: SingletonNNTP.page..2,572
INFO: SingletonNNTP.setIndex..2,562
INFO: SingletonNNTP.getMessages..
INFO: Messages.getModel..
INFO: Messages.getModel..
INFO: Messages.getModel..
INFO: Messages..
INFO: Messages.postConstruct..
INFO: SingletonNNTP.getMessages..
INFO: Messages.getModel..
INFO: Messages.getModel..
INFO: Messages.getModel..
INFO: Messages.getModel..
INFO: Messages.getModel..
INFO: Messages.getModel..
INFO: Messages.getModel..
INFO: Messages.getModel..
INFO: Messages.getModel..
INFO: Messages.getModel..
INFO: MessageListener.processAction..
INFO: ..MessageListener.processAction
INFO: MessageBean..
INFO: MessageBean.postConstruct..
INFO: SingletonNNTP.setIndex..0
INFO: SingletonNNTP.getMessage..
INFO: MessageBean.setMessage..2564
INFO: MessageBean.setId..2564
INFO: MessageBean.getId..2564
INFO: MessageBean.setPrevious..2563
INFO: MessageBean.getId..2564
INFO: MessageBean.setNext..2565
INFO: MessageBean.setUrl..
INFO: MessageBean.setMessage..2556
INFO: Messages.getModel..
INFO: Messages.getModel..
INFO: Messages.getModel..

但是,它永远不会从 client.xhtml 导航到 message.xhtml。

MessageListener.processAction() 在这里应该扮演什么角色并不清楚,也不清楚它具体如何影响导航

客户端.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                template="./template.xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:c="http://java.sun.com/jsp/jstl/core"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:f="http://java.sun.com/jsf/core">


    <ui:define name="top">
        <div style="float: left">
            <h:form>
                <h:commandLink action="#{messages.back()}">
                    <f:actionListener type="net.bounceme.dur.listeners.BackListener" />
                    <h:outputText value="..back"/>
                </h:commandLink>
            </h:form>
        </div>
        <div style="float: right">
            <h:form>
                <h:commandLink action="#{messages.forward()}">
                    <f:actionListener type="net.bounceme.dur.listeners.ForwardListener" />
                    <h:outputText value="..forward"/>
                </h:commandLink>
            </h:form>
        </div>
    </ui:define>
    <ui:define name="content">
        <h:dataTable value="#{messages.model}" var="m">
            <h:column>
                <f:facet name="id">
                    <h:outputText value="id" />
                </f:facet>
                <h:form>
                    <h:commandLink id="messageLink" action="#{messageBean.setMessage(m)}">
                        <f:actionListener type="net.bounceme.dur.listeners.MessageListener" />
                        <h:outputText value="#{m.messageNumber}"/>
                    </h:commandLink>
                </h:form>
            </h:column>
            <h:column>
                <f:facet name="subject">
                    <h:outputText value="subject" />
                </f:facet>
                <h:outputText value="#{m.subject}"></h:outputText>
            </h:column>
            <h:column>
                <f:facet name="date">
                    <h:outputText value="date" />
                </f:facet>
                <h:outputText value="#{m.sentDate}"></h:outputText>
            </h:column>
        </h:dataTable>
    </ui:define>
</ui:composition>

消息.java:

package net.bounceme.dur.beans;

import java.io.Serializable;
import java.net.URL;
import java.util.List;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.enterprise.context.ConversationScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.inject.Inject;
import javax.inject.Named;
import javax.mail.Message;
import net.bounceme.dur.nntp.SingletonNNTP;

@Named
@ConversationScoped
public class Messages implements Serializable {

    private static final long serialVersionUID = 1L;
    private static final Logger LOG = Logger.getLogger(Messages.class.getName());
    private DataModel messagesDataModel = null;
    private List<Message> messages = null;
    @Inject
    private MessageBean messageBean;

    @PostConstruct
    public void postConstruct() throws Exception {
        LOG.info("Messages.postConstruct..");
        SingletonNNTP nntp = SingletonNNTP.INSTANCE;
        boolean debugNNTP = false;
        messages = nntp.getMessages(debugNNTP);
        messagesDataModel = new ListDataModel(messages);
    }

    public Messages() {
        LOG.info("Messages..");
    }

    public DataModel getModel() throws Exception {
        LOG.info("Messages.getModel..");
        return messagesDataModel;
    }

    public void forward() throws Exception {
        LOG.info("Messages.forward..");

    }

    public void back() throws Exception {
        LOG.info("Messages.back..");
    }

    public MessageBean getMessageBean() {
        return messageBean;
    }

    public void setMessageBean(MessageBean messageBean) {
        this.messageBean = messageBean;
    }

    public void insert(Message message) {
        LOG.info("Messages.insert..");
    }
}
4

1 回答 1

2

为了在 POST 请求中导航到不同的页面,action 方法需要返回您需要导航到的页面的视图 ID。像这样的东西:

<h:commandLink action="#{bean.submit}">submit</h:commandLink>
   

public String submit() {
    // ...
    return "someViewId";
}

这将去someViewId.xhtml

动作监听器对导航没有影响。如有必要,它们旨在在调用实际操作之前预处理/准备内容。查看您的代码片段,您似乎并不完全了解如何使用动作和动作侦听器。您似乎将动作侦听器与动作混淆了。您在动作侦听器方法中所做的工作实际上应该在动作方法中完成。

在您的特定情况下,例如这个

<h:form>
    <h:commandLink id="messageLink" action="#{messageBean.setMessage(m)}">
        <f:actionListener type="net.bounceme.dur.listeners.MessageListener" />
        <h:outputText value="#{m.messageNumber}"/>
    </h:commandLink>
</h:form>

需要替换为

<h:form>
    <h:commandLink id="messageLink" action="#{messageBean.setMessage(m)}">
        <h:outputText value="#{m.messageNumber}"/>
    </h:commandLink>
</h:form>

public String setMessage(Message message) {
    // Do your business job here.

    return "message";
}

这将导航到message.xhtml操作完成的时间。

这与转换无关。转换器只是在 aString和复杂的 Java 对象之间进行转换,以便复杂的 Java 对象可以在 HTML 中呈现,或者可以作为 HTTP 请求参数处理,这些参数只能是字符串。

也可以看看:

于 2012-04-17T12:44:05.657 回答