Problem with RequestContext in a Portlet

UI Components for JSF
Post Reply
formenti
Posts: 9
Joined: 14 Dec 2012, 13:50

09 Apr 2013, 18:29

Hi,
I have a problem with RequestContext.update and scrollTo within a Portlet.
Here the implementation (PF 3.5), the behaviour is no update (even if a breakpoint allow me to detect that the method is invoked and i can see the <partial-response><changes><update id="javax.faces.ViewState">IZuHfC8ekpjXbJ9+SCHZLaAa7+/lC/4/KEpSk1+/tZXD4N3dV6SVKEOgDYH2ABw4mz6oWeIITJk7GwViop1kM66zKjlkRv3hfZP1NuqH1wluJqal5rNBxFK4VDA=</update></changes></partial-response> AJAX response) and with scrollTo i get a "b is undefined" javascript error.

Code: Select all

	<f:view>

		<h:outputStylesheet id="stylesheet1" name="theme.css" library="primefaces-aristo"></h:outputStylesheet>
		<h:outputStylesheet id="stylesheet2" name="primefaces.css" library="primefaces"></h:outputStylesheet>
		<h:outputScript id="script1" name="jquery/jquery.js" library="primefaces"></h:outputScript>
		<h:outputScript id="script4" name="jquery/jquery-plugins.js" library="primefaces"></h:outputScript>
		<h:outputScript id="script2" name="primefaces.js" library="primefaces"></h:outputScript>

	<h:form>
        <h3 style="margin-top:0">RequestContext (update)</h3>

        <p:selectBooleanCheckbox id="checkbox" itemLabel="Update first output"
                                 value="#{requestContextController.firstOutput}"/>
        
        <h:panelGrid columns="2" style="margin-top:10px;">
            <h:outputText value="First Output"/>
            <h:outputText id="firstOutput" value="#{requestContextController.counter}"/>
        
            <h:outputText value="Second Output"/>
            <h:outputText id="secondOutput" value="#{requestContextController.counter}"/>
        
            <f:facet name="footer">
                <p:commandButton value="Increment counter"
                                 actionListener="#{requestContextController.incrementWithUpdate}"
                                 process="@form" style="margin:10px 0 10px 0;"/>
            </f:facet>
        </h:panelGrid>

        <h3 style="margin-top:20px">RequestContext (scrollTo)</h3>

        <h:panelGrid id="counter" columns="2" style="font-weight:bold;">
            <h:outputText value="Counter"/>
            <h:outputText value="#{requestContextController.counter}"/>
        </h:panelGrid>
        
        <p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p>
        <p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p>
        <p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p>
        <p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p>
        <p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p>
        <p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p>        
        
        <p:commandButton value="Increment counter"
                         process="@form" update="counter"
                         actionListener="#{requestContextController.incrementWithScroll}"
                         style="margin:10px;"/>

		</h:form>
	</f:view>

Code: Select all

@ManagedBean(name = "requestContextController")
@ViewScoped
public class RequestContextController implements Serializable {
	private boolean firstOutput = true;
	private int counter = 0;

	public void incrementWithUpdate(ActionEvent ae) {
		counter++;
		RequestContext requestContext = RequestContext.getCurrentInstance();
		if (firstOutput) {
			requestContext.update("firstOutput");
		} else {
			requestContext.update("secondOutput");
		}
	}

	public void incrementWithScroll(ActionEvent ae) {
		counter++;
		RequestContext requestContext = RequestContext.getCurrentInstance();
		requestContext.scrollTo("counter");
	}

[...]

smithh032772
Posts: 6144
Joined: 10 Sep 2011, 21:10

10 Apr 2013, 01:41

Howard

PrimeFaces 6.0, Extensions 6.0.0, Push (Atmosphere 2.4.0)
TomEE+ 1.7.4 (Tomcat 7.0.68), MyFaces Core 2.2.9, JDK8
JUEL 2.2.7 | OmniFaces | EclipseLink-JPA/Derby | Chrome

Java EE 6 Tutorial|NetBeans|Google|Stackoverflow|PrimeFaces|Apache

formenti
Posts: 9
Joined: 14 Dec 2012, 13:50

10 Apr 2013, 02:04

Hi Howard,
as far as I know my javascript is correctly referenced within the page (i can see it in Firebug), so I don't think I need to investigate on how to include js in my portlet. Because of this, others javascript behaviour are working well (for example an explicit "update" client-side is working).
If you have more specific suggestion for the RequestContext-related problem, please provide a suggestion or a reference to a specific webpage.
Thank you for your contribution

smithh032772
Posts: 6144
Joined: 10 Sep 2011, 21:10

10 Apr 2013, 02:22

Since you say other/existing javascript is working as designed, it might be good to debug PrimeFaces RequestContext source code against a portlet and a non-portlet page.

See/click URL below. Since I am using PrimeFaces Push in my app, I decided to replace the (sometimes-unreliable) use/need of RequestContext with the 'always-reliable' websocket (PrimeFaces Push / Atmosphere).

http://stackoverflow.com/questions/3471 ... 9#15897149
Howard

PrimeFaces 6.0, Extensions 6.0.0, Push (Atmosphere 2.4.0)
TomEE+ 1.7.4 (Tomcat 7.0.68), MyFaces Core 2.2.9, JDK8
JUEL 2.2.7 | OmniFaces | EclipseLink-JPA/Derby | Chrome

Java EE 6 Tutorial|NetBeans|Google|Stackoverflow|PrimeFaces|Apache

formenti
Posts: 9
Joined: 14 Dec 2012, 13:50

11 Apr 2013, 09:28

Finally I found the problem investigating on the "b is undefined" javascript error:
within the Portal each ID has the namespace prefix, because of this a server-side invoked javascript need to have a ID with the portlet namespace:

Code: Select all

RequestContext requestContext = RequestContext.getCurrentInstance();
String namespace=((PortletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse()).getNamespace();
requestContext.scrollTo("view"+namespace+":counter");
This is the clue: requestContext.scrollTo("view"+namespace+":counter")

I hope this can help

smithh032772
Posts: 6144
Joined: 10 Sep 2011, 21:10

11 Apr 2013, 14:05

Good. Thanks for sharing!
Howard

PrimeFaces 6.0, Extensions 6.0.0, Push (Atmosphere 2.4.0)
TomEE+ 1.7.4 (Tomcat 7.0.68), MyFaces Core 2.2.9, JDK8
JUEL 2.2.7 | OmniFaces | EclipseLink-JPA/Derby | Chrome

Java EE 6 Tutorial|NetBeans|Google|Stackoverflow|PrimeFaces|Apache

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 70 guests