【问题标题】:Retrieving values from database on selection a dropdown value using struts2使用struts2在选择下拉值时从数据库中检索值
【发布时间】:2013-11-03 16:00:29
【问题描述】:

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

谢谢

【问题讨论】:

  • 你需要去 ajax 即 ($.post in jquery ) 即

标签: java javascript struts2


【解决方案1】:

您需要使用 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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-22
    • 2018-04-30
    • 2014-01-18
    • 1970-01-01
    • 2017-05-26
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多