简体   繁体   English

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

[英]Retrieving values from database on selection a dropdown value using struts2

I have a requirement where on selecting a value from a dropdown list, the corresponding value for that option is retrieved from database. 我有一个要求,在从下拉列表中选择一个值时,将从数据库中检索该选项的相应值。 I am using struts2 framework. 我正在使用struts2框架。

My jsp page is: 我的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>

I have written a javascript in the jsp page: 我在jsp页面中写了一个javascript:

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

Again in the struts.xml file: 再次在struts.xml文件中:

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

But my action method is not called in the javascript function.Since it is not called, i am not able to proceed any further. 但是我的动作方法没有在javascript函数中调用。由于没有被调用,所以我无法继续进行任何操作。 Also there is not any error in the console.Any help is highly appreciated. 控制台中也没有任何错误。我们非常感谢您的帮助。

Thanks 谢谢

You need to go for ajax ie ($.post in jquery ).Have a look on my code 您需要使用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>

in struts.xml 在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>

in Action class SearchServicePortlet 在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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM