flash.warning仅在页面刷新后显示

时间:2012-09-29 12:50:16

标签: jquery grails gsp

嗨我有一个gsp,它将有jq网格和一些最初将加载的模板。 如果控制器中有一些错误它将抛出flash.warning,如

def err = resp?.status?.message?:resp?.status?.message?:message(code:'common.unknown.error')
flash.warning = message(code:'common.error.default', args:[err])

在gsp中我会用

来捕捉它
<div id="errorPanel">
        <g:if test="${flash.warning}">
                <g:message code = "reporting.label" />
                <div class="message">${flash.warning}</div>
    </g:if>
   </div>   

如果te控制器抛出警告它只在页面刷新后显示,那么一切都很好。 请帮帮我

我的gsp是

<%@ page language="java" import="com.cisco.rms.constants.TicketType" %>
<html>
    <head>
        <meta name="layout" content="main" />
        <g:set var="entityName" value="${message(code: 'incident.label', default: '<incident.label>')}" />
        <title><g:message code = "incident.list" /></title>
   </head>
   <body>
   <div id="errorPanel">
        <g:if test="${flash.warning}">
                <g:message code = "reporting.label" />
                <div class="message">${flash.warning}</div>
        </g:if>
   </div>
   <div class="ros-pageContainer">
       <div id="incidentListContainer" class="rosPanel">
           <div id="incidentListHeaderContainer">
               <div class="ui-widget-header">
                   <span class="ros-title"> <g:message code="incident.label.plural" /> </span>
                   <g:include controller="ticket" action="searchTicketsInclude"/>
                   <!--
                   <span class="header-quick-action">       
                       <select name="quickactions">
                              <option selected> <g:message code="common.quickActions.header" /> </option>
                              <option value="download"><g:message code="common.quickActions.download" /> </option>
                              <option value="list and details"><g:message code="common.quickActions.listAndDetails" /> </option>
                              <option value="list only"><g:message code="common.quickActions.listOnly" /> </option>
                              <option value="list all"><g:message code="common.quickActions.listAll" /> </option>
                              <option value="print list"><g:message code="common.quickActions.printList" /> </option>
                              <option value="columns"><g:message code="common.quickActions.columns" /> </option>
                       </select>                                    
                   </span> -->
               </div> 
            </div>
            <jqgrid:wrapper id="incident" />
        </div>
        <g:render template="details"/>

        <br/>
        <g:render template="/site/sitesAffected"/>

        <br/>
        <g:render template="/device/listForIncident"/>  
        <br/>
        <g:render template="/workLog/list"/>    
        <br/>

        <g:render template="/alarm/listForIncident"/>

    </div>
    <g:render template="/layouts/gridTemplate" />
    <script type="text/javascript"> 

        function searchByTicketId()
        {
            var ticketId = $('#ticketId').val();
            var id_type = $("input[@name=id_type]:checked").val();;

            var ext = false;
            if(id_type == 'external')
                ext = true;

            search(ticketId, ext);
        }

        function search(ticketId, ext)
        {   
            var grid = $('#incidentGrid');

            grid.jqGrid("clearGridData", true);                 
            grid.jqGrid('setGridParam',{postData:{ticketId:ticketId, extTicketId:ext}});            
            grid.trigger( 'reloadGrid' );

            // alert('ticketId: ' + ticketId + ' type: ' + id_type + ' external: ' + ext);
        }

        function resetSearch()
        {
            $('#ticketId').val('');

            var grid = $('#incidentGrid');
            grid.jqGrid("clearGridData", true)
            grid.jqGrid('setGridParam',{postData:{ticketId:null}});
            grid.trigger( 'reloadGrid' );

        }

        function onIncidentGridComplete() {
            var devCount = $('#incidentGrid').getGridParam("reccount");
            if (devCount == 1) {
                var rowId = $('#incidentGrid').getDataIDs()[0];
                $('#incidentGrid').setSelection(rowId, true);
            }
            $(window).trigger('resize');
        }

        var ticketId;
        var ticketType = '${com.cisco.rms.constants.TicketType.INCIDENT}';

        function quickActions(actionsList)
        {
            var sel = actionsList.selectedIndex

            if(sel == 1)
            {
                window.open('../workLog/addWorkLog?ticketId=' + ticketId + '&ticketType=' + ticketType, 'Add Worklog', 'width=600,height=600', false)
            }               
        }

        function resizeGrid(){
            //$('#incidentGrid').jqGrid('setGridHeight',$(window).outerHeight() - 255 - 25 - $("#incidentDetailsContainer").outerHeight());
        }

        function clearDetails() {
            clearIncDetails();
        }

        function onSelectRow(rowId) {
            clearDetails();
            $('#quickActions').show();

            ticketId = $('#incidentGrid').getCell(rowId, 'id');
            populateDetails(rowId);

            var rowMinus1 = parseInt(rowId) - 1;
            $("#incidentGrid tr").removeClass("ui-state-highlight-1");
            $("#incidentGrid tr:eq(" + rowMinus1 + ")").addClass("ui-state-highlight-1");
            resizeGrid();

            reloadDeviceGrid(ticketId);
            reloadWorklogs (ticketId, ticketType);
            reloadSitesAffectedGrid(ticketId, ticketType);
            reloadAlarms(ticketId);
        }

        function populateDetails(rowId) {
            var grid = $('#incidentGrid');
            var id = grid.getCell(rowId, 'id');
            var submitDate = grid.getCell(rowId, 'submitDate');
            var summary = grid.getCell(rowId, 'summary');
            var status = grid.getCell(rowId, 'status');
            var type = grid.getCell(rowId, 'type');

            var data = {id:id, submitDate:submitDate, summary:summary, status:status, type:type};
            populateIncDetails(data);
        }




        $(document).ready(function() {

            <jqgrid:grid
                id="incident"
                url="'${createLink(action: 'listByCompanyJSON', params: [ticketId: params.ticketId])}'"
                colNames="
                    '${g.message( code:'common.priority' )}',
                    '${g.message( code:'common.id' )}',
                    '${g.message( code:'user.details.site' )}',
                    '${g.message( code:'common.impact' )}',
                    '${g.message( code:'incident.opened' )}',
                    '${g.message( code:'common.parameters.duration' )}',
                    '${g.message( code:'common.status' )}',
                    '${g.message( code:'common.description' )}',
                    '${g.message( code:'common.type' )}'
                "
                colModel="
                    {name:'priority', editable: false, fixed: true, width:'55px'},
                    {name:'id', editable: false, fixed: true, width:'100px'},
                    {name:'siteName', editable: false, fixed: true, classes:'ellipsis', width: '200px'},
                    {name:'impact', editable: false, fixed: true, width:'125px'},
                    {name:'submitDate', editable: false, fixed:true, width:'125px'},
                    {name:'duration', editable: false, fixed:true, width:'95px'},
                    {name:'status', editable: false, fixed: true, width:'65px'},
                    {name:'summary', editable: false, classes:'ellipsis' },
                    {name:'type', editable: false, hidden: true}
                "

                gridComplete="onIncidentGridComplete"
                onSelectRow="onSelectRow"
                resizable="true"
                sortname="'id'"
                sortorder="'desc'"
                height="265"
                autowidth="true"
                shrinkToFit="true"
                scrollOffset="16"
                viewrecords="true"
                showPager="true"
                rowNum="12"
                datatype="'json'">
                <jqgrid:navigation  id="incident"   refresh="true" />
                <jqgrid:resize id="incident" resizeOffset="0" />
            </jqgrid:grid>

            //Strips garbage from grid
            $("#incidentListContainer").find("#incidentWrapper").first().removeClass("ui-widget-header");

            //Wires window resize so grid will fit properly at all sizes
            $(window).resize(resizeGrid);

            $(window).trigger('resize');



        }

        );

        </script>
</body>   
</html>

2 个答案:

答案 0 :(得分:1)

这是因为flash对象仅在下一个请求时可用,因此仅在页面刷新后才可用。你可以在这里阅读 - &gt; http://grails.org/doc/latest/ref/Controllers/flash.html

您是否尝试使用Ajax?我不确定Grails 2.x但是当我使用Grails 1.3.7时,我们必须创建一个解决方法来强制消息在不使用flash范围的情况下出现。

答案 1 :(得分:0)

如果这是你期望的行为:

load page A -> call controller -> see flash.warning on page A

然后我担心你必须弄清楚其他一些显示信息的方法。此外,执行ajax调用时,flash对象不起作用。

如果你想用ajax做,你可以在模板中使用params

<g:if test="${params?.variable}"> ...