Target Unreachable, identifier 'regionMaster' resolved to null - jsf-2

I created a managedBean & .xhtml page.It works fine on jsf 2.1 but not working on jsf 2.2 .Showing error Target Unreachable, identifier 'regionMaster' resolved to null
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:prime="http://primefaces.org/ui">
<head>
</head>
<ui:composition>
<h:form id="regionMaster" name="regionMaster">
<div id="region">
<table align="center">
<tr>
<td><prime:panel header="Region">
<prime:messages autoUpdate="true" />
<prime:panelGrid style="margin-top:20px">
<prime:row>
<prime:column>
<h:outputLabel for="txtRegionID" value="RegionID" />
</prime:column>
<prime:column colspan="3">
<prime:inputText id="txtRegionID" value="#{regionMaster.regionId}"
label="Region ID" required="true" />
</prime:column>
<prime:column>
<!-- <h:commandButton id="openPopUp" value="..."
target="RegionList.xhtml"
onclick="window.open('RegionList.xhtml','childWindow','status=no,toolbar=no,location=no,menubar=no,resizable = no,width=1008,height=390,scrollbars,left=100,top=50');"
action="#{regionList.xhtml}" />-->
<!-- <prime:commandButton value="View" icon="ui-icon-extlink"
actionListener="#{regionMaster.regionList}" validateClient="false" immediate="true" />-->
<prime:dialog id="basicDialog" header="Basic Dialog"
widgetVar="dlg1">
<h:outputText value="Resistance to PrimeFaces is futile!" />
</prime:dialog>
<prime:commandButton id="basic" value="Basic"
onclick="PF('dlg1').show();" type="button" />
</prime:column>
</prime:row>
<prime:row>
<prime:column>
<h:outputLabel for="txtRegionName" value="RegionName" />
</prime:column>
<prime:column colspan="3">
<prime:inputText id="txtRegionName" value="#{regionMaster.regionName}"
label="Region Name" required="true" />
</prime:column>
</prime:row>
<prime:row>
<prime:column>
</prime:column>
<prime:column colspan="1">
<prime:commandButton action="#{regionMaster.save}"
value="save" />
</prime:column>
<prime:column colspan="1">
<prime:commandButton accesskey="R" alt="Click to Reset"
validateClient="false" immediate="true"
action="#{regionMaster.reset}" value="Reset" />
</prime:column>
<prime:column colspan="1">
<prime:commandButton accesskey="D"
onclick="if (!confirm('Are you sure you want to delete?')) return false"
alt="Click to Delete" actionListener="#{regionMaster.delete}"
validateClient="false" immediate="true" value="delete">
</prime:commandButton>
</prime:column>
</prime:row>
</prime:panelGrid>
</prime:panel></td>
</tr>
</table>
</div>
</h:form>
</ui:composition>
</html>
MyBean is
package com.sst.cms.web.beans;
import javax.faces.bean.RequestScoped;
import org.primefaces.context.RequestContext;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.inject.Named;
#ManagedBean(name="regionMaster",eager=false)
#SessionScoped
#Named("regionMaster")
public class RegionMasterBean implements Serializable {
//private static final FacesMessage RegionList = null;
public RegionMasterBean() {
}
private String regionId;
private String regionName;
private String createdBy;
private Date createdDate;
private String modifiedBy;
private Date modifiedDate;
private String entity;
private ArrayList<RegionMasterBean> regionList;
public String getRegionId() {
System.out.println("Region id======"+regionId);
return regionId;
}
public void setRegionId(String regionId) {
System.out.println("Region id======"+regionId);
this.regionId = regionId;
}
// other getter+setter
public String save(){
System.out.println("Bean is calleddddddddd");
return "SUCCESSFUL";
}
public void reset(){
this.regionId = "";
this.regionName = "";
}
public String delete(){
return "Sucess";
}
// public String setId(){
// System.out.println("Region iddddddddddddd");
//
// FacesContext fc = FacesContext.getCurrentInstance();
// this.regionId = getCountryParam(fc);
//
// return regionId;
// }
//
public ArrayList<RegionMasterBean> regionListLoad(){
regionList = new ArrayList<RegionMasterBean>();
RegionMasterBean dto = new RegionMasterBean();
dto.setRegionId("REG001");
dto.setRegionName("India");
RegionMasterBean dto1 = new RegionMasterBean();
dto1.setRegionId("REG002");
dto1.setRegionName("Afganisthan");
regionList.add(dto);
regionList.add(dto1);
return regionList;
}
public ArrayList<RegionMasterBean> getRegionList() {
return regionList;
}
public void setRegionList(ArrayList<RegionMasterBean> regionList) {
this.regionList = regionList;
}
public String saveSetting(){
System.out.println("this is calleddddddd");
this.regionId = "Roo1";
return regionId;
}
#PostConstruct
public void regionList() {
System.out.println("region list is calledddddddd");
RequestContext.getCurrentInstance().openDialog("RegionList");
System.out.println("region list is calledddddddd");
}
}
It works fine with the project where i have jsf 1.2 but when i add this to a new project with jsf 2.2 it shows the error

