Parsing argument with JSF commandlink to managed bean [duplicate] - jsf

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')}"

Related

EL expression in JSF f:param name attribute

I tried the following:
Something like the facelet I used:
...
<h:button value="x" outcome="nextpage">
<f:param name="#{myBean.PARAM_NAME}" value="someValue"/>
</h:button>
...
Something like the managed bean I used as a controller for the previous facelet:
#Named(value="myBean")
#ViewScoped
public MyBean implements Serializable
{
private static final String PARAM_NAME = "paramName";
public String getPARAM_NAME()
{ return PARAM_NAME };
#PostConstruct
public void init()
{
String passedParamValue = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(
PARAM_NAME );
...
}
}
The advantage of this is : I use the param name in two places. In the facelet and in the managed bean as well. This two places are separated. I sould use constants to reduced the possibility of the mistyping. But it seems the name of the f:param does not evaluate EL expressions. (passedParamValue is always null). But if I directly wire the text 'paramName' to the name attribute, it works fine. Am I right or is there any way to use constants here?

commandLink action not called [duplicate]

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.

Automatically set value of a managed bean variable with JSF

I would like to pass an value to a managed bean under the hood. So I have this managed bean:
#ManagedBean(name = "mbWorkOrderController")
#SessionScoped
public class WorkOrderController {
// more attributes...
private WorkOrder workOrderCurrent;
// more code here...
public WorkOrder getWorkOrderCurrent() {
return workOrderCurrent;
}
public void setWorkOrderCurrent(WorkOrder workOrderCurrent) {
this.workOrderCurrent = workOrderCurrent;
}
}
It holds a parameter workOrderCurrent of the custom type WorkOrder. The class WorkOrder has an attribute applicant of type String.
At the moment I am using a placeholder inside my inputtext to show the user, what he needs to type inside an inputText.
<p:inputText id="applicant"
value="#{mbWorkOrderController.workOrderCurrent.applicant}"
required="true" maxlength="6"
placeholder="#{mbUserController.userLoggedIn.username}" />
What I want to do, is to automatically pass the value of mbUserController.userLoggedIn.username to mbWorkOrderController.workOrderCurrent.applicant and remove the inputText for applicant completely from my form.
I tried to use c:set:
<c:set value="#{mbUserController.userLoggedIn.username}" target="#{mbWorkOrderController}" property="workOrderCurrent.applicant" />
But unfortunatelly I get a javax.servlet.ServletException with the message:
The class 'WorkOrderController' does not have the property 'workOrderCurrent.applicant'.
Does anybody have an advice?
The class 'WorkOrderController' does not have the property 'workOrderCurrent.applicant'.
Your <c:set> syntax is incorrect.
<c:set value="#{mbUserController.userLoggedIn.username}"
target="#{mbWorkOrderController}"
property="workOrderCurrent.applicant" />
You seem to be thinking that the part..
value="#{mbWorkOrderController.workOrderCurrent.applicant}"
..works under the covers as below:
WorkOrderCurrent workOrderCurrent = mbWorkOrderController.getWorkOrderCurrent();
workOrderCurrent.setApplicant(applicant);
mbWorkOrderController.setWorkOrderCurrent(workOrderCurrent);
This isn't true. It works under the covers as below:
mbWorkOrderController.getWorkOrderCurrent().setApplicant(applicant);
The correct <c:set> syntax is therefore as below:
<c:set value="#{mbUserController.userLoggedIn.username}"
target="#{mbWorkOrderController.workOrderCurrent}"
property="applicant" />
That said, all of this isn't the correct solution to the concrete problem you actually tried to solve. You should perform model prepopulating in the model itself. This can be achieved by using #ManagedProperty to reference another bean property and by using #PostConstruct to perform initialization based on it.
#ManagedBean(name = "mbWorkOrderController")
#SessionScoped
public class WorkOrderController {
#ManagedProperty("#{mbUserController.userLoggedIn}")
private User userLoggedIn;
#PostConstruct
public void init() {
workOrderCurrent.setApplicant(userLoggedIn.getUsername());
}
// ...
}
Perhaps you could explain the context a bit more, but here's another solution. If you're navigating from another page, you can pass some identifier of work WorkOrder in the URL, like this http://host:port/context/page.xhtml?workOrderId=1.
Then, you can set the identifier in the managed bean like this:
<h:html>
<f:viewParam name="workOrderId" value="#{mbWorkOrderController.id}"/>
</h:html>
You'll have to add a new property to your bean:
public class WorkOrderController {
private long id;
public long getId() { return id; }
public void setId(long id) { this.id = id; }
// ...
}
And then, after the property has been set by JSF, you can find the work order in a lifecycle event:
<h:html>
<f:viewParam name="workOrderId" value="#{mbWorkOrderController.id}"/>
<f:event type="preRenderView" listener="#{mbWorkOrderController.findWorkOrder()}"/>
</h:html>
public class WorkOrderController {
private long id;
public long getId() { return id; }
public void setId(long id) { this.id = id; }
public void findWorkOrder() {
this.workOrderCurrent = null /* some way of finding the work order */
}
// ...
}
This strategy has the advantage of letting you have bookmarkable URLs.

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

