Help

Built with Seam

You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.

There is a simple form to create autocomplete using Seam using EntityQuery.

Let's use the tag rich:suggestionbox. If you don't know this, visit RichFaces demo page . There is a good example about this tag.

<s:decorate id="userDecoration" 		template="layout/edit.xhtml">			<ui:define name="label">Name</ui:define>		<h:inputText id="user" value="#userList.instance.userName}">				</h:inputText>
<rich:suggestionbox id="suggestionBox" for="userName"		fetchValue="#{userList.instance.userName}"
	suggestionAction="#{userList.autoComplete}" 
	var="user" minChars="2">
	<h:column>						<h:outputText value="#user.userName}" />					</h:column>					</rich:suggestionbox>
</s:decorate>

Now, is necessary create the method autocomplete...

 
@Name("userList")
public class UserList extends EntityQuery {

...

public List<User> autoComplete(Object o) {
		String sql =  this.getEjbql() + " where lower(user.userName) like concat('%',lower('"+o.toString()+"'),'%')";
		return this.getEntityManager().createQuery(sql).getResultList();   
	}
 
5 comments:
 
30. Sep 2008, 10:19 America/New_York | Link

Please note that this example leaves you wide open to SQL injection

(and besides that the for parameter probably should be 'user' instead of 'userName' plus there are some typos)

 
07. Oct 2008, 17:16 America/New_York | Link
Bolke de Bruin wrote on Sep 30, 2008 10:19:
Please note that this example leaves you wide open to SQL injection (and besides that the for parameter probably should be 'user' instead of 'userName' plus there are some typos)

It's true! Thanks! We could to resolve this problem creating a parameter query. I'll change it.

 

Jony Santos

 
08. Dec 2008, 19:43 America/New_York | Link
oussema

hello we have problem in an Autocomplete with rich:suggestionbox, the fuction autoComplete in the DAO CursusHome is called from suggestionbox, Here is the code:

<h:inputText id="ecoleauto" value="#{cursusHome.instance.ecole}"/>
<h:graphicImage value="/img/arrow.png" onclick="#{rich:component('suggestionBox')}.callSuggestion(true)"/>                                                                                                                                 
<rich:suggestionbox id="suggestionBox" for="ecoleauto"
usingSuggestObjects="true"                                                                                                  
fetchValue="#{cursusHome.instance.ecole}"                                                               
suggestionAction="#{cursusHome.autocomplets}" var="varEcole">
<h:column>
<h:outputText value="#{varEcole.nomEcole}" />
</h:column>
</rich:suggestionbox>
 
and the autocomplete function :

public List<Ecole> autocomplets(Object o) {
String sql =  this.ecoleList.getEjbql() + " and lower(ecole.nomEcole) like concat('%',lower('"+o.toString()+"'),'%')";
List resultList = this.getEntityManager().createQuery(sql).getResultList();
return resultList;   
}


This code always gives  ahandling uncaught exception when we type a letter in the suggestiobox 

ERROR [ExceptionFilter] handling uncaught exception
javax.servlet.ServletException: L'ex?cution de la servlet a lanc? une exception
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:313)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at com.fitnetapplication.resources.pdf.RendererFilter.doFilter(RendererFilter.java:78)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
        at org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:38)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
        at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:85)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
        at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
        at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
        at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:177)
        at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:267)
        at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:380)
        at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:507)
        at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:60)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
        at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:58)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
        at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
        at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433)
        at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
        at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
        at java.lang.Thread.run(Thread.java:595)
Caused by: java.lang.OutOfMemoryError: Java heap space
19:19:46,718 ERROR [ExceptionFilter] exception root cause

Thanks

 
13. Jun 2009, 05:28 America/New_York | Link

Note that this example is similar to the example for <rich:suggestionBox> in Practical Richfaces book in that the suggest() (or in this case, autoComplete()) method is firing a JPQL query on every onkeyup event fired (i.e. every time the user enters a character in the inputText field, a DB hit occurs!)

I have implemented this solution and it works well so far:

//load all RecoveredEquipmentManagement entities into SMPC; assuming that siteId dropdown value is not populated so we can't add a filter to the JPQL query....
@Create
public void init(){
	allRecoveredEquipmentManagementList = entityManager.createQuery("select rem from RecoveredEquipmentManagement rem").getResultList();
        entityManager.clear();  //detach managed entities from persistence context cache
}

This implementation avoids any subsequent queries during the LRC.

refer to the following thread for more info: http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4237418#4237418

 

required reading: Scala and more Scala... www.artima.com

 
24. Oct 2011, 09:40 America/New_York | Link

Hi everybody i tried this example and it s works very well but i want to implement the methode suggestionAction in javaScript but i dont know how i must proceed can u help me

Thanks