Problem with a UrlAction and a managed bean…

Splash Forums PrettyFaces Users Problem with a UrlAction and a managed bean…

This topic contains 9 replies, has 3 voices, and was last updated by  balteo 6 years, 2 months ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #17899

    balteo
    Participant

    Hello,

    I have a problem with my app. Here it is:

    From a “sculpture.xhmtl” page I call a “GestionnaireInscriptionView” managed bean as follows:

    Code:
    <a4j:commandButton id=”soumettreInscription” action=”#{gestionnaireInscriptionView.sauvegarderAbonne}” value=”#{msg[‘creer.alerte’]}” render=”messageEmailP,abonneEmailP” type=”submit”/>

    Here is the GestionnaireInscriptionView managed bean:

    Code:
    package com.jeanbaptistemartin.view;

    import com.jeanbaptistemartin.service.AbonneExistsException;
    import com.jeanbaptistemartin.service.JbmService;
    import com.jeanbaptistemartin.util.FacesUtils;
    import com.ocpsoft.pretty.faces.annotation.URLMapping;
    import java.util.ResourceBundle;
    import javax.faces.application.FacesMessage;
    import javax.faces.component.html.HtmlInputText;
    import javax.faces.context.FacesContext;
    import javax.faces.event.ActionEvent;
    import org.apache.log4j.Logger;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Component;

    /**
    *
    * @author julien
    */
    @Component(“gestionnaireInscriptionView”)//todo rename
    @Scope(“request”)
    @URLMapping(id = “alertesAction”, parentId = “rootAction”, pattern = “alertes”, viewId = “/alertes.jsf”)
    public class GestionnaireInscriptionView {

    private static transient Logger log = Logger.getLogger(“com.jeanbaptistemartin.view”);
    private JbmService service;
    private String abonneEmail = “”;
    private HtmlInputText abonneEmailControl;

    public GestionnaireInscriptionView() {
    log.info(“GestionnaireInscriptionView()”);
    }

    @Autowired
    public GestionnaireInscriptionView(JbmService service) {
    log.info(“GestionnaireInscriptionView(JbmService service)”);
    this.service = service;
    }

    public String sauvegarderAbonne() {
    log.info(“sauvegarderAbonne”);
    FacesContext fc = FacesContext.getCurrentInstance();
    FacesUtils fu = new FacesUtils();
    ResourceBundle rb = ResourceBundle.getBundle(“resources”, fc.getViewRoot().getLocale());
    try {
    this.service.sauvegarderAbonne(this.abonneEmail, fc.getViewRoot().getLocale().toString());
    FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_INFO, rb.getString(“confirmation.inscription”), null);
    fc.addMessage(fu.getClientId(“email”), fm);
    this.abonneEmail = “”;
    return “succes”;
    } catch (AbonneExistsException aee) {
    FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_ERROR, rb.getString(“doublon.email”), null);
    fc.addMessage(fu.getClientId(“email”), fm);
    return “echec”;
    } catch (Exception e) {
    FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_ERROR, rb.getString(“erreur.technique”), null);
    fc.addMessage(fu.getClientId(“email”), fm);
    log.error(rb.getString(“erreur.technique”), e);
    return “echec”;
    }
    }

    public void reinitialiserFormulaire(ActionEvent e) {
    log.debug(“reinitialiserFormulaire(ActionEvent e)”);
    reinitialiserFormulaire();
    }

    private void reinitialiserFormulaire() {
    log.debug(“reinitialiserFormulaire”);
    if (this.abonneEmailControl != null) {
    this.abonneEmailControl.setLocalValueSet(false);
    this.abonneEmailControl.setSubmittedValue(null);
    }
    }

    public HtmlInputText getAbonneEmailControl() {
    return abonneEmailControl;
    }

    public void setAbonneEmailControl(HtmlInputText abonneEmailControl) {
    this.abonneEmailControl = abonneEmailControl;
    }

    public String getAbonneEmail() {
    return abonneEmail;
    }

    public void setAbonneEmail(String abonneEmail) {
    this.abonneEmail = abonneEmail;
    }
    }

    Possibly, the problem is that I call this method from the sculpture page and that a URLaction is declared for this page as follows (managed bean for sculpture follows):

    Code:
    package com.jeanbaptistemartin.view;

    import com.jeanbaptistemartin.domain.Sculpture;
    import com.jeanbaptistemartin.service.JbmService;
    import com.ocpsoft.pretty.faces.annotation.URLAction;
    import com.ocpsoft.pretty.faces.annotation.URLMapping;
    import javax.faces.event.ActionEvent;
    import org.apache.log4j.Logger;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Component;

    /**
    *
    * @author Julien Martin
    */
    @Component(“sculptureView”)
    @Scope(“request”)
    @URLMapping(id = “sculptureAction”, parentId = “rootAction”, pattern = “galerie/sculpture/#{sculptureView.titre}-#{sculptureView.id}”, viewId = “/sculpture.jsf”)
    public class SculptureView {

    private static transient Logger log = Logger.getLogger(“com.jeanbaptistemartin.view”);
    private JbmService service;
    private Sculpture sculpture;
    private SuggestionOeuvreView suggestionOeuvreView;
    private String titre;
    private int id;

    @Autowired
    public SculptureView(JbmService service) {
    log.info(“SculptureView(JbmService service)”);
    this.service = service;
    }

    public SuggestionOeuvreView getSuggestionOeuvreView() {
    log.debug(“getSuggestionOeuvreView”);
    return suggestionOeuvreView;
    }

    @Autowired
    @Scope
    public void setSuggestionOeuvreView(SuggestionOeuvreView suggestionOeuvreView) {
    log.warn(“setSuggestionOeuvreView”);
    this.suggestionOeuvreView = suggestionOeuvreView;
    }

    @URLAction//todo pose problème avec formulaire inscription qui se trouve sur page sculpture
    public String fetchSculpture() {
    log.debug(“fetchSculpture”);
    this.sculpture = service.findByID(this.id);
    //todo: gestion du cas SculptureNonTrouveeException: redirect to galerie??
    suggestionOeuvreView.enleverSculptureDeListeNonVisitees(this.id);
    suggestionOeuvreView.setSculptureSuggeree(suggestionOeuvreView.suggererSculpture());//refactor
    suggestionOeuvreView.setSculpturesSuggerees(suggestionOeuvreView.suggererSculptures());//refactor
    return “/sculpture.xhtml”;
    }

    public Sculpture getSculpture() {
    log.debug(“getSculpture”);
    return sculpture;
    }

    public void rechargeSculpturesSuggerees(ActionEvent evt) {
    log.debug(“rechargeSculpturesSuggerees”);
    suggestionOeuvreView.setSculpturesNonVisitees(suggestionOeuvreView.recupererSculpturesNonVisitees());
    suggestionOeuvreView.setSculpturesSuggerees(suggestionOeuvreView.suggererSculptures());
    }

    /**
    * @return the titre
    */
    public String getTitre() {
    return titre;
    }

    /**
    * @param titre the titre to set
    */
    public void setTitre(String titre) {
    this.titre = titre;
    }

    /**
    * @return the id
    */
    public int getId() {
    return id;
    }

    /**
    * @param id the id to set
    */
    public void setId(int id) {
    this.id = id;
    }
    }

    The problem I have is that the “sauvegarderAbonne” method is not called by the above a4j command button. I suspect it is due to one of the Urlactions.

    Can anyone please help?

    Thanks in advance,

    Julien.

    #20824

    If you remove the URLActions, is the method called?

    #20825

    balteo
    Participant

    Hello Lincoln,

    I commented out the @URLMapping in gestionnaireinscriptionview but was not able to get the application to work after commenting out the @URLAction in sculptureView (given the way it is designed this has to be in place for this part of the app to work)

    I forgot to mention that I have a pretty configurator as follows:

    *********************************

    package com.jeanbaptistemartin.util;

    import com.ocpsoft.pretty.faces.config.PrettyConfig;

    import com.ocpsoft.pretty.faces.config.mapping.UrlAction;

    import com.ocpsoft.pretty.faces.config.mapping.UrlMapping;

    import com.ocpsoft.pretty.faces.spi.ConfigurationPostProcessor;

    import javax.servlet.ServletContext;

    import org.apache.log4j.Logger;

    /**

    *

    * @author julien

    */

    public class PrettyFacesPostProcessor implements ConfigurationPostProcessor {

    private static transient Logger log = Logger.getLogger(“com.jeanbaptistemartin.util”);

    @Override

    public PrettyConfig processConfiguration(ServletContext sc, PrettyConfig pc) {

    log.debug(“processConfiguration”);

    UrlAction rootAction = new UrlAction(“#{accueilView.changeLocale}”);

    UrlMapping biographieMapping = (UrlMapping) pc.getMappingById(“biographieAction”);

    biographieMapping.addAction(rootAction);

    UrlMapping commandesPersonnaliseesMapping = (UrlMapping) pc.getMappingById(“commandesPersonnaliseesAction”);

    commandesPersonnaliseesMapping.addAction(rootAction);

    UrlMapping contactMapping = (UrlMapping) pc.getMappingById(“contactAction”);

    contactMapping.getActions().add(0, rootAction);

    UrlMapping galerieMapping = (UrlMapping) pc.getMappingById(“galerieAction”);

    galerieMapping.getActions().add(0, rootAction);

    UrlMapping liensMapping = (UrlMapping) pc.getMappingById(“liensAction”);

    liensMapping.getActions().add(0, rootAction);

    UrlMapping sculptureMapping = (UrlMapping) pc.getMappingById(“sculptureAction”);

    sculptureMapping.getActions().add(0, rootAction);

    UrlMapping alertesMapping = (UrlMapping) pc.getMappingById(“alertesAction”);

    alertesMapping.getActions().add(0, rootAction);

    return pc;

    }

    }

    *********************************

    Any other suggestion?

    J.

    #20826

    I don’t think that this has something to do with PrettyFaces. The “action is not called” problems usually are caused by other things like:

    • Is the button placed inside a component with a rendered attribute that evaluates to false during RESTORE_VIEW?
    • Is the button placed inside an iterating component (like h:dataTable) that is bound to a list that is empty during the postback? This may happen if the list of rendered items is placed in a request scoped bean.
    #20827

    balteo
    Participant

    Hello Christian,

    Thanks for your reply. I’ve commented out the URLAction as follows:

    ****************

    //@URLAction

    public String fetchSculpture() {

    ****************

    I’ve modified the xhtml page so that it can be displayed without using the fetchSculpture() method and the action method from GestionnaireInscriptionView is now indeed called.

    Do you see my point?

    Julien.

    #20828

    Is the commandButton placed inside a data table?

    You could also try to use @URLAction(onPostback=false). This way the method won’t be executed on postbacks. Executing page actions on postbacks may sometimes lead to problems with action components placed in data tables.

    #20829

    balteo
    Participant

    No the commandButton is not placed inside a data table.

    I’ll try the onPostback=false and keep you posted.

    J.

    #20830

    balteo
    Participant

    Hi Christian!

    onPostback=false works well on one of the problematic pages. However I have another commandbutton placed inside a rich:datagrid which does not work. Is it somehow related to what you said in your first thread for this post?? If so what is the workaround?

    J.

    #20831

    Yeah, I think it has something to do with the list in your bean getting changed (or getting empty) between the page rendering and the postback! How does this list get initialized? Make sure the list keeps its state! You could use view scope or something like a4j:keepAlive for that!

    #20832

    balteo
    Participant

    Thanks a lot Christian!

    I did not know about this new jsf2 scope.

    J.

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.

Comments are closed.