0

Referencing Oracle docs, does h:outputText require a Converter here? Should I be using f:setPropertyActionListener instead of f:actionListener?

Clicking id 2564 does show at the end of the logs:

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

However, it never navigates away from client.xhtml to message.xhtml.

What role MessageListener.processAction() should have here isn't clear, nor how it impacts navigation specifically.

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

Messages.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..");
    }
}
Thufir
  • 8,216
  • 28
  • 125
  • 273

1 Answers1

2

In order to navigate to a different page on a POST request, the action method needs to return the view ID of the page where you need to navigate to. Something like this:

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

with

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

This will go to someViewId.xhtml.

The action listeners have no influence on navigation. They are intented to preprocess/prepare stuff before invoking the real action, if necessary. Looking at your code snippet, you seem to not entirely understand how to use actions and action listeners. You seem to confuse action listeners with actions. The jobs which you're doing in the action listener methods should actually be done in the action methods.

In your particular case, for example this

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

needs to be replaced by

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

with

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

    return "message";
}

This will navigate to message.xhtml when the action is finished.

This has nothing to do with conversion. Converters are just to convert between a String and a complex Java object so that complex Java objects can be presented in HTML or can be processed as HTTP request parameters which can be strings only.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • thanks, I also looked at [another question](http://stackoverflow.com/questions/10125540/how-redirect-the-url-from-hcommandlink). Kinda silly, because I knew that about the return String. if it's staying on the same page, that's a separate problem? setMessage is getting invoked but I can't track down "where" the String it returns goes. – Thufir Apr 17 '12 at 13:18
  • Then the return value is invalid. Are you inside a folder? Try setting the absolute view ID instead, e.g. `return "/somefolder/message.xhtml";`. When your JSF project stage is set to "Development" then you should have gotten a developer warning about this. – BalusC Apr 17 '12 at 13:28
  • Yeah, javax.faces.PROJECT_STAGE shows as Development in web.xml, so not sure about that. Seems like a separate problem. Thank you, though. – Thufir Apr 17 '12 at 13:39