Related

Load value from database to <h:inputText> with <h:selectOneMenu> valueChangeEvent

I want to develop a JSF Page which lets user edit a Employee from database. I loaded list of all employees in h:selectOneMenu. and second thing i want that with valueChangeEvent employee detail should be loaded in corresponding h:inputText. but nothing happens with every valueChangeEvent. So, where is the problem in codes.
<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<div id="hdr">
<h1>Vinweb Services</h1>
<hr/>
</div>
;
<div id="mnu" style="height: 50px; width: auto; background: skyblue;">
<h:form>
<h:commandLink value="Home" action="index"/>
<h:outputText value=" "/>
<h:commandLink value="Create" action="create"/>
<h:outputText value=" "/>
<h:commandLink value="View" action="view"/>
<h:outputText value=" "/>
<h:commandLink value="Edit" action="edit"/>
</h:form>
</div>
<div id="cnt">
<h:form>
<h:panelGrid columns="2">
<h:outputLabel value="Select an Employee"/>
<h:selectOneMenu value="#{esb.empName}" onchange="submit()" valueChangeListener="#{esb.loadEmployeeDetail}">
<f:selectItems value="#{esb.employeeList}"/>
</h:selectOneMenu>
<h:outputLabel value="Employee Code"/>
<h:inputText value="#{esb.empCode}" required="true"/>
<h:outputLabel value="Employee Name"/>
<h:inputText value="#{esb.empName}" required="true"/>
<h:outputLabel value="Joining Date"/>
<h:inputText value="#{esb.joinDate}" required="true">
<f:convertDateTime pattern="MM/yy"/>
</h:inputText>
<h:outputLabel value="Salary"/>
<h:inputText value="#{esb.salary}" required="true"/>
<h:commandButton value="Update" action="#{esb.updateEmployeeDetail}"/>
</h:panelGrid>
</h:form>
</div>
</h:body>
</html>
Backing Bean:
package ems.bean;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import javax.annotation.Resource;
import javax.faces.event.ValueChangeEvent;
import javax.sql.DataSource;
#Named(value = "esb")
#SessionScoped
public class EmployeeServiceBean implements Serializable {
#Resource(name = "JPA4A")
private DataSource ds;
// Member Declaration
private String empCode;
private String empName;
private Date joinDate;
private long salary;
private ArrayList empList;
// Service Code Segment
// This method will publish all emloyees in table to selectOneMenu
public ArrayList getEmployeeList() throws SQLException{
empList = new ArrayList();
Connection con = ds.getConnection();
try{
Statement stmt = con.createStatement();
ResultSet rslt = stmt.executeQuery("SELECT empName FROM Employee");
while(rslt.next()){
empList.add(rslt.getString("empName"));
}
}
catch(SQLException se) {
throw new SQLException();
}
finally{
con.close();
}
return empList;
}
// This method should load selected empoyee's details in inputText fields
public void loadEmployeeDetail(ValueChangeEvent evt) throws SQLException{
String emp = evt.getNewValue().toString();
Connection con = ds.getConnection();
try{
Statement stmt = con.createStatement();
String qry = "SELECT * FROM Employee WHERE empName = '"+emp+"'";
ResultSet rslt = stmt.executeQuery(qry);
rslt.next();
this.empCode = rslt.getString("empCode");
this.empName = rslt.getString("empName");
this.joinDate = rslt.getDate("joinDate");
this.salary = rslt.getLong("salary");
}
catch(SQLException se) {
se.printStackTrace();
}
finally{
con.close();
}
}
public void updateEmployeeDetail() throws SQLException{
Connection con = ds.getConnection();
try{
Statement stmt = con.createStatement();
boolean rs = stmt.execute("UPDATE Employee SET empCode='"+empCode+"', empName='"+empName+"', joinDate='"+joinDate+"', salary="+salary);
}
catch(SQLException se) {
throw new SQLException();
}
finally{
con.close();
}
}
// Property Getter & Setter code segment
public String getEmpCode() {
return empCode;
}
public void setEmpCode(String empCode) {
this.empCode = empCode;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Date getJoinDate() {
return joinDate;
}
public void setJoinDate(Date joinDate) {
this.joinDate = joinDate;
}
public long getSalary() {
return salary;
}
public void setSalary(long salary) {
this.salary = salary;
}
}
The valueChangeListener is the wrong tool for the job.
You're here doing a onchange="submit()" which submits the entire form, including those required fields which in turn caused validation errors which in turn failed to reach your attention because you don't have any <h:message(s)> for them. If you have placed a <h:messages/> inside the form, or have paid more love and attention to server logs, you should have been notified of those required="true" validation errors.
You need <f:ajax listener>.
Replace
<h:selectOneMenu value="#{esb.empName}" onchange="submit()" valueChangeListener="#{esb.loadEmployeeDetail}">
<f:selectItems value="#{esb.employeeList}"/>
</h:selectOneMenu>
by
<h:selectOneMenu value="#{esb.empName}">
<f:selectItems value="#{esb.employeeList}"/>
<f:ajax listener="#{esb.loadEmployeeDetail}" render="#form" />
</h:selectOneMenu>
and replace
public void loadEmployeeDetail(ValueChangeEvent evt) throws SQLException{
String emp = evt.getNewValue().toString();
// ...
}
by
public void loadEmployeeDetail() throws SQLException{
String emp = empName; // You can also just use `empName` directly.
// ...
}
See also:
When to use valueChangeListener or f:ajax listener?

JSF 2 sending parameters via URL

I'm trying to send parameter via URL and it fails, because I somewhat can't assign value to the bean or I can't concat value with URL string. By the way when I write color value by hand everything works, for example:
<h:button outcome="viewParams?c=red"/>
This is the page code from which I'm trying to send parameter.
<h:form>
<h:selectOneMenu value="#{viewMngr.selectedColor}">
<f:selectItems value="#{viewMngr.colors}" var="c"
itemLabel="#{c}" itemValue="#{c}" />
<f:ajax listener="#{viewMngr.valueChanged}" />
</h:selectOneMenu>
<h:button outcome="viewParams?c=#{viewMngr.selectedColor}"/>
</h:form>
This is viewManager bean code (as far as I can see bean value is set, because I'm able to print it out)
#ManagedBean
public class ViewMngr {
private ArrayList<String> colors = new ArrayList<String>();
private String selectedColor;
public ViewMngr()
{
getColors().add("red");
getColors().add("green");
getColors().add("blue");
getColors().add("gray");
getColors().add("yellow");
getColors().add("orange");
}
public ArrayList<String> getColors() {
return colors;
}
public void setColors(ArrayList<String> colors) {
this.colors = colors;
}
public String getSelectedColor() {
return selectedColor;
}
public void setSelectedColor(String selectedColor) {
System.out.println("Selected color: " + selectedColor);
this.selectedColor = selectedColor;
}
public void valueChanged() {
System.out.println("Value changed!");
}
}
Now here is the code from second page which tries to catch that parameter:
<?xml version="1.0" encoding="UTF-8"?>
<!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:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<f:metadata>
<f:viewParam name="c" value="#{color.color}"/>
</f:metadata>
<h:body style="background-color:#{color.color}">
<fieldset>
<h:form>
<h:outputText value="Some text!"/>
</h:form>
</fieldset>
</h:body>
</html>
And here is color bean:
#ManagedBean
public class Color {
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
if(color != null)
System.out.println(color);
this.color = color;
}
}
Being your destination page viewParams.xtml:
<h:button outcome="viewParams">
<f:param name="c" value="#{viewMngr.selectedColor}" />
</h:button>
Takes the view id and appends the params to the url.
It turned out that button needed to be rerendered because c value didn't get set. The tricky part was that viewMngr.selectedColor was set but to set parameter c, button needed to be rerendered. Here is the edited code:
<h:body>
<fieldset>
<h:form prependId="false">
<h:selectOneMenu value="#{viewMngr.selectedColor}">
<f:selectItems value="#{viewMngr.colors}" var="c"
itemLabel="#{c}" itemValue="#{c}" />
<f:ajax listener="#{viewMngr.valueChanged}" render="buttonId"/>
</h:selectOneMenu>
<h:button id="buttonId" outcome="viewParams?c=#{viewMngr.selectedColor}"/>
</h:form>
</fieldset>
</h:body>

How to create JSF Table with links after post

I like to display a form where the end-user can enter details and the post will result in the table which contain a filtered list based on the user input. This table should have link for each row to select that specific row and return a detailed view.
accounts-list.xhtml:
<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"
xmlns:utils="http://java.sun.com/jsf/composite/snippets" template="/WEB-INF/templates/default.xhtml">
<ui:define name="title"></ui:define>
<ui:define name="content">
<h:form id="search">
<fieldset>
<legend>Search for account</legend>
<div class="account_list_section">
<span class="account_section_title"><h:outputLabel for="id" value="Account ID" /></span>
<span class="account_list_item">
<h:inputText id="id" value="#{accountsCrudBean.searchId}" styleClass="field_inputtext_account" />
</span>
</div>
<div class="account_list_section">
<span class="account_section_title"><h:outputLabel for="name" value="Account Name" /></span>
<span class="account_list_item">
<h:inputText id="name" value="#{accountsCrudBean.searchName}" styleClass="field_inputtext_account" />
</span>
</div>
<h:commandButton id="search" value="Display" action="#{accountsCrudBean.actionSearch}" />
</fieldset>
</h:form>
<h:form id="view" rendered="#{accountsCrudBean.dataItemId.value != null}">
<h:dataTable var="dataItem" binding="#{accountsCrudBean.dataTable}" value="#{accountsCrudBean.list}" id="accountListTable"
columnClasses="accountsCrudBeanTable_row_date, transactionsListTable_row_amount, transactionsListTable_row_balance, transactionsListTable_row_description" styleClass="transactionsListTable">
<h:column headerClass="transactionsListTable_header_amount">
<f:facet name="header">Id</f:facet>
<h:commandLink value="#{dataItem.id}" action="#{accountsCrudBean.editDataItem}" />
</h:column>
<h:column headerClass="transactionsListTable_header_balance">
<f:facet name="header">Name</f:facet>#{dataItem.name}</h:column>
<h:column headerClass="transactionsListTable_header_description">
<f:facet name="header">Total Balance</f:facet>#{dataItem.totalBalance}</h:column>
</h:dataTable>
<h:inputHidden binding="#{accountsCrudBean.dataItemId}" />
</h:form>
</ui:define>
</ui:composition>
accounts-edit.xhtml:
<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:utils="http://java.sun.com/jsf/composite/snippets"
template="/WEB-INF/templates/default.xhtml">
<ui:define name="title"></ui:define>
<ui:define name="content">
<h:form id="edit" rendered="#{accountsCrudBean.dataItemId.value != null}">
<fieldset>
<legend>View account 1</legend>
<div class="account_list_section">
<span class="account_section_title"><h:outputLabel for="id" value="Account ID" /></span>
<span class="account_list_item">
#{accountsCrudBean.dataItemId.value}
</span>
</div>
<div class="account_list_section">
<span class="account_section_title"><h:outputLabel for="name" value="Account Name" /></span>
<span class="account_list_item">
<h:inputText readonly="true" id="name" value="#{accountsCrudBean.dataItem.name}" styleClass="field_inputtext_account" />
</span>
</div>
<div class="account_list_section">
<span class="account_section_title"><h:outputLabel for="accountOwners" value="Owners" /></span>
<span class="account_list_item">
<h:selectOneListbox readonly="true" id="accountOwners" value="#{accountsCrudBean.selectedOwner}">
<f:selectItems value="#{accountsCrudBean.accountOwners}" />
</h:selectOneListbox>
</span>
</div>
<h:inputHidden binding="#{accountsCrudBean.dataItemId}" />
<h:commandButton id="edit" value="Save" action="#{accountsCrudBean.actionSearch}" />
</fieldset>
</h:form>
<h:outputText value="No item selected." rendered="#{accountsCrudBean.dataItemId.value == null}" />
</ui:define>
</ui:composition>
AccountsCrudBean.java:
#ManagedBean
#SessionScoped
public class AccountsCrudBean extends JsfManagedBeanBase {
private static final long serialVersionUID = 1L;
#EJB
AccountsManagerLocal accountsManager;
private Long searchId;
private String searchName;
private String selectedOwner;
private Account dataItem;
private HtmlDataTable dataTable;
private HtmlInputHidden dataItemId = new HtmlInputHidden();
private List<Account> accountList = null;
public AccountsCrudBean() {
}
public Long getSearchId() {
return searchId;
}
public void setSearchId(Long searchId) {
this.searchId = searchId;
}
public String getSearchName() {
return searchName;
}
public void setSearchName(String searchName) {
this.searchName = searchName;
}
public HtmlInputHidden getDataItemId() {
return dataItemId;
}
public Account getDataItem() {
return dataItem;
}
public void setDataItem(Account dataItem) {
this.dataItem = dataItem;
}
public HtmlDataTable getDataTable() {
return dataTable;
}
public void setDataTable(HtmlDataTable dataTable) {
this.dataTable = dataTable;
}
public void setDataItemId(HtmlInputHidden dataItemId) {
this.dataItemId = dataItemId;
}
public String getSelectedOwner() {
return selectedOwner;
}
public void setSelectedOwner(String selectedOwner) {
this.selectedOwner = selectedOwner;
}
public List<Account> getList() {
if (accountList == null) {
actionLoad();
}
return accountList;
}
private void actionLoad() {
if (searchId != null) {
Account account = accountsManager.getAccount(searchId);
accountList = Arrays.asList(account);
} else if (searchName != null) {
accountList = accountsManager.findAccountByName(searchName);
} else if (dataItemId != null && dataItemId.getValue() != null) {
Account account = accountsManager.getAccount(Long
.valueOf(dataItemId.getValue().toString()));
accountList = Arrays.asList(account);
} else
accountList = new ArrayList<Account>();
};
public void actionSearch() {
dataItem = (Account) dataTable.getRowData();
dataItemId.setValue(dataItem.getId());
actionLoad();
}
public Map<String, User> getAccountOwners() {
List<User> owners = accountsManager.getUsers(dataItem);
LinkedHashMap<String, User> map = new LinkedHashMap<String, User>();
for (User user : owners) {
map.put(user.getFirstName() + " " + user.getLastName(), user);
}
return map;
}
public String editDataItem() {
dataItem = (Account) dataTable.getRowData();
dataItemId.setValue(dataItem.getId());
return "accounts-edit";
}
}
This works on JBoss 7.1 but on 6 it's failed with:
javax.faces.el.EvaluationException: javax.el.ELException: /s/accounts-list.xhtml at line 22 and column 94 action="#{accountsCrudBean.actionSearch}": java.lang.IllegalArgumentException: row is unavailable
at javax.faces.component._MethodExpressionToMethodBinding.invoke(_MethodExpressionToMethodBinding.java:96)
at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:100)
at javax.faces.component.UICommand.broadcast(UICommand.java:120)
at javax.faces.component.UIViewRoot._broadcastAll(UIViewRoot.java:889)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:238)
at javax.faces.component.UIViewRoot._process(UIViewRoot.java:1201)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:627)
at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:34)
at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:171)
at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:189)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:324)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242)
at org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:67)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242)
at com.demo.server.ejb.security.AuthenticationFilter.doFilter(AuthenticationFilter.java:35)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181)
at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285)
at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:159)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951)
at java.lang.Thread.run(Thread.java:722)
Caused by: javax.el.ELException: /s/accounts-list.xhtml at line 22 and column 94 action="#{accountsCrudBean.actionSearch}": java.lang.IllegalArgumentException: row is unavailable
at org.apache.myfaces.view.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:95)
at javax.faces.component._MethodExpressionToMethodBinding.invoke(_MethodExpressionToMethodBinding.java:88)
... 35 more
Caused by: java.lang.IllegalArgumentException: row is unavailable
at javax.faces.model.ListDataModel.getRowData(ListDataModel.java:69)
at javax.faces.component.UIData.getRowData(UIData.java:462)
at com.demo.server.web.AccountsCrudBean.actionSearch(AccountsCrudBean.java:130)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.el.parser.AstValue.invoke(AstValue.java:196)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:43)
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:56)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:43)
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:56)
at org.apache.myfaces.view.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:83)
... 36 more
This is based on http://balusc.blogspot.com/2006/06/using-datatables.html#AddBackingBeanActionToEveryRow
On the post the author use #RequestScoped bean but if I try to change the bean to be #RequestScoped the filter does not work, and i don't get the table with the links.
Thanks.
Your bean is in the session scope. You don't need the <h:inputHidden binding="..."> at all. Remove it and all related code. Your backing bean action methods should look like this:
public void actionSearch() {
actionLoad();
}
public String editDataItem() {
dataItem = (Account) dataTable.getRowData();
return "accounts-edit";
}
Also replace #{accountsCrudBean.dataItemId} in your view by #{accountsCrudBean.dataItem.id}.
Unrelated to the concrete problem, please note that the article is nearly 6 years old and based on JSF 1.x. These days, with JSF 2.x and EL 2.2, there are other and easier ways to get the selected row. My favourite is
<h:commandLink value="#{dataItem.id}" action="#{accountsCrudBean.editDataItem(dataItem)}" />
with
public String editDataItem(Account dataItem) {
this.dataItem = dataItem;
return "accounts-edit";
}

