【问题标题】:Display data in a GSP page text area在 GSP 页面文本区域中显示数据
【发布时间】:2017-02-07 12:26:16
【问题描述】:

我有一个带有两个选择下拉菜单的 gsp 页面。根据我选择的值并单击比较按钮,它会从数据库中检索值并进行操作。 (如比较)如果它们在同一个 gsp 页面中打印相同,则值相同。如果它们不相同,则该值必须与值并排显示在两个文本区域的索引 gsp 页面中。

this is my gsp page

   <!DOCTYPE html>
<html>
<head>
<meta name="layout" content="main" />
<title>Json Compare</title>

<g:javascript plugin="jquery" library="jquery" src="jquery/jquery-1.7.2.js"/>
    <script>
    $(document).ready(function(){
        $('.testMe').click(function(){
            var URL="${createLink(controller:'jsonComparison',action:'compare')}";
            alert(URL)
            alert(firstText.value)
            alert(secondText.value)
            $.ajax({
                url:URL,
                data: {firstText:firstText.value,secondText:secondText.value},
                success: function(data){
                    //console.log(resp);

                    $("#result").val(data).show()

                }
            });
        });
    });

    </script>


</head>
<body>
    <g:form>
        <div></div><label>From Time</label><g:select name="firstText" from="${eventsList}"  noSelection="['':'-Choose the From Date-']"/> 
        <label>To Time</label><g:select name="secondText" from="${eventsList}"  noSelection="['':'-Choose the To Date-']"/></div>
        <button class="testMe">Compare</button>
        <br>
        <textarea id="result" style="display: none"></textarea>
        <%-- <textarea id="result1" style="display:none"></textarea> <textarea id ="result1" style="display:none"></textarea> --%>
    </g:form>
</body>
</html>

这是我的控制器。根据在索引页面中选择的值并单击比较按钮,我正在调用一个调用控制器的 ajax 函数。在控制器中,我传递选定的值并从数据库中检查它们是否相同,并且根据响应我需要在 index.gsp 中显示消息

 class JsonComparisonController {

    def preparedStatementService


    def index() {
        //List eventsList = preparedStatementService.retrieveValuesFromDb()
        def eventsList = ['2017-10-11 04.94.34', '2016-09-11 04.94.44', '2017-10-12 04.94.89']
        render view: 'index', model: [eventsList: eventsList]
    }

    def compare() {
        println "The Compare is called"
        String firstParameter = params.firstText
        String secondParameter = params.secondText
        def returnValue
        println "The first value is: " + firstParameter + "The second value is: " + secondParameter
        if(firstParameter !=null && secondParameter !=null && firstParameter.length() > 0 && secondParameter.length() > 0){
            println "Both the values are not null"
            if(firstParameter.equals(secondParameter)){
                println "First and second values are equal"
                returnValue = "The Json are Equal and no MisMatch"
                render status: 200, text: returnValue
            }else{
                println "The values are not equal"
                String value1 = "The First Json values"
                String value2 = "The Second Json Values"
                render status: 200, model:[firstText:value1,secondText:value2]
            }

        }else{
            render status: 200, text: "Please select the Time"
        }

    }
}

如何在 ajax 函数中接收来自控制器的响应。并将结果显示在 index.gsp 页面中

【问题讨论】:

    标签: javascript jquery grails


    【解决方案1】:

    请学习并理解 gsp 和控制器中的修复 这是你的 gsp 修复:

    <!DOCTYPE html>
    <html>
    <head>
    <meta name="layout" content="main" />
    <title>Json Compare</title>
    <g:javascript plugin="jquery" library="jquery" src="jquery/jquery-1.7.2.js"/>
        <script>
        $(document).ready(function(){
            $('.testMe').click(function(){
                var URL="${createLink(controller:'jsonComparison',action:'compare')}";
                alert(URL)
                alert(firstText.value)
                alert(secondText.value)
                $.ajax({
                    url:URL,
                    data: {firstText:firstText.value,secondText:secondText.value},
                    success: function(data){
                      if (data.status===true) {
                         //notice .html since it is content of textArea
                         $('#result1').html(data.result)
                         //if this was <input type=text value="something"
                         //then result would be the value of the form field
                         //$('#result1').val(data.result)
                      } else { /// if (data===false ) {
                            $('#result1').html(data.value1).show()
                            $('#result2').html(data.value2).show()
                      } 
                    }
                });
            });
        });
        </script>
    </head>
    <body>
        <g:form>
            <div></div><label>From Time</label><g:select name="firstText" from="${eventsList}"  noSelection="['':'-Choose the From Date-']"/> 
            <label>To Time</label><g:select name="secondText" from="${eventsList}"  noSelection="['':'-Choose the To Date-']"/></div>
            <button class="testMe">Compare</button>
            <br>
            <textarea id="result" style="display: none"></textarea>
            <%-- <textarea id="result1" style="display:none"></textarea> <textarea id ="result1" style="display:none"></textarea> --%>
        </g:form>
    </body>
    </html>
    

    您的控制器已修复

    class JsonComparisonController {
    
        def preparedStatementService
    
    
        def index() {
            //List eventsList = preparedStatementService.retrieveValuesFromDb()
            def eventsList = ['2017-10-11 04.94.34', '2016-09-11 04.94.44', '2017-10-12 04.94.89']
            render view: 'index', model: [eventsList: eventsList]
        }
    
        def compare() {
            String firstParameter = params?.firstText   //make it nullsafe not going to throw an exception with ?
            String secondParameter = params?.secondText
            def returnValue
            //this is doing exactly what you were doing long winded
            def reply
            if(firstParameter && firstParameter) {
                if (firstParameter == firstParameter ){
                    reply=[status:true, result:"The Json are Equal and no MisMatch"]                
                }else{
                    reply = [status:false, value1:"The First Json values ${firstParameter}", value2:"The Second Json Values ${secondParameter}"]               
                }
            }else{
                reply = [status:false, value1:"issue here", value2:"issue here"]       
            }
            render status: 200, text:(reply as JSON).toString()
        }
    }
    

    而不是点击我的按钮,你可以有

    $('#secondText').on('change', function() {
    
     var val = $(this).val();
     var other = $('#firstText').val();
      if (val!='' && other!='') {
    var URL="${createLink(controller:'jsonComparison',action:'compare')}";
                    alert(URL)
                    alert(val)
                    alert(other)
                    $.ajax({
                        url:URL,
                        data: {firstText:val,secondText:other},
                        success: function(data){
                          if (data.status===true) {
                             //notice .html since it is content of textArea
                             $('#result1').html(data.result)
                             //if this was <input type=text value="something"
                             //then result would be the value of the form field
                             //$('#result1').val(data.result)
                          } else { /// if (data===false ) {
                                $('#result1').html(data.value1).show()
                                $('#result2').html(data.value2).show()
                          } 
                        }
                    });
      }
    })
    

    然后在第二个字段更改时自动触发

    【讨论】:

    • 我的问题是我需要在同一个 gsp 页面中显示结果。如果它们不相等,它们应该并排显示在两个单独的文本区域中
    • 一旦我从控制器得到值不相等的响应,那么文本区域应该是可见的,它应该打印两个值
    • 我试过你上面提到的代码,但现在我的文本区域没有显示
    • data.status===true not data,success===true 和以前一样,这是我的错误,但它清楚地表明你不太了解发生了什么
    • 我已经改变了,问题是我的文本区域没有显示
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    • 2010-11-17
    • 2020-03-21
    • 2022-01-11
    • 2014-07-29
    相关资源
    最近更新 更多