1 2 Previous Next 20 Replies Latest reply on Apr 30, 2009 5:45 AM by edre.py

    getPreferences with JSF portlet

    franco12

      Hi,

      I'm trying to set a view and edit modes through two instances.

      One instance has a parameter edit="true" et the other has the parameter edit="false"

      I'd like to use those parameters to display different things using JSF

      but i'm unable to get Preferences, here is my code to get these preferences :

      protected void doView(RenderRequest request, RenderResponse response) throws PortletException, PortletSecurityException, IOException
       {
       response.setContentType("text/html");
       PortletPreferences prefs = request.getPreferences();
       String parameter = prefs.getValue("edit",init);
       if (parameter.equals("true"))
       {
      
       }
       else
       {
      
       }
       }


        • 1. Re: getPreferences with JSF portlet
          franco12

          I forgot to say

          thanks ^^
          regards

          • 2. Re: getPreferences with JSF portlet
            franco12

            The problem is that the program won't pass through 'doView'




            MDP_contribution_consultation.java

            import javax.portlet.PortletPreferences;
            import javax.portlet.PortletSecurityException;
            import javax.portlet.RenderRequest;
            import javax.portlet.RenderResponse;
            import javax.portlet.PortletException;
            import java.io.IOException;
            
            public class MDP_contribution_consultation
            {
             String consulter="ok";
             String supprimer;
             private String init="false";
            
             protected void doView(RenderRequest request, RenderResponse response) throws PortletException, PortletSecurityException, IOException
             {
             response.setContentType("text/html");
             // StringBuffer html = new StringBuffer();
             // PrintWriter writer = response.getWriter();
             PortletPreferences prefs = request.getPreferences();
             String parameter = prefs.getValue("suppression_pref",init);
             if (parameter.equals("true"))
             {
             this.supprimer="ok";
             // html.append("<-- Contribution --><br/>");
             // html.append("<img src='/WEB-INF/images/logo.gif'/>");
             }
             else
             {
             this.supprimer="pas ok";
             // html.append("<-- Consultation --><br/>");
             }
             // response.getWriter().write(html.toString());
             // writer.close();
             }
            
             public String getConsulter()
             {
             return consulter;
             }
            
             public void setConsulter(String consulter)
             {
             this.consulter = consulter;
             }
            
             public String getSupprimer()
             {
             return supprimer;
             }
            
             public void setSupprimer(String supprimer)
             {
             this.supprimer = supprimer;
             }
            }


            view.jsp

            <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
            <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
            
            <f:view>
             <div align="center">
             <h:panelGrid columns="3">
             <h:outputText value="NOM FICHE"/>
             <h:outputText value="CONSULTER"/>
             <h:outputText value="SUPPRIMER"/>
             <h:outputText value="fiche 1"/>
             <h:outputText value="#{MDP_contribution_consultation.consulter}"/>
             <h:outputText value="#{MDP_contribution_consultation.supprimer}"/>
             <h:outputText value="fiche 2"/>
             <h:outputText value="#{MDP_contribution_consultation.consulter}"/>
             <h:outputText value="#{MDP_contribution_consultation.supprimer}"/>
             <h:outputText value="fiche 3"/>
             <h:outputText value="#{MDP_contribution_consultation.consulter}"/>
             <h:outputText value="#{MDP_contribution_consultation.supprimer}"/>
             </h:panelGrid>
             <br/>
             </div>
            </f:view>


            sorry for the multiple posts
            regards

            • 3. Re: getPreferences with JSF portlet
              theute

              It doesn't look like a portlet to me, you are not extending GenericPortlet or anything else.

              • 4. Re: getPreferences with JSF portlet
                franco12

                 

                "thomas.heute@jboss.com" wrote:
                It doesn't look like a portlet to me, you are not extending GenericPortlet or anything else.



                sorry I'm a noob,
                what have I to do except adding "extends GenericPortlet" ?

                Because now, I've having this error
                Object not found mdpApp.MDP_contribution_consultation


                thanks

                • 5. Re: getPreferences with JSF portlet
                  theute

                  It looks like you didn't specify your XML descriptors correctly.

                  You should start from the HelloWorld portlet and extend it step by step:
                  http://labs.jboss.com/portal/portletswap/portlets_tutorial.html

                  • 6. Re: getPreferences with JSF portlet
                    franco12

                     

                    "thomas.heute@jboss.com" wrote:
                    It looks like you didn't specify your XML descriptors correctly.

                    You should start from the HelloWorld portlet and extend it step by step:
                    http://labs.jboss.com/portal/portletswap/portlets_tutorial.html


                    hu sorry,
                    I had to refresh anoter time
                    it's working without error

                    but the program won't pass through doView
                    i'd like to set supprimer="ok" if my parameter suppression_pref="true"
                    or supprimer="pas ok" if suppression_pref="false"

                    and then I display supprimer in my JSP
                    <h:outputText value="#{MDP_contribution_consultation.supprimer}"/>


                    thanks for your quick replies

                    • 7. Re: getPreferences with JSF portlet
                      theute

                      Oups sorry i replied too quickly i missed that you were working on a JSF portlet.

                      On a JSF portlet you don't need to extend GenericPortlet and render() won't be called.

                      You need to pass a JSF page as parameter in portlet.xml in getSupprimer() you should access the preferences like this:

                      public String getSupprimer() {
                       PortletPreferences prefs = request.getPreferences();
                       String parameter = prefs.getValue("suppression_pref",init);
                       if (parameter.equals("true))
                       {
                       return "ok";
                       }
                       else
                       {
                       return "pas ok";
                       }
                      }
                      


                      • 8. Re: getPreferences with JSF portlet
                        franco12

                        ok thanks for your help Thomas :)

                        I removed "extends GenericPortlet" and the doView method
                        and I updated getSupprimer() like this

                        import javax.portlet.PortletPreferences;
                        import javax.portlet.PortletSecurityException;
                        import javax.portlet.RenderRequest;
                        import javax.portlet.RenderResponse;
                        import javax.portlet.PortletException;
                        import java.io.IOException;
                        
                        public class MDP_contribution_consultation
                        {
                         String consulter="ok";
                         String supprimer;
                         private String init="false";
                        
                         public String getConsulter()
                         {
                         return consulter;
                         }
                        
                         public void setConsulter(String consulter)
                         {
                         this.consulter = consulter;
                         }
                        
                         public String getSupprimer(RenderRequest request, RenderResponse response) throws PortletException, PortletSecurityException, IOException
                         {
                         PortletPreferences prefs = request.getPreferences();
                         String parameter = prefs.getValue("edit",init);
                         if (parameter.equals("true"))
                         {
                         return "ok";
                         }
                         else
                         {
                         return "pas ok";
                         }
                         }
                        
                         public void setSupprimer(String supprimer)
                         {
                         this.supprimer = supprimer;
                         }
                        }
                        


                        now I'm having this error

                        An error occured while rendering window 'default.MaPage.contrib'
                        
                        org.apache.jasper.JasperException: Bean: MDP_contribution_consultation, property: supprimer


                        my portlet.xml has a jsp page as an init parameter, like this

                        <portlet-app version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                         xsi:schemaLocation="http://java.sun.com/xml/ns/portlet"
                         xmlns="http://java.sun.com/xml/ns/portlet">
                         <portlet>
                         <portlet-name>MDP_contribution_consultation</portlet-name>
                         <portlet-class>org.apache.myfaces.portlet.MyFacesGenericPortlet</portlet-class>
                         <portlet-info>
                         <title>MDP_contribution_consultation</title>
                         </portlet-info>
                         <init-param>
                         <name>default-view</name>
                         <value>/WEB-INF/jsp/view.jsp</value>
                         </init-param>
                         <init-param>
                         <name>suppression_init</name>
                         <value>false</value>
                         </init-param>
                         <portlet-preferences>
                         <preference>
                         <name>edit</name>
                         <value>true</value>
                         </preference>
                         </portlet-preferences>
                         <supports>
                         <mime-type>text/html</mime-type>
                         <portlet-mode>VIEW</portlet-mode>
                         </supports>
                         </portlet>
                        </portlet-app>
                        


                        regards

                        • 9. Re: getPreferences with JSF portlet
                          theute

                          getSupprimer should have no parameter.
                          You can get the request from FacesContext.getExternalContext.getRequest();

                          • 10. Re: getPreferences with JSF portlet
                            franco12

                            did you mean
                            PortletPreferences prefs = FacesContextWrapper.getExternalContext.getRequest().getPreferences();

                            or

                            PortletPreferences prefs = FacesContextFactoryImpl.getExternalContext.getRequest().getPreferences();


                            FacesContext cannot be resolved

                            FacesContextWrapper or FacesContextFactoryImpl can't be resolved ("It is indirectly referenced from required .class files")

                            • 11. Re: getPreferences with JSF portlet
                              theute

                              I need to wake up...
                              I meant:
                              FacesContext.getCurrentInstance().getExternalContext().getRequest()

                              Then you can cast it to a PortletRequest.

                              FacesContext is part of the JSF public API.
                              http://java.sun.com/javaee/javaserverfaces/1.0/docs/api/javax/faces/context/FacesContext.html

                              • 12. Re: getPreferences with JSF portlet
                                franco12

                                sorry I don't get it :-s

                                FacesContext doesn't work alone
                                eclipse suggests me FacesContextWrapper or FacesContextFactoryImpl but not FacesContext alone

                                • 13. Re: getPreferences with JSF portlet
                                  theute

                                  Add the JSF API to Eclipse classpath then...

                                  • 14. Re: getPreferences with JSF portlet
                                    franco12

                                    ok :)
                                    first of all I'd like to say thank you for bearing with the n00b I am

                                    ok now eclipse is ok with FacesContext

                                    but now i'm facing to the 'cast' problem
                                    by "casting to a PortletRequest", did you mean :
                                    PortletRequest prefs = FacesContext.getCurrentInstance().getExternalContext().getRequest();
                                    ?

                                    thanks

                                    1 2 Previous Next