inputText validator doesn't display error message

I have a simple form that get code then display his libelle, I added a validator bean that check if the code exist.
My problem is I can't display the error message whith when the code doesn't exist.
Here is the code:
test.xhtml
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head><title>Test</title>
</h:head>
<body class="bodyMain">
<h:form>
<h:panelGrid columns="3">
<h:outputText value="Code: " />
<h:inputText id="code" value="#{myBean.code}"
validator="#{myBean.validateCode}">
<f:ajax execute="#this" render="libelle" listener="#{myBean.setLibelle()}"/>
</h:inputText>
<h:message for="code" style="color:red"/>
</h:panelGrid>
<h:panelGrid id="libelle" columns="2">
<h:outputText value="Libelle: " />
<h:outputText value="#{myBean.libelle}" />
</h:panelGrid>
</h:form>
</body>
</html>
MyBean.java
#ManagedBean
#ViewScoped
public class MyBean implements java.io.Serializable{
private static final long serialVersionUID = 1L;
private String code="";
private String libelle="";
public String getCode() {
return this.code;
}
public void setCode(String code) {
this.code=code;
}
public String getLibelle() {
return this.libelle;
}
public void setLibelle(String libelle) {
this.libelle=libelle;
}
public void setLibelle() {
if (code.compareTo("1")==0)
libelle="One";
else
libelle="";
}
public void validateCode(FacesContext context, UIComponent toValidate, Object value) throws ValidatorException {
String code = (String)value;
if (code.compareTo("1") != 0) {
FacesMessage message = new FacesMessage("Code doesn't exist");
throw new ValidatorException(message);
}
}
}
Thank you for your help to resolve this problem
You're not updating the <h:message> component by the <f:ajax>. You need to give the <h:message> an id and include it in the <f:ajax render>.
<h:inputText id="code" value="#{myBean.code}" validator="#{myBean.validateCode}">
<f:ajax execute="#this" render="libelle codeMessage" listener="#{myBean.setLibelle()}"/>
</h:inputText>
<h:message id="codeMessage" for="code" style="color:red"/>
Unrelated to the concrete problem, don't initialize properties to empty strings. Let them by default null. Also, comparing objects should be done with equals() method, not with compareTo(). Finally, using a dropdown list with all available values instead of an input field would be more user friendly.

