commandLink action not called [duplicate] - jsf

This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
Closed 5 years ago.
I'm using netbeans with glassfish
In my .xhtml file, a have a link which I expect to call action shopBean.instructorScheduleById()
<p:commandLink styleClass="tooltip tour-sched"
rendered="#{shopBean.instructorSchedule!=null}"
update=":dialogFormTS" action="#
{shopBean.instructorScheduleById()}"
oncomplete="dlgTS.show()" process="#this"
ajax="true">TS<h:outputText styleClass="tooltiptext"
value="#{shopBean.instructorSchedule.name}"/>
</p:commandLink>
Bean file
#ManagedBean(name = "shopBean")
#SessionScoped
public class ShopBean {
public ShopBean() {}
public void getInstructorScheduleById(){
System.out.println("hello");
}
}
I looked to my Developer tools under network i see there is an ajax request but when i looked on glassfish output, there's no word hello

Base on what i have learned here at stackoverflow, the get prefix is used for getter methods which means it must have a return value.
public void sum(){
int num1 = 1;
int num2 = 2;
System.out.println(num1 + num2);
}
// xhtml
<p:commandButton action="#{bean.sum()}" />
if you want get the value from a getter method:
pivate int sum;
public int getSum(){
return this.sum;
}
public void setSum(sum){
this.sum = sum;
}
// xhtml
<p:outputText value="#{bean.sum}" />
The quick solution for your problem is to remove the get prefix in your
bean.

Related

how can i pass an url parameter between two xhtml pages? [duplicate]

This question already has answers here:
Does f:viewParam only pass query string in url when first page uses the same managed bean as the second page?
(2 answers)
Bookmarkability via View Parameters feature
(1 answer)
Closed 2 years ago.
i am trying to pass a parameter between two pages
in the bean i have this string with getters and setters
outcome is the function to run with the button
#Named("MeubleBean")
#ManagedBean
#SessionScoped
#RequestScoped
public class MeubleBean implements Serializable {
private String param1;
public String outcome() {
FacesContext fc = FacesContext.getCurrentInstance();
Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
param1 = params.get("param1Name");
return "meubles3.jsf?faces-redirect=true&includeViewParams=true";
}
}
the code of the command button in the page meubles.xhtml :
<f:param id="param1" name="param1Name" value="param1Value"/>
</h:commandButton>
and finally the output in meubles3.xhtml :
<h:form><h:outputText value="#{meubleBean.param1}"></h:outputText>
</h:form>
i am trying the "param1value" as test value .
the output is always empty, i tried several ways none of em worked.
any solution please ???

UI is not updating the backing bean [duplicate]

This question already has answers here:
How to choose the right bean scope?
(2 answers)
Closed 5 years ago.
This is my UI code in JSF. The app is a small currency conversion from Singaporean dollar to Japanese Yen.
<h:form>
<h:inputText id="conversion" value="#{conversor.sgd}"></h:inputText>
<h:commandButton value = "Convertir a yen">
<f:ajax execute = "#all" render = "conversion_lista" event="click"/>
</h:commandButton>
<h2><h:outputText id = "conversion_lista" value = "Conversion: #{conversor.jpy}"/></h2>
</h:form>
And this is my backing bean:
#Named("conversor")
#Dependent
public class Conversor {
private int sgd;
public Conversor() {
sgd = 1; //initial value
}
public int getSgd(){
return sgd;
}
public void setSgd(int sgd){
this.sgd = sgd;
}
public int getJpy(){
return sgd * 2; //fake
}
}
I want to click on the commandButton, execute the first inputText, make the sgd property in the backing bean change, then render the outputText by previously calculating the converstion from Singaporean dollar to Japanese yen.
However the input isn't passed to the backing bean.
I did try some things like, making getJpy() return a random number everytime it's called, and it did render a random number everytime I clicked the button. So that means that call works. But I can't pass the text input (sgd/singaporean dollar) to the backing bean and make the right conversion.
What's going on in here?
Ok, I fixed it.
I used #RequestScoped instead of #Dependent.
I have no idea what I'm doing, but it works.
Now the outcome is what I wanted, the conversion takes place on the UI when I click the button.

How can I add f:selectItem with the message "select..." to h:selectOneMenu of objects (JSF)? [duplicate]

