• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

S2: autocompleter - how to populate a list based on user's input?

 
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my actioncompleter tag I'm trying to give the end user a list based on his/her input (the list is returned from a db)

JSP:


BUT where is the list?!?!?!

The method potentialList is invoked (and I get the user's input, so far so good), but how does the statement above (jsp) knows which list to use (what list to show the end-user)?!

And what should the potentialList method return?

THANKS for any pointers!
(Changed title from actioncompleter to autocompleter)
[ June 18, 2008: Message edited by: Merrill Higginson ]
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The potentialList method should return a list of Labels and Ids in JSON Format. For example, if you wanted the user to select states, the method would return:

For more information, see the ajax samples that come with the struts2-showcase-2.0.11.war file that comes with the download.
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Merrill,

mmm, still not sure I completely understand the JSON list.

say this is my invoked method:

public String potentialList()
{
System.out.println("user pressed. "+userKeys); //this works fine!
//here I'll check from the db the value of userKeys and return potential list

//now I need to return a JSON list ? how should I call it?

myList = new ArrayList();

myList.add("[\"Alabama\",\"AL\"],");
myList.add("[\"Alaska\",\"AK\"],");
myList.add("[\"American Samoa\",\"AS\"],");
myList.add("[\"Arizona\",\"AZ\"],");
myList.add("[\"Arkansas\",\"AR\"],");

return myList; //??
return "user-potential-list"; //??
}

Also, I checked struts2-showcase-2.0.11.war and the JSONList.action refers to a JSONList.js (this means that it was built in advance and it's not dynamic). Do I need to create a js file?

Not sure I completely understands how it works.
Thank you!
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in other words, what should be the

JSONList.js

so it will provide the list I just created (the dynamic list)
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the example, they tried to simplify the process by just providing the literal JSON content in a file called JSONList.js. That's obviously not what you want, since you plan to generate the list dynamically.

I'd recommend that you use the JSON Plugin. This allows you to create a response of type "JSON" that automatically serializes Java Objects into JSON strings. Also, below is a link explaining in greater detail the format of the JSON string that the s:autocomplete tag will accept.

http://struts.apache.org/2.0.11.1/docs/autocompleter.html
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
almost there...ok, got the idea, I'm using JSON and it works (integrated successfully). I understand that I need to 'convert' the list into JSON notation, so I'm using JSONArray

I did this:


XML:


(should anything get there?)

so I still can't see the result populated and I wonder what am I doing wrong. Thank you.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be honest, I've never used the Struts autocompleter tag, so I'm learning along with you. I did spend some time and got an example working, but I did it differently that you did.

I'll share my code with you, and hopefully you'll be able to get it working also. I just took the existing code from the struts2-showcase application and modified it so that rather than using the static JSONList.js file, it uses an Action class to generate the output.

The first thing I did was download the jsonplugin-0.30.jar file from this link and put it in my WEB-INF/lib folder.

Here's the modifications I made to the struts-ajax.xml file:



I then created the following Action class:



In the index.jsp, all I had to do was add dataFieldName="states" to each of the <s:autocompleter> tags.

Once I did this, the tags work exactly the way they did with the static list.
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


THANK YOU!!!

YES, the dataFieldName did it all

(* too bad the struts2-showcase doesn't provide such example with dynamic list; but maybe this thread will help others)

thank you so much!
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Merrill ,
would you please give us full example from A to Z and please make it simple as possible.. i wish frankly to use autocomplete but still failed to use this feature ..it will be nice from you if you go with us step by step in the simpliest way how to make ajax with autocomplete tag works..
thanks a lot
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "A to Z" example is in the struts2-showcase-2.0.11.war file that comes with the Struts 2 download. I'd suggest you deploy it, run it, and study the source code to get a good idea of how it works.

The one problem with the example, though, is that it gets its JSON content from a static source (a file named JSONList.js), which is not what you'd normally do.

Once you've got the static example working, just modify the source code according to my instructions above, and you'll change the static example into a more dynamic one.
 
hani Ibrahim
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot
this is what was missing:
<s:autocompleter name="state" theme="ajax" href="%{jsonList}" dataFieldName="states" cssStyle="width: 200px;" autoComplete="true" searchType="substring">
</s:autocompleter>

my mistake is remove href="%{jsonList}" and add dataFieldName="states"

this is the wrong

THAAAAANKS A LOT
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My json action works fine, my <s:autocompleter/> works fine, but tell me please, how can I submit key?
My Map consisits of key and value, value is displayed inside <s:autocompleter/> but I don't understand how can I send via form submit my hidden map key. Please, help.
 
hani Ibrahim
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also me dont know how to send the key to server side.
also if you put onChange on <s:autoComplete> to alert any thing it is not work would anyone tell us how
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just add a name attribute to the tag, and as with any other Struts 2 tag, it can be retrieved in the Action class by a corresponding property getter in the Action class when the form is submitted.
 
hani Ibrahim
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the name attribute will send the value what we asked about is key
we hope to deal with autocomplete like drop down list:

<select name="mydropdown">
<option value="1">Fresh Milk</option>
<option value="2">Old Cheese</option>
<option value="3">Hot Bread</option>
</select>

In case of autocompleter is you can send Fresh Milk not 1.
here the dilemma
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you add a keyName attribute, the key selected will be passed to the Action class with the name you specify.
 
hani Ibrahim
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot it works perfectly with keyname
the last problem is No javascript with it worked
i tried to write on it onchange="alert('hi');"
and wasnt work
any help please
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Merrill Higginson:
To be honest, I've never used the Struts autocompleter tag, so I'm learning along with you. I did spend some time and got an example working, but I did it differently that you did.

I'll share my code with you, and hopefully you'll be able to get it working also. I just took the existing code from the struts2-showcase application and modified it so that rather than using the static JSONList.js file, it uses an Action class to generate the output.

The first thing I did was download the jsonplugin-0.30.jar file from this link and put it in my WEB-INF/lib folder.

Here's the modifications I made to the struts-ajax.xml file:



I then created the following Action class:



In the index.jsp, all I had to do was add dataFieldName="states" to each of the <s:autocompleter> tags.

Once I did this, the tags work exactly the way they did with the static list.



Hi

i m new to Struts 2.

Can anyone please post/mail the whole expmle with struts.xml and java file and jsp file ?

with using


I'm trying to do exactly as in the example as well as stucked after lots of searching about it i have seen your post.

I have tried the example from your reply to the post but still its not working. even I tried the dojo tag-lib .

actually I want to fill the list of autocompleter from the database.....

I just wants the example using JSON List means href tag of autocompleter.
I tried lots of different methods to retrive the list and I can't get the list thats why I am asking for the whole code.

Ihave also tried the showcase code and change it according to your instructions ... but stll its not works....

please please please help me....

my mail ID is


Thank you in advance,

VIRAL
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am new to struts . Please help me in implementing autocompleter tag in textbox.

My java action is ::

public class HelloWorld extends ExampleSupport {

private Map<String,String> testingMap;
private List<String> testingMapKeys;

public String execute() throws Exception {
testingMap = new HashMap<String,String>();
testingMapKeys= new ArrayList<String>();

testingMap.put("TREE","DEL");
testingMap.put("HELLO","MUM");

testingMapKeys.add("TREDD");
testingMapKeys.add("TREND");

return SUCCESS;
}

public Map<String,String> getTestingMap() {
return testingMap;
}

public List<String> getTestingMapKeys() {

return testingMapKeys;
}


}


My Jsp is ::

...
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="sx" uri="/struts-dojo-tags" %>
...
<head>
<title><s:text name="HelloWorld.message"/></title>
<sx:head debug="true"/>
</head>

...
<body>
<s:url id="cityTest" action="HelloWorld" />
<sx:autocompleter name="origin" href="%{cityTest}" showDownArrow="false" autoComplete="false" dataFieldName="testingMap" />
...

My Struts .xml

<package name="example" namespace="/example" extends="json-default">
<action name="HelloWorld" class="example.HelloWorld">
<result type="json">/example/HelloWorld.jsp</result>
</action>
.....
</package>

Please tell me where i am wrong. I am unable to get back values of testingMap in Json format in my Jsp. At first i am unable to get to my Jsp page. Please help me with theme ="ajax"
reply
    Bookmark Topic Watch Topic
  • New Topic