I am trying to search a database that I have developed using JSF as the front end technology. I am getting an error saying the following:
Unable to find method [searchContractors] with [0] parameters
Here is the code that I have used. Can anyone tell me if there is something obvious that I am doing wrong because I can't understand why I am getting this error. Thanks for the help.
JSF code
<!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">
<head>
<title>Search</title>
</head>
<body>
<h:form id="searchForm">
<H2>Search</H2>
<H4>Please select the county that you live in.
</H4>
<table>
<tr>
<td><h:outputLabel for="county">
<h:outputText id="countyLabel" value="County" />
</h:outputLabel></td>
<td><h:selectOneMenu id="countyName"
value="#{searchBean.countyId}">
<f:selectItems value="#{registerBean.counties}" var="county"
itemLabel="#{county.name}" itemValue="#{county.id}" />
</h:selectOneMenu></td>
</tr>
<tr>
<td><h:commandButton id="searchContractors"
action="#{searchBean.searchContractors(searchBean.countyId)}">
<h:outputText value="Search Contractors" />
</table>
</h:form>
</body>
</html>
Java code
#ManagedBean
#SessionScoped
public class SearchBean implements Serializable {
private static final long serialVersionUID = -2107387060867715013L;
private static final String PERSISTENCE_UNIT_NAME = "NeedABuilderUnit";
private static EntityManagerFactory factory;
private int countyId;
public List<BusinessAccount> searchContractors(int countyId) {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
List<BusinessAccount> contractorList = new ArrayList<BusinessAccount>();
em.getTransaction().begin();
Query myQuery = em.createQuery("SELECT u FROM BusinessAccount u WHERE u.county.id=:CountyId");
myQuery.setParameter("CountyId", countyId);
contractorList=myQuery.getResultList();
em.getTransaction().commit();
em.close();
return contractorList;
}
public int getCountyId() {
return countyId;
}
public void setCountyId(int countyId) {
this.countyId = countyId;
}
}
the following should work:
<ui:param name="countyId" value="#{searchBean.countyId}" />
...
<h:commandButton id="searchContractors"
action="#{searchBean.searchContractors(countyId)}">
Related
I have created a simple XHTML file and a corresponding Java Bean. Inside the Java Bean, I generate an ArrayList with objects of my custom class "FilePreview", which is defined in a different package (I imported it into the Bean, of course). In my XHTML-File I use ui-repeat to iterate over the list and display each element. I try to access the properties of my objects using get-Methods.
My XHTML-File
<?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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:b="http://bootsfaces.net/ui"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<link href="css/stylesheet.css" rel="stylesheet" type="text/css" />
</h:head>
<body>
<ui:composition template="./template.xhtml">
<ui:define name="title">
Dateien
</ui:define>
<ui:define name="content">
<b:container>
<ui:repeat value="#{FileBean.files}" var="files">
<b:panel title="#{files.headline}" look="primary">
<h:outputText value="#{files.description}" />
</b:panel>
<b:alert class= "file" severity="success"><strong>#{files.headline}</strong><br></br> <span>#{files.description}</span></b:alert>
</ui:repeat>
</b:container>
</ui:define>
</ui:composition>
</body>
</html>
My JavaBean:
import de.unibremen.st.gradelog.model.FilePreview;
import java.util.ArrayList;
#ManagedBean(name = "FileBean")
#SessionScoped
public class FileBean implements Serializable {
// private DBHandler handler;
private ArrayList<FilePreview> files;
public void create() {
files = new ArrayList();
files.add(new FilePreview("Hausordnung",
"Anbei findet Ihr die aktualisierte Hausordnung. Bitte gründlich lesen!",
null, null, null));
}
public ArrayList getFiles() {
/**
* DBHandler.getFiles(userID);
*/
System.out.println("Lese die Dateien aus.");
create();
return files;
}
}
And finally, the class FilePreview:
package de.unibremen.st.gradelog.model.FilePreview
public class FilePreview {
private String headline, description, authorID, fileID;
private File file;
public FilePreview(String headline, String description, String authorID,
String fileID, File file) {
this.headline = headline;
this.description = description;
this.authorID = authorID;
this.fileID = fileID;
this.file = file;
}
public String getHeadline() {
return headline;
}
//... more simple getters and setters
}
Everything seems to work just find, but when I run the application and access my new page, I get the following error:
Schwerwiegend: Error Rendering View[/files.xhtml]
javax.el.ELException: /files.xhtml #51,82 value="#{FileBean.files}": java.lang.NoClassDefFoundError: de/unibremen/st/gradelog/model/FilePreview
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114)
at com.sun.faces.facelets.component.UIRepeat.getValue(UIRepeat.java:279)
at com.sun.faces.facelets.component.UIRepeat.getDataModel(UIRepeat.java:255)
...
Caused by: javax.el.ELException: java.lang.NoClassDefFoundError: de/unibremen/st/gradelog/model/FilePreview
at javax.el.BeanELResolver.getValue(BeanELResolver.java:368)
Do I have to make the class known to JSF in some way? I'm working with an existing JSF project, and when I tried to do the same thing with a new project, everything worked just fine.
Anyone got an idea what could cause this?
You have to generate a faces-config.xml file where you declare that your managed bean is FileBean. You have to do something like this:
enter image description here
Hi I'm trying to access and call a method inside a managed bean in my JSF page. Here is the relevant part of the JSF page:
<ui:composition xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://xmlns.jcp.org/jsf/passthrough"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns="http://www.w3.org/1999/xhtml"
template="./template.xhtml">
<ui:define name="right">
<c:forEach items="#{tweetManager.getTweets}" var="item">
<h:link value="#{item.username}" />
Likes: <h:outputText value="#{item.likes}" />
<h:link id="" value="like" /> <br />
<textarea>
<h:outputText value="#{item.text}" />
</textarea>
</c:forEach>
</ui:define>
Here is the managed bean.
#ManagedBean
#RequestScoped
public class TweetManager {
private Tweet TweetEntity;
private List<Tweet> Tweets;
#EJB
private TweetService TweetService;
#PostConstruct
public void init(){
TweetEntity = new Tweet();
}
public void setTweet(Tweet tweetEntity){
this.TweetEntity = tweetEntity;
}
public Tweet getTweet(){
return this.TweetEntity;
}
public void Save(){
TweetService.create(TweetEntity);
}
public List<Tweet> getTweets(){
Query query = TweetService.getEntityManager().createNativeQuery("SELECT * FROM tweet");
Tweets = query.getResultList();
return Tweets;
}
}
I'm getting an error saying: ... .TweetManager' does not have the property 'getTweets'.
getTweet() is not a property, it is the accessor (or "getter") of the property.
The name of the property is tweets (without the get, first letter to lowercase). So:
<c:forEach items="#{tweetManager.tweets}" var="item">
Remember that boolean properties have "getters" like "is" (v.g. isRich())
And keep in mind my comment about using generics.
I am using the RichFaces 4 in my JSF application, but I`d like call a method from my bean when the user press the save button (for example: to save in a file text.txt)
Is there a way to call a java method when the user click on rich:editor save button?
Here is the code i`m using
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</h:head>
<h:body>
<h:form>
<rich:editor id="editor" toolbar="full" value="#{editorBean.value}"
style="margin-bottom: 1em" height="400" >
<a4j:ajax event="change" render="panel" status="panelUpdateStatus" />
<a4j:ajax event="dirty" render="panel" status="panelUpdateStatus">
<a4j:attachQueue requestDelay="1000" />
</a4j:ajax>
</rich:editor>
<rich:panel id="panel">
<f:facet name="header">
Output from Editor
<a4j:status name="panelUpdateStatus">
<f:facet name="start">
(Updating)
</f:facet>
</a4j:status>
</f:facet>
<h:outputText escape="false" value="#{editorBean.value}" />
</rich:panel>
</h:form>
EditorBean
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
#Named
#SessionScoped
public class EditorBean implements Serializable{
/**
*
*/
private static final long serialVersionUID = 5383915229820571701L;
private String value;
/**
* #return the value
*/
public String getValue() {
return value;
}
/**
* #param value the value to set
*/
public void setValue(String value) {
this.value = value;
}
public void save(){
System.out.println(" Saving ");
//Code to save
}
}
<form>
<editor></editor>
<a4j:commandButton value="Ok" type="submit" execute="#form" action="{editorBean.save()}" />
</form>
Bean:
public void save(){
System.out.println(" Saving: " + this.value);
//Code to save
}
This should submit the form, so the editor value gets saved to the bean.
When pressing the button, the editor content should show in your System.out.
I'm trying to load some data after a user logs in to my app. I need the username to load data specific to the user. The problem I'm having is that I have a SessionScoped backing bean that contains the code to log the user in, and then I have a ViewScoped page specific backing bean (which injects the SessionScoped bean) which is to load the data for the page.
Currently this is the code that I have and I get a null pointer when loading the data because the username doesn't exist when the loadData() method is called.
I'm having a hard time coming up with a solution to this problem as I'd like to NOT have to put the login dialog on every page and keep it in the template.xhtml file if possible.
template.xhtml
...
<!-- Login Dialog -->
<p:dialog id="loginDialog" header="Login" widgetVar="loginWidget" modal="true" visible="#{!accessBacking.hasAccess}" closable="false">
<h:form id="loginForm">
<p:messages id="loginFormMessages" severity="error" autoUpdate="true" showDetail="true" />
<h:panelGrid columns="2" cellspacing="10" width="300">
<p:outputLabel for="username" value="Username" />
<p:inputText id="username" value="#{accessBacking.username}" required="true" requiredMessage="Username is Required" />
<p:outputLabel for="password" value="Password" />
<p:password id="password" value="#{accessBacking.password}" required="true" requiredMessage="Password is Required" />
<h:panelGroup></h:panelGroup>
<h:panelGroup>
<p:commandButton value="Login" styleClass="ui-priority-primary" actionListener="#{accessBacking.checkViewAccess}" oncomplete="handleAuthenticationRequest(xhr, status, args)" update="loginFormMessages" />
</h:panelGroup>
</h:panelGrid>
</h:form>
</p:dialog>
...
userGroups.xhtml
<?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:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:body>
<f:metadata>
<f:event type="preRenderView" listener="#{userGroupBacking.setCurrentMenu}" />
</f:metadata>
<ui:composition template="/templates/template.xhtml">
<p:dataTable var="user" value="#{userGroupBacking.users}" editable="true" id="userTable">
...
AccessBacking.java
#ManagedBean(name="accessBacking")
#SessionScoped
public class AccessBacking {
private String username;
private String password;
public boolean checkViewAccess() {
Access access = new Access();
if(access.authenticate(username, password)) {
// user is logged in
}
}
}
UserGroupBacking
#ManagedBean(name="userGroupBacking")
#ViewScoped
public class UserGroupBacking {
#ManagedProperty(value="#{accessBacking}")
private AccessBacking accessBacking;
public void setAccessBacking(AccessBacking accessBacking) {
this.accessBacking = accessBacking;
}
#PostConstruct
public void init() {
loadData();
}
/**
* Loads the data for the page
*/
public void loadData() {
Dao dao = new Dao(ds);
users = dao.findAllUsers(accessBacking.getUsername(), accessBacking.getRoles()); // NULL POINTER BECAUSE ACCESSBACKING.GETUSERNAME() IS NULL SINCE THE USER HASN'T LOGGED IN YET.
}
}
I've figured out a good solution by using ui:param. Basically I set a ui:param with my backing bean value in my page specific xhtml and then reference that ui:param in my template.
Updated code from question with new solution:
template.xhtml
...
<!-- NOTE the 'myBacking' instead of the 'userGroupBacking' -->
<!-- Login Dialog -->
<p:dialog id="loginDialog" header="Login" widgetVar="loginWidget" modal="true" visible="#{!myBacking.hasAccess}" closable="false">
<h:form id="loginForm">
<p:messages id="loginFormMessages" severity="error" autoUpdate="true" showDetail="true" />
<h:panelGrid columns="2" cellspacing="10" width="300">
<p:outputLabel for="username" value="Username" />
<p:inputText id="username" value="#{accessBacking.username}" required="true" requiredMessage="Username is Required" />
<p:outputLabel for="password" value="Password" />
<p:password id="password" value="#{accessBacking.password}" required="true" requiredMessage="Password is Required" />
<h:panelGroup></h:panelGroup>
<h:panelGroup>
<p:commandButton value="Login" styleClass="ui-priority-primary" actionListener="#{myBacking.checkViewAccess}" oncomplete="handleAuthenticationRequest(xhr, status, args)" update="loginFormMessages" />
</h:panelGroup>
</h:panelGrid>
</h:form>
</p:dialog>
...
userGroups.xhtml
<?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:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:body>
<f:metadata>
<f:event type="preRenderView" listener="#{userGroupBacking.setCurrentMenu}" />
</f:metadata>
<!-- THIS IS PART OF THE SOLUTION -->
<ui:param name="myBacking" value="#{userGroupBacking}" />
<ui:composition template="/templates/template.xhtml">
<p:dataTable var="user" value="#{userGroupBacking.users}" editable="true" id="userTable">
...
AccessBacking.java
#ManagedBean(name="accessBacking")
#SessionScoped
public class AccessBacking {
private String username;
private String password;
public boolean checkViewAccess() {
Access access = new Access();
if(access.authenticate(username, password)) {
// user is logged in
}
}
}
UserGroupBacking
#ManagedBean(name="userGroupBacking")
#ViewScoped
public class UserGroupBacking {
#ManagedProperty(value="#{accessBacking}")
private AccessBacking accessBacking;
public void setAccessBacking(AccessBacking accessBacking) {
this.accessBacking = accessBacking;
}
#PostConstruct
public void init() {
loadData();
}
// CHECKS AGAINST ACCESSBACKING
public boolean isHasAccess() {
return accessBacking.isHasAccess();
}
// CHECKS AGAINST ACCESSBACKING
public boolean checkViewAccess() {
return accessBacking.checkViewAccess();
}
/**
* Loads the data for the page
*/
public void loadData() {
Dao dao = new Dao(ds);
users = dao.findAllUsers(accessBacking.getUsername(), accessBacking.getRoles());
}
}
So, I have a backing bean, Foo, and a template with a client, request and response. the clients are redundant, I want just one client.
Clients:
thufir#dur:~$
thufir#dur:~$ cat NetBeansProjects/NNTPjsf/web/foo/request.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
template="./template.xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<ui:define name="left">
<h:form>
<h:inputText size="2" maxlength="50" value="#{foo.bar}" />
<h:commandButton id="submit" value="submit" action="response" />
</h:form>
</ui:define>
<ui:define name="content">
<h:outputText value="#{foo.bar}"></h:outputText>
</ui:define>
</ui:composition>
thufir#dur:~$
thufir#dur:~$ cat NetBeansProjects/NNTPjsf/web/foo/response.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
template="./template.xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<ui:define name="left">
<h:form>
<h:inputText size="2" maxlength="50" value="#{foo.bar}" />
<h:commandButton id="submit" value="submit" action="response" />
</h:form>
</ui:define>
<ui:define name="content">
<h:outputText value="#{foo.bar}"></h:outputText>
</ui:define>
</ui:composition>
thufir#dur:~$
Which I think is ok, in and of itself.
Backing bean:
package guessNumber;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.http.HttpSession;
#Named
#SessionScoped
public class Foo implements Serializable {
private String bar = "bar";
private String response = "response";
public Foo() {
}
/**
* #return the bar
*/
public String getBar() {
return bar;
}
/**
* #param bar the bar to set
*/
public void setBar(String bar) {
this.bar = bar;
}
/**
* #return the response
*/
public String getResponse() {
FacesContext context = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
session.invalidate();
response = "hmm";
return response;
}
/**
* #param response the response to set
*/
public void setResponse(String response) {
this.response = response;
}
}
What I would like is just a single client, request_response or something. So that the text input form stays on the left and the result on the right. That's done with composition tags? Or, a third "general client" which has two sub-clients?
You need to change your commandButton on the request page to call an action method in the backing bean:
<h:commandButton id="submit" value="submit" action="#{foo.doAction}" />
In the action method set the response:
public String doAction() {
response = "hmm";
return "response";
}
The return value of the action method navigates to the page /response.xhtml.
But you don't need two pages. You can return null from the action method to reload the current (request) page:
public String doAction() {
response = "hmm";
return null;
}
Then the changed values for bar and response can be shown on the right side:
<ui:define name="content">
<h:outputText value="#{foo.bar}"></h:outputText>
<h:outputText value="#{foo.response}"></h:outputText>
</ui:define>