开发者

Problem while trying to update content on a jsp page using ajax in struts (when onChange event is fired)

I am trying to update drop down lists when an onChange event is fired by change in selection of another drop down list.

I have 2 methods in mind:

1.) I tried using asynchronous calls using dojo, but in that case although I was able to call an action asynchronously but when the action is executed it does not update the contents of drop down lists.I guess I am missing some code which will refresh the contents of the drop downs.

Here are the codes:

The JSP page:

<%@taglib uri="/struts-tags" prefix="s" %>
<%@taglib uri="/struts-dojo-tags" prefix="sd" %>

<s:url id="scriptURL" action="getLists" /> 
<sd:a href="%{scriptURL}" listenTopics="getLists" formId="dayReport" showLoadingText="Working..."/>

<s:form action="viewDayReport" id="dayReport">
<s:select label="Customer " name="customer" headerKey="0" headerValue="Select" list="customerList" onchange="dojo.event.topic.publish('getLists');"/>
<s:select label="Contact "  name="contact"  headerKey="0" headerValue="Select" list="contactList"  onchange="dojo.event.topic.publish('getLists');"/>
<s:select label="Employee " name="employee" headerKey="0" headerValue="Select" list="employeeList" onchange="dojo.event.topic.publish('getLists');"/>
<s:select label="Stage "    name="stage"    headerKey="0" headerValue="Select" list="stageList"    onchange="dojo.event.topic.publish('getLists');"/>
<s:select label="Type "     name="type"     headerKey="0" headerValue="Select" list="typeList"     onchange="dojo.event.topic.publish('getLists');"/>
<sd:datetimepicker label="Date" name="date" displayFormat="dd/MM/yyyy"                             onchange="dojo.event.topic.publish('getLists');"/>
<s:submit value="View Report(s)"/>
</s:form>

The action:

The action sets all the 5 lists required in the <s:select> tags in the page.
The lists are getting generated properly, I have checked.

The struts config:

<struts>
<!-- Add your configuration elements here -->
<package name="Deutek.admin" extends="struts-default" >
    <result-types>
        <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
    </result-types>
    <action name="dayReportPage" class="admin.dayReportAction">
        <result type="tiles">dayReport</result>
    </action>

    //This is the action mapping which maps the action in use.(Notice that the above action is also forwarding to the same page, it is the action which first time displays the page.)

    <action name="getLists" class="admin.getLists">
        <result type="tiles">dayReport</result>
    </action>
</package>
</struts>

2.) The 2nd method I found by googling requires the following java scripting on the jsp page

function retrieveURL(url) 
{
if (window.XMLHttpRequest) 
{ 
// Non-IE browsers
            开发者_JAVA百科req = new XMLHttpRequest();       
            req.onreadystatechange = processStateChange;
            try {
                 req.open("GET", url, true);
            } catch (e) {
                 alert(e);
            }
            req.send(null);
        } else if (window.ActiveXObject) { // IE

             req = new ActiveXObject("Microsoft.XMLHTTP");
            if (req) {
                 req.onreadystatechange = processStateChange;
                 req.open("GET", url, true);
                 req.send();
             }
        }
  }

Here the "req.open" requires "url" as a parameter. Here I dont understand what should be the URL so that the action class I have mentioned in the first method gets called.

Please tell me what code am I missing in the first method for updating the drop down lists with the new lists which were set by the action class.

Also what should be the URL in the second method.

And which method is better in your opinion.

Please help asap.

Thanks!!


Best way is to download struts2-showcase application from struts2 official site and extract it in your tomcat. They have lots of example showing how various things working inside struts2 domain which includes your case like two interconnected combobox/dropdown

what you are looking can be found under ajax tab in showcase application

here is the link to download sample applications

Showcase application

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