Javascript function not working inside composite control JSF [duplicate] - java

This question already has answers here:
Adding <h:form> causes java.lang.IllegalStateException: Cannot create a session after the response has been committed
(5 answers)
Closed 7 years ago.
I have a composite control as below;
&LT;html xmlns="http://www.w3.org/1999/xhtml"
xmlns:composite="http://java.sun.com/jsf/composite"
xmlns:h="http://java.sun.com/jsf/html">
<composite:interface>
<composite:attribute name="validatorId"/>
<composite:attribute name="message"/>
</composite:interface>
<composite:implementation>
<div>
<script type="text/javascript">
function #{cc.attrs.validatorId}(){alert('why is it not working :(');};
</script>
<div style="background-color: #F2DEDE;color: #b94a48;border-style: solid;border-color: #FAC3CB;border-width: 1.8px;font-family: Arial;
font-size: 12px;height:17px;vertical-align: middle;padding-left: 8px;padding-right: 8px;padding-top: 2px;display: none;"
id="#{cc.attrs.validatorId}">
#{cc.attrs.message}
<img src="images/close-mini.png" style="cursor: pointer;padding-left: 5px;"/>
</div>
</div>
</composite:implementation> </html>
This control is placed in a xhtml file.The javascript and composite controls are rendering as the expected way. But when a request is made to xhtml following exception occurs at Glassfish Server :
java.lang.IllegalStateException: PWC3999: Cannot create a session after the response has been committed.
What can be the reason? When I remove the javascript function exception doesn't occurs.

The following prerender listener is added at composite control,
<f:event type="preRenderView" listener="#{fieldValidatorBean.PreRenderValidator}"/>
and inside the listener function,
public void PreRenderValidator(){
FacesContext.getCurrentInstance().getExternalContext().getSession(true);
System.out.println("prerender being called");
}
to reenable session. Thanks #BalusC.

Related

How does the POST REDIRECT GET works in JSF on commandButton

I was testing couple of new features of JSF and I came across Post Redirect Get.
I wanted to redirect from my first page say first.xhtml to second.xhtml.
I have a number as a property in both the managed beans and I wanted to pass it to the second bean from the first bean using request parameter.
This is my first page
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<head>
<title>Landing Page</title>
</head>
<body>
<h3>Enter Number</h3>
<h:form>
<h:inputText id="input" name="number" value="#{postRedirectGet.number}" />
<h:commandButton value="redirect to result"
action="resultPage?faces-redirect=true&includeViewParams=true">
</h:commandButton>
</h:form>
</body>
</html>
And in the second page I have
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:metadata>
<f:viewParam name="number" value="#{postRedirectResult.number}"/>
</f:metadata>
<head>
<title>Result Page</title>
</head>
<body>
<h:form>
<h:outputText value="Number #{postRedirectGet.number}" />
<h:outputText value="Number #{postRedirectResult.number}" />
<h:commandButton value="Redirect to index" action="/index?faces-redirect=true" />
</h:form>
</body>
</html>
Now the page is doing a POST using commandButton and then redirecting to second page from first but it passes number=0 in the URL. It works if I change
<f:viewParam name="number" value="#{postRedirectResult.number}"/>
to
<f:viewParam name="number" value="#{postRedirectGet.number}"/>
but I thought the viewParam is used to set the value to a bean and not used to pass the values in URL. Could someone please explain how can we do POST and set the property of the managed bean on next page.
The problem is that the f:viewParam is used in two different ways in two scenarios . In h:link it is used to set the property of target bean , in h:commandButton it is used to compute the GET URL and then the target bean property can be set using #ManagedProperty . Is my understanding correct or can the f:viewParam be used to set the property in h:commandButton POST redirect get also.
What you seem to be missing is what includeViewParams does. Let me quote this very informative article (you should read all of it):
The other special query string parameter, includeViewParams, tells the navigation handler to include the view parameters when performing the navigation. But what view parameters should be included? The view parameters to be included when performing the navigation are declared on the to-view-id page.
So JSF looks at your resultpage.xhtml to determine which parameters to pass. And then dutifully proceeds to pass the current value of postRedirectResult#number (which at this time is unset/0).
To have the GET number parameter reflected in your bean, pass it as a real parameter:
<h:commandButton value="redirect to result"
action="resultPage?faces-redirect=true&number=4" />
See also:
What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?
There are different 4 ways to transfer data from JSF Page To Backing Bean.
We can use
f:param
f:setPropertyActionListener
f:attribute
Method expression (JSF 2.0).
Here you can try f:setPropertyActionListener as..
<h:commandButton value="redirect to result"
action="resultPage?faces-redirect=true">
<f:setPropertyActionListener target="#{postRedirectResult.number}" value=4 />
</h:commandButton>
Here is the link for this.