JSF2:How to bind string value to h:commandLink action

Hi i need to dyamically set h:commandLink action as a string value from bean side. Here explains my problem with code
MenuObject.java
public class MenuObject {
private String menuName;
private String menuAction;
public String getMenuName() {
return menuName;
}
public void setMenuName(String menuName) {
this.menuName = menuName;
}
public String getMenuAction() {
return menuAction;
}
public void setMenuAction(String menuAction) {
this.menuAction = menuAction;
}
}
MenuCreator.java
public class MenuCreator {
public List getMenu(){
List menuList = new ArrayList();
MenuObject menu1 = new MenuObject();
menu1.setMenuAction("accountController.beginSearch()");
menu1.setMenuName("Account");
menuList.add(menu1);
MenuObject menu2 = new MenuObject();
menu2.setMenuAction("companyController.beginSearch()");
menu2.setMenuName("Company");
menuList.add(menu1);
return menuList;
}
main.xhtml
<ui:repeat value="#{menuCreator.menu}" var="subMenu">
<li class="glyphicons cogwheels"><h:commandLink action="#{subMenu.menuAction}"><i></i><span><h:outputText value="#{subMenu.menuName}"/></span></h:commandLink></li>
</ui:repeat>
Here what i need is i need to dynamically change commandlink action value with respect to bean string value (here it was menuAction). But in this situation i got following exception
javax.el.MethodNotFoundException: /WEB-INF/layouts/main.xhtml #137,85 action="#{menuCreator.menu}": Method not found: com.util.MenuObject#30c96021.menuAction()
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:109)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:101)
at net.bull.javamelody.JsfActionListener.processAction(JsfActionListener.java:65)
You are trying to use the EL to return a value expression to be used as a method expression in a single expression. The JEE7 tutorial states:
9.3 Value and Method Expressions
The EL defines two kinds of expressions: value expressions and method expressions.
Value expressions can either yield a value or set a value. Method expressions reference
methods that can be invoked and can return a value.
You can achive this behaviour using javascript or use a library that offers you a dynamic menu component, like primefaces.
Maybe you can try something like Command Pattern. This is only an idea, I did not tested it.
In the xhtml:
<ui:repeat value="#{menuCreator.menu}" var="subMenu">
<li class="glyphicons cogwheels">
<h:commandLink action="#{invoker.callAction}" value="#{subMenu.menuName}">
<f:setPropertyActionListener target="#{invoker.action}" value="#{subMenu.action}" />
</h:commandLink>
</li>
</ui:repeat>
The command pattern:
/* The Command interface */
public interface Command {
String execute();
}
The menu item:
public class MenuObject {
private String menuName;
private Command action;
// Getters and setters...
}
The invoker:
#Named("invoker")
public class Invoker {
private Command action;
public String callAction(){
return action.execute();
}
}

Resources