How to send value recently selected in the url as a param?
I'm working on a struts2 (2.2.3) web app, using jquery plugin (3.0.0) for ajax.
I have a jsp in which I have 2 sj:autocompleter (the first one for providers and the second one for products). I need to filter the products by the provider selected. In this particular application is a must to have an autocompleter for the second collection.
What I want is to send in the url the value selected in the first autocompleter as a parameter so that I'm able to use that selection for filtering the second autocompleter.
Part of my jsp code:
<s:url id="url_products" action="myAction" method="showProducts">
<s:param name="providerSelected" value="%{purchase.provider.id}">&l开发者_Python百科t;/s:param>
</s:url>
<sj:autocompleter
id="purchaseProviderId"
name="purchaseProvider"
value="%{purchaseProvider}"
onCompleteTopics="providerChanged"
cssClass="product"
href="%{url_providers}"
/>
<sj:autocompleter
id="productId"
name="productDescription"
value="%{productDescription}"
listenTopics="providerChanged"
href="%{url_products}"
/>
The problem I'm having is that "%{purchase.provider.id}" is something selected exactly before sending the url_products and when I send the value through the url the value is not updated from the latest selection done. What I think is that this value is part of the session and since the new selection done for the provider has not been submitted yet to the action, "%{purchase.provider.id}" is just pointing to the value the session is aware of.
Question: How can I send the value recently selected in "purchaseProviderId" autocompleter through the url as a param?
Thanks in advance!
See example for using double select at Struts2-jQuery documentation.
Notice that the second select has formIds="formSelectReload"
. This tells the action/url which is called to fill the second select that it depends on values in the formSelectReload
.
In your case, you might have something like:
<s:form id='myForm'>
<sj:autocompleter
id="purchaseProviderId"
name="purchaseProvider"
value="%{purchaseProvider}"
onCompleteTopics="providerChanged"
cssClass="product"
href="%{url_providers}"
/>
<sj:autocompleter
id="productId"
name="productDescription"
formIds="myForm" <!-- This will send form values to action including selected purchaseProviderId -->
value="%{productDescription}"
listenTopics="providerChanged"
href="%{url_products}"
/>
</s:form>
Add below mention code in you jsp page.
<sj:autocompleter
id="purchaseProviderId" name="purchaseProvider"
onSelectTopics="providerChanged"
list="productMap"
//Fill this map with the key value pair, and also make the getter
//and setter in order to have the access of the variable in jsp page
cssClass="product"/>
<s:url id="provider" action="getProvider"/>
<sj:autocompleter
id="productId" name="productDescription"
listenTopics="providerChanged"
listKey="key" listValue="value"
href="%{provider}" />
<script type="text/javascript">
var key;
$.subscribe('providerChanged', function(event, data)
{
var ui = event.originalEvent.ui;
key = ui.item.key;
//clear the children autocompleters
jQuery('#productId').val("");
jQuery('#productId_widget').val("");
var options_branchCity_widget = {};
options_productId_widget.hiddenid = "productId";
options_productId_widget.delay = 50;
options_productId_widget.minimum = 3;
options_productId_widget.selectBox = false;
options_productId_widget.forceValidOption = true;
options_productId_widget.jqueryaction = "autocompleter";
options_productId_widget.id = "productId_widget";
options_productId_widget.name = "productDescription_widget";
options_productId_widget.href = "getProvider.action"+"?providerSelected="+key;
options_productId_widget.listentopics = "providerChanged";
jQuery.struts2_jquery_ui.bind(jQuery('#productId_widget'),options_productId_widget);
});
</script>
Add the below code in struts.xml file to register the respected action that will fetch the data for autocompleter.
<action name="getProvider" class="com.sample" method="getProvider">
<result type="json">
<param name="root">providerList</param>
</result>
</action>
Create the DTO class as mention below.
public class autocompleteDTO
{
private String key;
private String value;
public autocompleteDTO(String key, String value)
{
this.key = key;
this.value = value;
}
public String getKey()
{
return key;
}
public void setKey(String key)
{
this.key = key;
}
public String getValue()
{
return value;
}
public void setValue(String value)
{
this.value = value;
}
}
Add the below mention code in you action class to work the autocompleter.
public String getProvider()
{
String userTypeCharacter = request.getParameter("term");
String providerSelected = request.getParameter("providerSelected");
List providerListDB = //fetch list from the database with respect to user typed character
//and providerSelected.
//Create the array list of the DTO class
providerList= new ArrayList<autocompleteDTO>();
//providerList will be the member variable of the action along with the getter and setter,
//for data display in the jsp page
//Iterate the list and add the data to another list
for (Object provider: providerListDB )
{
Provider providerData = (Provider) provider;
String providerCode = providerData .getProviderCode();
String providerName = providerData .getProviderName();
providerList.add(new autocompleteDTO(providerCode , providerName ));
}
}
return SUCCESS;
}
This code will work on struts 2 and struts 2 jquery plugin 3.6.1.
If you any help then please let me know. -Regards
精彩评论