Access bean inside repeat in jsf [duplicate]

This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
Closed 9 years ago.
I have a userProfileBean which I can access outside of the repeat but when I do
<ui:repeat id="reqlistID" var="reqlist" value="#{userProfileBean.friendRequestsList}">
jmeno: #{reqlist.jmeno} <br/> prijmeni: #{reqlist.prijmeni} <br/> id: #{reqlist.id} <br/>
<p:commandButton id="allowFriendButton"
action="#{userProfileBean.allowFriend()}"
value="Přidat"
update="reqlistID">
</p:commandButton>
Then the allowFriend method doesn't get called and netbeans says unknown property. Am I missing something?
Edit: thanks for the replies. Still doesn't work. Now the state is this.
<h:form>
<p:commandButton id="allson"
action="#{userProfileBean.testt()}"
value="Přidat"
ajax="false">
</p:commandButton>
</h:form>
<c:if test="#{principalBean.p.login == userProfileBean.name}">
Žádosti přidání do přátel:<br/>
<ui:repeat id="reqlistID" var="reqlist" value="#{userProfileBean.friendRequestsList}">
jmeno: #{reqlist.jmeno} <br/> prijmeni: #{reqlist.prijmeni} <br/> id: #{reqlist.id} <br/>
<h:form>
<p:commandButton id="allowFriendButton"
action="#{userProfileBean.testt()}"
value="Přidat"
ajax="false">
</p:commandButton>
</h:form>
<br/>
</ui:repeat>
</c:if>
The first <p:commandButton> works and calls the method properly (with page reload). The second button reloads the page but does not call the method. The problem seems to be with accessing the userProfileBean from inside the repeat. But I can't google out any explanation.
I guess you're missing the form element. Try putting your buttons in a form like
<h:form>
<ui:repeat id="reqlistID" var="reqlist" value="#{userProfileBean.friendRequestsList}">
jmeno: #{reqlist.jmeno} <br/> prijmeni: #{reqlist.prijmeni} <br/> id: #{reqlist.id} <br/>
<p:commandButton id="allowFriendButton"
action="#{userProfileBean.allowFriend()}"
value="Přidat"
update="reqlistID">
</p:commandButton>
</ui:repeat>
</h:form>
Found the source of the problem, the form is getting regenerated and the bean cannot find the original button element, because it is request scoped. Tried to implement a view scope, but something failed there. So I just made the bean session scoped.

JSF Component IDs ignored when dynamically generating Views

