当前位置: 首页 > 知识库问答 >
问题:

Grails方法代码太大问题

江睿
2023-03-14

我正在构建一个grails应用程序,groovy控制器抛出了一个错误——“方法代码太大”

代码:

class ComController {



    def ajaxGetMv = {
        if(params.metric == 'Marketing visits')
        {
            //All-4 "All" condition
            if (params.mv == 'All' && params.browser == 'All' && params.platform == 'All' && params.device == 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.time_period])
                render com as JSON
            }
            // 1 All conditions
            else if(params.mv == 'All' && params.browser != 'All' && params.platform != 'All' && params.device != 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.device = ? and p.browser = ? and p.platform = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.device, params.browser, params.platform, params.time_period])
                render com as JSON
            }
            else if(params.mv != 'All' && params.browser == 'All' && params.platform != 'All' && params.device != 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.device = ? and p.mv = ? and p.platform = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.device, params.mv, params.platform, params.time_period])
                render com as JSON
            }
            else if(params.mv != 'All' && params.browser != 'All' && params.platform == 'All' && params.device != 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.device = ? and p.mv = ? and p.browser = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.device, params.mv, params.browser, params.time_period])
                render com as JSON
            }
            else if(params.mv != 'All' && params.browser != 'All' && params.platform != 'All' && params.device == 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.platform = ? and p.mv = ? and p.browser = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.platform, params.mv, params.browser, params.time_period])
                render com as JSON
            }

            // 2 All conditions
            else if(params.mv == 'All' && params.browser == 'All' && params.platform != 'All' && params.device != 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.platform = ? and p.device = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.platform, params.device, params.time_period])
                render com as JSON
            }
            else if(params.mv == 'All' && params.browser != 'All' && params.platform == 'All' && params.device != 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.device = ? and p.browser = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.device, params.browser, params.time_period])
                render com as JSON
            }
            else if(params.mv == 'All' && params.browser != 'All' && params.platform != 'All' && params.device == 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.platform = ? and p.browser = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.platform, params.browser, params.time_period])
                render com as JSON
            }
            else if(params.mv != 'All' && params.browser == 'All' && params.platform == 'All' && params.device != 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.mv = ? and p.device = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.mv, params.device, params.time_period])
                render com as JSON
            }
            else if(params.mv != 'All' && params.browser != 'All' && params.platform == 'All' && params.device == 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.mv = ? and p.browser = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.mv, params.browser, params.time_period])
                render com as JSON
            }
            else if(params.mv != 'All' && params.browser == 'All' && params.platform != 'All' && params.device == 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.mv = ? and p.platform = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.mv, params.platform, params.time_period])
                render com as JSON
            }

            //3 All conditions
            else if(params.mv == 'All' && params.browser == 'All' && params.platform == 'All' && params.device != 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.device = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.device, params.time_period])
                render com as JSON
            }
            else if(params.mv != 'All' && params.browser == 'All' && params.platform == 'All' && params.device == 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.mv = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.mv, params.time_period])
                render com as JSON
            }
            else if(params.mv == 'All' && params.browser != 'All' && params.platform == 'All' && params.device == 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.browser = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.browser, params.time_period])
                render com as JSON
            }
            else if(params.mv == 'All' && params.browser == 'All' && params.platform != 'All' && params.device == 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.platform = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.platform, params.time_period])
                render com as JSON
            }


            //No all - basic condition
            else
            {
                def com = Com.executeQuery("select p.date_hour, (p.marketing * 100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.device = ? and p.browser = ? and p.platform = ? and p.mv = ? and p.time_period = ? order by col_0_0_ asc ",[params.department, params.device, params.browser, params.platform, params.mv, params.time_period])
                render com as JSON
            }
        }

if中的整个代码将重复15次,因为有15个度量(params.metric),因此会出现错误。查找错误后,唯一的选择是拆分代码。

所以我在grails中使用“重定向”操作拆分它,并将为每个if-fe语句使用不同的方法,以防止方法代码太大

新代码:

def ajaxGetMv = {
        if(params.metric == 'Marketing visits')
        {
            redirect ( action : "ajaxGetMv2" )
        }

但是,这不起作用,甚至没有抛出错误。为什么重定向不起作用?

还有什么其他方法可以拆分代码?

欢迎所有方法/建议。

共有2个答案

尉迟子民
2023-03-14

通过简单地调用方法中的方法来解决它

代码:

def ajaxGetMv = {
        if(params.metric == 'Marketing visits')
        {
            ajaxGetMv2();
        }

def ajaxGetMv2 = {
//All if-else conditions

}

以这种方式编写确保没有方法代码

墨宜人
2023-03-14

查看条件查询。它可能会帮助您将查询参数化,并消除那些糟糕的if-else块

 类似资料:
  • 如果Grails模板大小太大,并且使用的g:标记太多,无法通过模板引擎编译,那么会引发错误方法代码太大?

  • 我有一个gsp文件,其中包含1800行代码和从该视图渲染的两个模板。在我添加几个if条件之前,该视图工作得非常好。 堆栈跟踪如下:

  • 解决方案是什么?只有4-5行代码隐藏并重新启动,然后完全成功运行,引导文件大小为149k。当我注释或删除4-5行代码时,它将无误运行!

  • 问题内容: 我有一个单元测试,其中我静态定义了一个很大的字节数组(超过8000个字节)作为我每次运行单元测试时都不希望读取的文件的字节数据。 这在Eclipse中可以正常编译,但是通过Ant脚本进行编译时,出现以下错误: 任何想法为何以及如何避免这种情况? 答案 :Shimi的答案成功了。我将字节数组移到一个单独的类中,并且编译良好。谢谢! 问题答案: Java中的方法在字节码中限制为64k。静态

  • 问题内容: 我正在尝试运行我的应用程序,但在编译时出现错误,当最小字符超过时,代码太大。 那么,除了JSP:include还是将页面分成多个部分,还有其他方法吗?如何在Glassfish服务器的编译中设置JSP页面的最大字符数限制? 谢谢 问题答案: Java类的大小限制为(64k),此问题中对此进行了详细描述:Java类/异常表的最大大小 因此,这是一个硬限制,不应覆盖(如果内部地址为2字节,从

  • 我无意中发布了一个APK,它有一个非常大的版本代码。即整数值的最大值。