How to pass attributes to a composite-component

I am having trouble in using a JSF composite-component in the right way. I put some components together and everything was working. Then I just extracted the code to a composite-component, and passed the corresponding attributes and suddenly I am getting conversation exceptions.
<composite:interface>
<composite:attribute name="selectedIDs" type="java.util.Collection" required="true"/>
<composite:attribute name="selectItems" type="java.util.List" required="true" />
<composite:attribute name="addAction" required="true"/>
<composite:attribute name="deleteAction" required="true"/>
<composite:attribute name="deleteButtonDisabled" />
<composite:attribute name="ajaxListener" method-signature="void listener(javax.faces.event.AjaxBehaviorEvent)"/>
</composite:interface>
<composite:implementation>
<div class="myClass">
<h:outputStylesheet library="views" name="selectManyControlPanel.css" target="head" />
<h:form>
<h:selectManyListbox value="#{cc.attrs.selectedIDs}">
<f:selectItems value="#{cc.attrs.selectItems}" />
<f:ajax render="delete"
listener="#{cc.attrs.ajaxListener}" />
</h:selectManyListbox>
<br />
<h:commandButton id="delete" value="Delete"
disabled="#{cc.attrs.deleteButtonDisabled}"
action="#{cc.attrs.deleteAction}" />
<h:commandButton id="add" value="Add" action="#{cc.attrs.addAction}"/>
</h:form>
</div>
</composite:implementation>
Here is the page where I am using the newly created component
<view:selectManyControlPanel
selectedIDs="#{bean.selectedIds}"
selectItems="#{bean.listOfSelectItems}"
addAction="#{bean.addNew}"
deleteAction="#{bean.deleteSelection}"
ajaxListener="#{bean.selectionChanged}"
deleteButtonDisabled="#{bean.deleteButtonDisabled}" />
Bean (some methods skipped an parts renamed)
package views;
#SuppressWarnings("serial")
#Named
#RequestScoped
public class Bean implements Serializable, IOverviewView {
#Inject
Presenter presenter;
private boolean deleteButtonDisabled;
private List<SelectItem> listOfSelectItems;
private Set<Long> selectedIds;
public Bean(){
deleteButtonDisabled = true;
listOfSelectItems = new ArrayList<SelectItem>(10);
}
public void selectionChanged(AjaxBehaviorEvent event){
if(selectedIds.isEmpty())
deleteButtonDisabled = true;
else
deleteButtonDisabled = false;
}
public void deleteBikes(){
presenter.delete(selectedIds);
}
public void addNew(){
presenter.addNew();
}
public List<SelectItem> getListOfSelectItems() {
return listOfSelectItems;
}
public Set<Long> getSelectedIds() {
return selectedIds;
}
#PostConstruct
public void init(){
System.out.println("INITIALIZING BEAN: " + this.getClass().getName());
this.presenter.setView(this);
this.presenter.init();
}
public boolean isDeleteButtonDisabled() {
return deleteButtonDisabled;
}
#Override
public void setDeleteButtonEnabled(boolean isEnabled) {
deleteButtonDisabled = !isEnabled;
}
public void setListOfSelectItems(List<SelectItem> list) {
this.listOfSelectItems = list;
}
public void setSelectedIds(Set<Long> selectedIds) {
this.selectedIds = selectedIds;
}
#Override
public void updateBikesList(Set<ViewObject> objectsToDisplay) {
updateList(objectsToDisplay);
}
private void updateList(Set<ViewObject> objectsToDisplay){
listOfSelectItems.clear();
for (ViewObject vO : objectsToDisplay) {
final String label = vO.getManufacturer() + ", " + vO.getModel() + " (" + vO.getYear() + ")";
listOfSelectItems.add(new SelectItem(vO.getId(), label));
}
}
....
}
Exception
javax.el.ELException: /resources/views/selectManyControlPanel.xhtml #25,56 value="#{cc.attrs.selectedIDs}": /index.xhtml #21,70 selectedIDs="#{bean.selectedIds}": Cannot convert [Ljava.lang.String;#1e92093 of type class [Ljava.lang.String; to interface java.util.Set
The only thing that changed is that I am using the composition instead of the plain code. The EL-expressions are still the same. Can someone enlighten me please? I expected that the parameters are just passed through but obviously not...
This is related to Mojarra issue 2047. It's scheduled to be fixed in the upcoming 2.2.
The issue ticket also proposes the following workaround:
<view:selectManyControlPanel
selectedIDsBean="#{bean}"
selectedIDsProperty="selectedIds"
with in CC interface
<composite:attribute name="selectedIDsBean" required="true"/>
<composite:attribute name="selectedIDsProperty" required="true"/>
and in CC implementation
<h:selectManyListbox value="#{cc.attrs.selectedIDsBean[cc.attrs.selectedIDsProperty]}">

Resources