使用struts2在选择下拉值时从数据库中检索值

时间:2013-11-03 16:00:30

标签: java javascript struts2

我有一个要求,从下拉列表中选择一个值,从数据库中检索该选项的相应值。我正在使用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函数中调用。因为它没有被调用,我无法继续进行。此外,控制台中没有任何错误。非常感谢任何帮助。

由于

1 个答案:

答案 0 :(得分: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

}