This question already has answers here:
Best way to add a "nothing selected" option to a selectOneMenu in JSF
(3 answers)
Closed 6 years ago.
I am working with JSF 2.2 and I would like the next alert message (picture) don't show when user selected the "Seleccione..."message:
[serverERROR: class javax.faces.component.UpdateModelException Cannot convert Seleccione... of type class java.lang.String to class modelo.entidades.Equipo]
My Bean is:
#ManagedBean
#SessionScoped
public class Bean{
private String message;
private Equipo equipoSelected;
private List<Equipo> equipos;
public Bean() {
}
#PostConstruct
public void init(){
message = "Seleccione...";
equipos = new ArrayList<Equipo>();
equipos.add(new Equipo(/*....*/));
equipos.add(new Equipo(/*....*/));
equipos.add(new Equipo(/*....*/));
equipoSelected = new Equipo();
}
public void updateSelected(){
//...
}
public setMessage(String message){
this.message = message;
}
public String getMessage(){
return message;
}
public void setEquipos(List<Equipo> equipos){
this.equipos = equipos;
}
public List<Equipo> getEquipos(){
return equipos;
}
public void setEquipoSelected(Equipo equipoSelected){
this.equipoSelected = equipoSelected;
}
public Equipo getEquipoSelected(){
return equipoSelected;
}
}
and the xhtml code is:
<h:form>
<h:selectOneMenu value="#{bean.equipoSelected}">
<f:selectItem itemValue = "#{null}" itemLabel="#{bean.message}"/>
<f:selectItems value="#{bean.equipos}" var="equipo" itemLabel="#{equipo.name}" itemValue="#{equipo}"/>
<f:ajax event="change" listener="#{bean.updateSelected}" render="#form" execute="#form"/>
</h:selectOneMenu>
</h:form>
thank you!
You're using objects of type Equipo in the Java bean, but the JSF component only copes with strings. You can fix this using a converter. Here's a good explanation of converters in general. This StackOverflow question has examples specifically for JSF (both with and without PrimeFaces).
You may also want to adopt the converter of OmniFaces. That's adding another library, but after that, the converter "automagically" does everything for you with almost no extra effort on your side.
Try with add attribute noSelectionOption="true" in tag f:selectItem.
These other question maybe help you:
Best way to add a "nothing selected" option to a selectOneMenu in JSF

Parsing argument with JSF commandlink to managed bean [duplicate]

This question already has an answer here:
How to write a hardcoded string value inside EL expression #{ } in JSF?
(1 answer)
Closed 6 years ago.
I tried to parse argument with JSF to managed bean with ajax. my JSF code is this
<h:commandLink id="user" action="#{pageBean.setPage("user")}" >
user
<f:ajax execute="user" render="contentBody" />
</h:commandLink>
managed bean is this
#ManagedBean
public class PageBean {
private String path;
private String page;
public PageBean() {
}
#PostConstruct
public void init(){
path = "/WEB-INF/dashboard.xhtml";
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
}
But when I run this I got following error. Why is that?
Error Parsing /WEB-INF/templete.xhtml: Error Traced[line: 37] Element type "h:commandLink" must be followed by either attribute specifications, ">" or "/>".
You are using double quotes in your attribute around user.
<h:commandLink id="user" action="#{pageBean.setPage("user")}" >
This results in templete.xml not beeing a valid XML-File.
Correct example line using single quotes (as proposed by #gWombat):
<h:commandLink id="user" action="#{pageBean.setPage('user')}" >
You should use simple quotes around the parameter user:
action="#{pageBean.setPage('user')}"

h:commandButton inside h:dataTable [duplicate]

This question already has answers here:
How can I pass selected row to commandLink inside dataTable or ui:repeat?
(4 answers)
Closed 7 years ago.
I am using a JSF data table. One of the columns in the table is a Command button.
When this button is clicked I need to pass few parameters (like a value of the selected row) using the Expression language. This paramaters need to be passed to the JSF managed bean which can execute methods on them.
I have used the following snippet of code but the value i am getting on the JSF bean is always null.
<h:column>
<f:facet name="header">
<h:outputText value="Follow"/>
</f:facet>
<h:commandButton id="FollwDoc" action="#{usermanager.followDoctor}" value="Follow" />
<h:inputHidden id="id1" value="#{doc.doctorid}" />
</h:column>
Bean Method:
public void followDoctor() {
FacesContext context = FacesContext.getCurrentInstance();
Map requestMap = context.getExternalContext().getRequestParameterMap();
String value = (String)requestMap.get("id1");
System.out.println("Doctor Added to patient List"+ value);
}
How can I pass values to the JSF managed bean with a commandbutton?
Use DataModel#getRowData() to obtain the current row in action method.
#ManagedBean
#ViewScoped
public class Usermanager {
private List<Doctor> doctors;
private DataModel<Doctor> doctorModel;
#PostConstruct
public void init() {
doctors = getItSomehow();
doctorModel = new ListDataModel<Doctor>(doctors);
}
public void followDoctor() {
Doctor selectedDoctor = doctorModel.getRowData();
// ...
}
// ...
}
Use it in the datatable instead.
<h:dataTable value="#{usermanager.doctorModel}" var="doc">
And get rid of that h:inputHidden next to the h:commandButton in the view.
An -less elegant- alternative is to use f:setPropertyActionListener.
public class Usermanager {
private Long doctorId;
public void followDoctor() {
Doctor selectedDoctor = getItSomehowBy(doctorId);
// ...
}
// ...
}
With the following button:
<h:commandButton action="#{usermanager.followDoctor}" value="Follow">
<f:setPropertyActionListener target="#{usermanager.doctorId}" value="#{doc.doctorId}" />
</h:commandButton>
Related:
The benefits and pitfalls of #ViewScoped - Contains CRUD example using DataModel<E>.

Resources