根据从struts2应用程序的下拉列表中选择的值填充表 [英] Populating a table based on values chosen from a drop down in struts2 application

查看:55
本文介绍了根据从struts2应用程序的下拉列表中选择的值填充表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个包含以下代码的jsp页面中下拉菜单

I have a drop down in a jsp page having the following code

<s:select name="newQuestion.CertificationId"
    list="certificationList"
    listKey="certificationId"
    listValue="certificationName"
    headerKey=""
    headerValue="Select Certification"
    label="Certification Name"
    onchange="getQuestionsList(this.value)" />

现在,下拉列表的on change事件正在调用JS函数getQuestionsList(),该函数旨在获取所选ID的问题列表.

Now the on change event of the drop down is calling a JS function getQuestionsList() which intends to fetch the question list of the certification Id choosen.

在JS函数中,我将其提交给一个动作类,在该类中,我正在执行数据库调用以根据证书ID的值获取问题列表.

In the JS function I am submitting it to a action class where I am doing a DB call to fetch question list based on values of certification Id

function getQuestionDetails(value){
    var submiturl = "Action1.action?certId="+value;

$.ajax({
    url:submiturl,
    type: 'get',
    beforeSend: function(){
        $("#loading").show();
        alert("parsed");
    },  
    success: function(result){
        if(result!=''){

        } else {
            alert(result);
        }
    },
  });
}

现在在动作类中,我使用从数据库中获取的值设置questionList的值.questionList是动作类中具有getter和setter方法的实例变量.现在在struts.xml中,我将传递给同一个jsp下拉了

Now in the action class I am setting the value of questionList with values I fetch from database .questionList is a instance variable in action class with getter and setter methods.Now in struts.xml I am passing to the same jsp where I had the drop down

<action name="questionAction" class="questionInfo.QuestionManager"
            method="displaySelectedQuestions">
    <result name="loaded">/questionAdmin.jsp</result>
</action>

我遇到的问题是当我回到jsp时,我无法检索在迭代器中显示的问题列表

The problem I encounter is when I go back to the jsp I am not able to retrieve the question List which I display in an iterator

<table class="registrationDetailsTable">
    <tr class="tabledataheader">
        <td>Question Id</td>
        <td>Question Description</td>
    </tr>
    <s:iterator value="questionList">
        <tr class="tabledatarow">
            <td><s:property value="questionId" /></td>
            <td><s:property value="questionDesc" /></td>        
        </tr>
    </s:iterator>
</table>

请让我知道我出了什么问题,以便在我的jsp中填充问题列表

Please let me know where I am getting wrong so that I have the question List populated in my jsp

推荐答案

使用$.ajax(...)调用操作时,该操作通过转发到视图(questionAdmin.jsp)来创建响应,对吗?并且该响应应该在该Ajax调用的返回值处理程序中对您可用.

When you call your action with $.ajax(...), the action creates the response by forwarding to the view (questionAdmin.jsp), right? And this response should be available to you in that Ajax call's return value handler.

因此,当您拥有此功能时:

So when you have this:

success: function(result){
        if(result!=''){
            // nothing here currently!
        } else {
            alert(result);
        }
    }

您应该对生成的HTML进行处理.您可以仅执行alert(result)来开始查看要获取的内容,然后使用Javascript显示新的列表内容.我会这样做,以便您有一个包含表的div,如下所示:

You should do something with the resulting HTML. You could begin by doing just alert(result) to see what you are getting and then display the new list contents using Javascript. I would do it so that you have a div containing your table like this:

<div id="myTableWrapper"> <!-- I added this div -->
    <!-- This part should come from the action when called by Ajax -->
    <table class="registrationDetailsTable">
        <tr class="tabledataheader">
            <td>Question Id</td>
            <td>Question Description</td>
        </tr>
       <tr class="tabledatarow">
            <td>1</td>
            <td>My question #1</td>        
        </tr>
        <tr class="tabledatarow">
            <td>2</td>
            <td>My question #2</td>        
        </tr>
    </table>
    <!-- //This part should come from the action when called by Ajax -->
</div>

现在您可以执行以下操作:

So now you can do this:

success: function(result){
        if(result!=''){
            $('.myTableWrapper').html(result);
        } else {
            alert(result);
        }
    }

这篇关于根据从struts2应用程序的下拉列表中选择的值填充表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