0

我有一个要求,在从下拉列表中选择一个值时,从数据库中检索该选项的相应值。我正在使用 struts2 框架。

我的jsp页面是:

<tr>
                <td>
                    Service Name
                </td>
                <td>
                    <select name="serviceName" id="serviceName" onchange="javascript:fnGetApplicationCount()" >
                        <s:iterator value="servicesList" var="rowstatus">
                            <option value="<s:property value="id" />">
                                <s:property value="name" />
                            </option>
                        </s:iterator>
                    </select>
                </td>
            </tr>


<tr>
             <td>
                <table>
                    <tr>
                        <td rowspan="2" colspan="1" align="center" width="11%">No. Of Applications</td>
                    </tr>
                    <tr>
                        <td align="left" width="11%">
                            <s:textfield name="noOfApplication" label="noOfApplication" id="noOfApplication" value="noOfApplication" />
                        </td>
                    </tr>
                </table>
              </td>
            </tr>

我在jsp页面中写了一个javascript:

<script type="text/javascript">
function fnGetApplicationCount()
    {
        document.formName.action="getPreviousMonthPending";
    }
</script>

再次在 struts.xml 文件中:

<action name="getPreviousMonthPending" class="com.stp.portal.view.SearchServicePortlet" method="getPreviousMonthPending">
            <result name="success">/WEB-INF/view/AddOfflineServices.jsp</result>            
        </action>

但是我的操作方法没有在 javascript 函数中调用。因为它没有被调用,所以我无法继续进行。控制台中也没有任何错误。非常感谢任何帮助。

谢谢

4

1 回答 1

0

您需要使用 ajax 即(jquery 中的 $.post)。看看我的代码

        <script type='javascript/text' src='http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'> 
</script>
       <script type='javascript/text' > 
    function fnGetApplicationCount()
    {
    var service_val=$("#serviceName option:selected").val();
    $.post('getPreviousMonthPending',{'service':service_val},function(data){
        //pass this data to 
           alert(data);
        });
    }
</script>

在 struts.xml 中

<action name="getPreviousMonthPending" class="com.stp.portal.view.SearchServicePortlet" method="getPreviousMonthPending">
            <result type='stream'>
<param name="contentType">text/html</param>
 <param name="inputName">inputStream</param>
</result>            
        </action>

在 Action 类 SearchServicePortlet

class SearchServicePortlet extends ActionSupport{
InputStream inputStream;
String service;
public String getPreviousMonthPending(){

//logic to retrieve from database using getService, let's suppose output string name is serviceValue


inputStream=new StringBufferInputStream(serviceValue);
return SUCCESS;
}

//setter and getter of inputStream,service

}
于 2013-11-03T21:20:36.920 回答