I'm using JSF 2.0 and I want to invoke a function defined in a Java controller when I click on an ace:textEntry.
I tried in this way:
<ace:textEntry readonly="true" value="#{myController.getValue()}"
onclick="#{myController.myFunc()}"/>
but when my page is open, the click event is called instantly.
So, I tried with:
<ace:textEntry readonly="true" value="#{myController.getValue()}">
<ace:ajax event="click" listener="#{myController.myFunc()}"/>
</ace:textEntry>
but my page is not rendered.
Is there another way to implement this behaviour ?
PS: I can use similar JSF components instead of ace:textEntry too.
First, you do not access getters directly in JSF for value backing - you access the property. Secondly, you should call the listener with the correct signature. To correct your example I would first rewrite the call like this,
<ace:textEntry readonly="true" value="#{myController.value}">
<ace:ajax event="click" listener="#{myController.myFunc}"/>
</ace:textEntry>
Then define MyController, like this;
#Named
#ViewScoped
public class MyController {
private value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public void myFunc(javax.faces.event.AjaxBehaviorEvent event) {
/* Do somethinig here... */
}
}
Related
I want to use one html page with two ManagedBeans. Depending on the parameter in the URL, I want to set the bean name through JSTL variable. For example:
<c:set var="bean" value="#{webModule1}" scope="request" />
or
<c:set var="bean" value="#{webModule2}" scope="request" />
I have class which was use all this time, so for sure this works:
#ManagedBean(name = "webModule1")
#ViewScoped
public class WebModule1 implements Serializable {
protected WebModulesFormBean form;
#PostConstruct
public void init() {
if (!firstInit()){
this.form= new WebModulesFormBean();
}
//some code
}
public void process() {
if(this.form.isActive()){
//some code
}
}
}
And new one:
#ManagedBean(name = "webModule2")
#ViewScoped
public class WebModule2 extends WebModule1 {
public void process() {
if(this.form.isActive()){
//code with some changes
}
}
}
This solutions works with value attribute and form in not null, for example:
<p:treeTable value="#{bean.form.root}" var="node" id="modulesTree">
But I have problem with this piece of code:
<p:poll listener="#{bean.process()}" widgetVar="documentOutcome" autoStart="#{bean.form.start}" update="modulesTree" async="false"
interval="1" id="myPoll2" />
When listener is called, NullPointerException appears.
And this problem is with all p:polls (I have a few), so this is not a problem with method code.
Problem is that 'form' is null, although at the beginning variable 'form' is initialized and treeTable is shown at the page. So 'form' starts to be null when the listener is called.
Thanks!
I have a solution!
The problem was with:
<c:set var="bean" value="#{webModule1}" scope="request" />
scope="request" was not enough and in my understanding this scope meant that when poll was called, a new bean reference was created and form variable was null because it was not a firstInit().
scope="view" is the solution.
Thank you for comments!
I have two buttons, in one I need <f:convertDateTime> to work but in another I need to disable <f:convertDateTime> on button click.
I tried the attributes rendered and disabled, but it didn't work, which was my mistake as it is not available as per the API docs.
Also, is there a way to override the class javax.faces.converter.DateTimeConverter such that whenever f:convertDateTime is triggered my class will be called?
I tried the attributes rendered and disabled, but it didn't work, which was my mistake as it is not available as per the API docs.
Indeed, this behavior is not supported. However, as to a possible solution, you basically already gave the answer yourself:
Also, is there a way to override the class javax.faces.converter.DateTimeConverter such that whenever f:convertDateTime is triggered my class will be called?
That is possible and will also solve your initial problem. Just register it as <converter> in faces-config.xml on exactly the same <converter-id> as <f:convertDateTime>.
<converter>
<converter-id>javax.faces.DateTime</converter-id>
<converter-class>com.example.YourDateTimeConverter</converter-class>
</converter>
Therein you could do additional conditional checking, such as checking if a certain button is pressed, or if a certain request parameter is present or absent. If you'd like to continue the default <f:convertDateTime> job, just delegate to super provided that your converter extends from DateTimeConverter.
E.g. in getAsObject():
public class YourDateTimeConverter extends DateTimeConverter {
#Override
public void getAsObject(FacesContext context, UIComponent component, String submittedValue) {
// ...
if (yourCondition) {
// Do your preferred way of conversion here.
// ...
return yourConvertedDateTime;
} else {
// Do nothing. Just let default f:convertDateTime do its job.
return super.getAsObject(context, component, submittedValue);
}
}
// ...
}
I'm guessing you have some text displayed based on a button click (correct me if I'm wrong). So it should be something like that:
<h:commandButton value="Convert" action="#{bean.doBtnConvert}" />
<h:commandButton value="Don't convert" action="#{bean.doBtnDontConvert}" />
<h:panelGroup id="pgText">
<h:outputText value="#{bean.someDateTime}" rendered="#{bean.convert}">
<f:convertDateTime pattern="dd.MM.yyyy HH:mm:ss" />
</h:outputText>
<h:outputText value="#{bean.someDateTime}" rendered="#{not bean.convert}"> />
</h:panelGroup>
And in bean you have the following field and methods:
private Date someDate;
private boolean convert;
public String doBtnConvert(){
setConvert(true);
String viewId = FacesContext.getCurrentInstance().getViewRoot().getViewId();
return viewId + "?faces-redirect=true";
}
public String doBtnDontConvert(){
setConvert(false);
String viewId = FacesContext.getCurrentInstance().getViewRoot().getViewId();
return viewId + "?faces-redirect=true";
}
// getter and setter for 'someDate' and 'convert' fields
I have a button. If this is pressed, a checkbox should be rendered.
This of course works pretty well. The checkbox should, when changed call a function, which should just System.out.println() something so I see it's called.
The problem is: just the simple checkbox function, without the rerendering works pretty well. But as soon as the a4j:support is rerendered, it's not working, the System.out.println() is never called
This looks so easy, but I don't know why it's not working at all!
Any hint/trick?
This is old environment, so JSF 1.2 only.
My xhtml
<s:div id="sdBoolCheckboxtest">
#{testController.testRerenderBool}
<s:div id="sdRerender" rendered="#{testController.testRerenderBool}">
<h:selectBooleanCheckbox id="myscheckbox" value="#{testController.testBool}">
<a4j:support ajaxSingle="true" event="onclick" immediate="true"
status="globalStatus" action="#{testController.testCheckbox()}" />
</h:selectBooleanCheckbox>
</s:div>
</s:div>
</h:form>
My Java class:
#Name("testController")
#AutoCreate
public class TestController {
private boolean testBool = false;
public boolean isTestRerenderBool() {
return testRerenderBool;
}
public void setTestRerenderBool(boolean testRerenderBool) {
this.testRerenderBool = testRerenderBool;
}
private boolean testRerenderBool;
public void switchtestRerenderBool(){
System.out.println("switch");
testRerenderBool = !testRerenderBool;
}
public void testCheckbox(){
System.out.println("drin");
}
public boolean isTestBool() {
return testBool;
}
public void setTestBool(boolean testBool) {
this.testBool = testBool;
}
}
Okay, i got it working, with these changes:
First i added #Scope(ScopeType.CONVERSATION) to the TestController
#Name("testController")
#AutoCreate
#Scope(ScopeType.CONVERSATION)
public class TestController {
...
}
And as a second change, I changed the <s:div to an <a4j:outputPanel
<a4j:outputPanel id="sdBoolCheckboxtest">
This solved my problem, but I'm still wondering why I need the #Scope(ScopeType.CONVERSATION) (I found the hint with the a4j:outputPanel here)
Any explanation would be helpful, thanks!
I'm using JSF 2.0 and Richfaces 3.3.2.
I'm having an issue on one of my pages.
First of all I have a main page which contains a rich:tabPanel with several rich:tabs, no problem there switching between them.
On the first tab of my page I have another tabPanel with some other tabs.
They all have an actionListener which will call the ViewScoped ManagedBean to edit some Data, which will be displayed on the clicked tab. The method to edit the data calls a method on an ApplicationScoped Bean which is correctly injected (It works in other tabs). When switching a tab on the second layer and the action-method is called, the ApplicationScoped Bean is null.
This only happens when I switch tabs (so it does not happen on the first tab, which is displayed by default) so I think it's related to the actionListener, but i can't figure out why. Unfortunately I can't provide the actual code but this is the rough structure of my page.
mainPage.xhtml
...
<rich:tabPanel>
<rich:tab>
<ui:include src="tabs/tab1.xhtml" />
</rich:tab>
otherTabs
</rich:tabPanel>
...
tab1.xhtml
...
<rich:tabPanel>
<rich:tab actionListener="#{viewScopedBean.method}">
<ui:include src="subtabs/subtab1.xhtml" />
</rich:tab>
<rich:tab actionListener="#{viewScopedBean.method2}">
<ui:include src="subtabs/subtab2.xhtml" />
</rich:tab>
</rich:tabPanel>
ViewScopedBean.java
#ManagedBean
#ViewScoped
public class ViewScopedBean {
#ManagedProperty(value="#{applicationBean}")
private ApplicationBean applicationBean;
private Data data;
public void init() {
...
data = applicationBean.retrieveData();
...
}
public void method(ActionEvent e) {
...
data = applicationBean.retrieveData();
...
}
public void method2(ActionEvent e) {
...
data = applicationBean.retrieveData();
...
}
// getters/setters
}
ApplicationBean.java
#ManagedBean
#ApplicationScoped
public class applicationBean {
public Data retrieveData() {
...
}
}
When clicking subtab2 I get a NullPointerException at the line data = applicationBean.getData(), so the applicationBean is null. This happens everytime on every tab I'm switching to, but never in the init() method, so the applicationBean should not be null.
I could not find any solution or hints on this problem and hope someone has an idea.
Thanks in advance
regards
I want to upload many files and I have chosen rich:fileUpload control for this.
My problem is that I need to add more information for each file, for example the title I want to appear in the application for that file. How can I do that, and send to the fileUploadListener method in order to use the id?
Based in your question, the RichFaces FileUpload demo has all the info you need to handle file upload for 1 or more files at the same time.
If you want to add more data (like h:inputText values and others), then you should pass them using valueChangeListener instead value tag attribute, because the fileUploadListener is an event that happens within an ajax call, so your UIComponents won't call the setters for the attributes.
Some code to explain the behavior:
<h:panelGrid cols="2">
<h:outputText value="File Title:">
<h:inputText value="#{fileBean.fileTitle}" immediate="false"
valueChangeListener="#{fileBean.valueChangeFileTitle}" />
<h:outputText value="File:">
<rich:fileUpload
fileUploadListener="#{bean.fileUpload}">
</rich:fileUpload>
</h:panelGrid>
The Bean to handle the requests
public class Bean {
private String fileTitle;
public Bean() {
}
//getters and setters...
public String getFileTitle() {
return this.fileTitle;
}
public void setFileTitle(String fileTitle) {
System.out.println("Calling the setter");
this.fileTitle = fileTitle;
}
public void valueChangeFileTitle(ValueChangeEvent e) {
System.out.println("Calling the ValueChangeListener");
fileTitle = (String)e.getNewValue();
}
//this will be invoked by an ajax call
//the setter of the view won't be invoked for fileTitle
//instead, we should take its value using valueChangeListener
public void fileUpload(UploadEvent ue) {
MyFileManager mfm = MyFileManager.getFileManager();
MyFile myFile = new MyFile();
myFile.setTitle(this.fileTitle);
myFile.setName(ue.getUploadItem().getFileName());
myFile.setData(ue.getUploadItem().getData());
mfm.createFile(myFile);
}
}
Also, avoid to use System.out.println calls in your code, I'm doing it so you can understand what method will be called, instead use a Logger like Log4j.