如何获得对同一GSP页面的响应

时间:2017-02-07 07:16:15

标签: jquery ajax grails

我有两个选择下拉框,一旦我选择了值并单击提交我需要从数据库中检索值并检查它们是否相同如果它们是相同的那么我需要在同一个gsp页面中显示一条消息“两个值都相同“。如果它们不相等,那么我应该在同一gsp页面的文本区域中打印检索到的值。所有这些都应该显示在同一个gsp页面中

This is my gsp page

<!DOCTYPE html>
<html>
<head>
<meta name="layout" content="main" />
<title>Render Domain</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(asd.value)
            alert(asd1.value)
            $.ajax({
                url:URL,
                data: {asd:asd.value,asd1:asd1.value},
                success: function(data){
                    //console.log(resp);
                    $("#asd").val(resp.asd);
                    $("#asd").val(resp.asd);
                }
            });
        });
    });
    </script>


</head>
<body>
    <g:form>
        <g:select name="asd" from="${eventsList}" value="1" noSelection="['':'-Choose the From Date-']"/>
        <g:select name="asd1" from="${eventsList}" value="1" noSelection="['':'-Choose the From Date-']"/>
        <%-- <g:actionSubmit value="Compare" action="compare" /> --%>
        <button class="testMe">Compare</button>
    </g:form>
</body>
</html>

This is my controller method
def compare(){
        println "compare method called"
        def values = params.asd
        def values1 = params.asd1
        println "first value"+ values
        println "second value"+values1
        println "form submitted successfully"
        if(values !=null && values1 !=null){
            render (view:"index")
        }
        else{
            println "values are same"
        }

    }

任何人都会帮我解决这个问题,因为我是grails的新手,并且知道如何做到这一点

1 个答案:

答案 0 :(得分:0)

我添加了一些最小变化的小例子。我认为这只是一项学习任务,因为这种架构不适合真正的解决方案。

ComareController:

class CompareController {

    def index() {
        def eventsList = ['event1', 'event2', 'event3']
        render view: 'index', model: [eventsList: eventsList]
    }

    def ajaxCompare() {
        //You can retrieve entries from DB here like Event.get(params.long('firstEventId'))
        //But it will be better to add EventService to interact with DB
        render status: 200, text: params.firstEvent == params.secondEvent ? "Equal" : "Different: ${params.firstEvent} != ${params.secondEvent}"
    }
}

比较/ index.gsp:

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
    <head>
        <title> Render Domain </title>
        <g:javascript plugin="jquery" library="jquery" src="jquery/jquery-1.11.1.js"/>
        <script>
            $(document).ready(function(){
                $('#doCompare').click(function(){
                    $.ajax({
                        url: "${createLink(controller:'compare', action:'ajaxCompare')}",
                        data: {firstEvent: $('#firstEvent').val(), secondEvent: $('#secondEvent').val()},
                        success: function(data){
                            $('#result').val(data)
                        }
                    });
                });
            });
        </script>
    </head>

    <body>
        <g:select name="firstEvent" from="${eventsList}" value="${eventsList.first()}" noSelection="['':'-Choose the From Date-']"/>
        <g:select name="secondEvent" from="${eventsList}" value="${eventsList.last()}" noSelection="['':'-Choose the From Date-']"/>
        <button id="doCompare" onclick="void(0);">Compare</button>
        <br>
        <textarea id="result"></textarea>
    </body>
</html>