I ran into a bit of a problem.
I generate Views dynamically by iterating a list of strings via the c:forEach tag
and then including them via the facelets include tag.
This works fine for building the layout but shows some strange behaviour.
I have a primefaces tabview containing 2 tabs.
For the first Tab (the one that gets initially displayed) the component id is set (eg. tabview:categoryTab) but that's not the case for e.g. the second tab, here I only get tabview: for the component id (but it actually should be tabview:usrTab)
Why does JSF override the id I set for the second Tab? Am I missing some crucial information from the spec?
I use JSF 2 with Primefaces 3.6 (snapshot build)
(And yes, I use the snapshot build on purpose and have tested this with stable PF releases as well but the same behaviour occurs)
edit
Code:
admin.xhtml
<ui:composition template="/templates/commonLayout.xhtml">
<ui:define name="content">
<p:panel id="parentPanel">
<h:outputText value="Verwaltung" />
<br />
<p:tabView id="tabview">
<!-- insert marker -->
<c:forEach items="#{adminTabs}" var="tab">
<ui:include src="#{tab}" />
</c:forEach>
</p:tabView>
</p:panel>
</ui:define>
</ui:composition>
catTab.xhtml
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<p:tab title="Categories Tab" id="catTab">
....
</p:tab>
</ui:composition>
usrTab.xhtml
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<p:tab title="Users Tab" id="usrTab">
....
</p:tab>
</ui:composition>
testTab.xhtml
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<p:tab title="TestTab" id="testTab">
....
</p:tab>
</ui:composition>
ContentProvider.java
public class ContentProvider {
....
#Produces
#Named("adminTabs")
public List<String> getTabs(){
List<String> components = new ArrayList<String>();
components.add("/templates/tabs/catTab.xhtml");
components.add("/templates/tabs/usrTab.xhtml");
components.add("/templates/tabs/testTab.xhtml");
return components;
}
....
}
This gets generated:
<ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all" role="tablist">
<li class="ui-state-default ui-tabs-selected ui-state-active ui-corner-top" aria-expanded="true" role="tab">
Categories
</li>
<li class="ui-state-default ui-corner-top" aria-expanded="false" role="tab">
TestTab
</li>
<li class="ui-state-default ui-corner-top" aria-expanded="false" role="tab">
Benutzer und Rollen
</li>
</ul>
So, to reiterate: Only the id for the first tab is retained, the ids for the other tabs are generated despite being set in the xhtml code...
I ran into something very similar with Richfaces 4 and JSF 2. I managed to hack around it by including an EL expression in the id attribute. For me
<h:form id="staticName">
was being rendered into
<form id="j_idblah>
but once I did
<h:form id="#{_objectInContext}">
it began rendering properly. Pretty hacky, but for now it'll work. Good luck!!
I ran into the same problem when including facelets with a c:forEach loop.
What worked for me was specifying the Ids as EL-Constants: id="#{'address_street'}"

jboss-seam-pdf inside rich:modalPanel cannot work

I have a modalPanel declared as
<rich:modalPanel id="printEmailExcel" minWidth="800" minHeight="600" resizable="true">
<div style="width: 1055px; height: 700px; overflow: auto">
<s:decorate template="/view/popup/printEmailExcelPopup.xhtml"styleClass="popupCDiv">
<ui:param name="panelid" value="printEmailExcel" />value="/view/printForm/order.xhtml" />
</s:decorate>
</div>
</rich:modalPanel>
There is also a commandbutton to show the modalPanel in the same page
<s:button oncomplete="#{rich:component('printEmailExcel')}.show()" image="/images/icons/btn_print.gif" ></s:button>
inside the printEmailExelPopup.xhtml, there is a
<h:form><s:button view="/view/print/order.xthml" /></h:form>
order.xhtml :
<p:document xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://jboss.com/products/seam/pdf">
The document goes here.
< /p:document>
The problem is that when i click the s:button in the modalPanel, it only refresh the whole page and the pdf file IS NOT downloaded. However, if i place the s:button outside the modalPanel(same level of the rich:modalPanel, it works normally). Any Help?
try surrounding your commandButton with form tags.

How to define an onLoad function in JSF template that will be defined elsewhere

I'm designing the view for my site, which has a standard login and landing page, and I want to have an onLoad function called for my login page, but not for my other pages (yet). I've got a template.xhtml file, which has this insert:
<div id="content">
<ui:insert name="content"/>
</div>
Then in login.xhtml I have:
<ui:define name="content">
...
</ui:define>
Normally I would put this in login.xhtml:
<body onload="document.getElementById('login_form:name').focus();">
But since I'm using JSF's ui composition tags, I can't have the <body/> tag in login.xhtml (at least the way I am attempting to do it).
Is there a way to accomplish this with the structure I've described? The way I would think of doing it is to have onLoad call a function in the template, and then each page with ui:define would populate this function. Is that possible?
Thanks!
I can think of at least two ways:
define the header section with <ui:define name="header">, and put a javascript function (function bodyLoaded(){..}) in it - different on every page, and then reference it via <body onload="bodyLoaded();">
use facelets params. I.e. <body onload="#{onLoadJS}"/> and on each page including the template use <ui:param name="onLoadJS" value="document.getElementById(..)" />

Categories