以编程方式检测是否已读入外部配置文件?

时间:2015-09-02 00:14:55

标签: grails grails-config

我们使用grails.config.locations从应用程序外部的文件中提取(可选)设置。有没有一种很好的方法来检测这个文件是在Bootstrap.groovy中还是在Config.groovy本身中加载的?

Config.groovy有这个:

<html>
<head>
<link type="text/css"rel="stylesheet" href="stylesheet.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<title> Comment Post </title>
<script>
    function validate()
    {
        var name = document.getElementById("name", "title", "textarea");
        var blank_field = false;
        if(name.value == "")
        {
            name.style.backgroundColor = "pink";
            blank_field = true;
        }
        else
        {
            name.style.backgroundColor = "lightgreen";
        }
        if(title.value == "")
        {
            title.style.backgroundColor = "pink";
            blank_field = true;
        }
        else
        {
            title.style.backgroundColor = "lightgreen";
        }
        if(textarea.value == "")
        {
            textarea.style.backgroundColor = "pink";
            blank_field = true;
        }
        else
        {
            textarea.style.backgroundColor = "lightgreen";
        }
        var select1 = document.getElementById("select1");
        var select2 = document.getElementById("select2");
        if(select1.value == "Choose A Color")
        {
            select1.style.border = "2px solid red";
            blank_field = true;
        }
        else
        {
            select1.style.border = "none";
        }
        if(select2.value == "Choose A Color")
        {
            select2.style.border = "2px solid red";
            blank_field = true;
        }
        else
        {
            select2.style.border = "none";
        }
        var icon = document.getElementsByName("icon");
        var radiobtns = document.getElementById("radiobtns");
        var blank_radio = true;
        for(var i = 0; i < icon.length; i++)
        {
            if(icon[i].checked == true)
            {
                blank_radio = false;
            }
        }
        if(blank_radio == true)
        {
            radiobtns.style.border = "2px solid red";
            blank_field = true;
        }
        else
        {
            radiobtns.style.border = "none";
        }
        var error = document.getElementById("error");
        if(blank_field == true)
        {
            error.innerHTML = "<h3 style = 'color:red;'>You missed a spot!</h3>";
        }
        else
        {
            error.innerHTML = "";

        }
        add();
    }
    var array = [];
    function add()
    {
        //variables
        var name = document.getElementById("name");
        var title = document.getElementById("title");
        var textarea = document.getElementById("textarea");
        var icon = document.getElementsByName("icon");
        var select1 = document.getElementById("select1");
        var select2 = document.getElementById("select2");
        var icon_value;
        for(i = 0; i < icon.length; i++)
        {
            if(icon[i].checked == true)
            {
                icon_value = icon[i].value;
            }
        }

        //all variables and loops go above this
        var storage = {name: name.value, title: title.value, comment: textarea.value, icon: icon_value, tcolor: select1.value, bgcolor: select2.value};
        array.push(storage);
        show();
    }//end function add
    function show()
    {
        var display = document.getElementById("display");
        var post = "";
        for(var i = 0; i < array.length; i++)
        {
            post += "<div style='background-color:"+array[i].bgcolor+";'>"; 
            post += "<span style='color:"+array[i].tcolor+";'>"; 
            post += "<b>" + array[i].name + "</b><br>";
            post += array[i].title + "<br>";
            post += array[i].comment + "<br>";
            post += "<img src = " + array[i].icon + " class = 'icon'  />";
            post += "</span>";
            post += "</div>";
            html += "<input type='checkbox' onclick='removeComment("+i+");'/>";
        }
        display.innerHTML = post;
    }//end function show
    function remove(index)
    {
        show.splice(index, 1);
        show();
    }
</script>
</head>
<body id = "body">
<div class = "container">
    <div class = "row">

        <div class="col-xs-12 col-sm-4" id="logo">
            <img class = "img-responsive" src = "bubble.jpg">
        </div>

    </div>
    <div class = "row">

        <div class = "col-xs-12" id = "menu">
            <ul class = "nav nav-tabs">
                <li><a href = "http://simplycoding.org">Simply coding</a></li>
                <li><a href = "http://google.com">Google</a></li>
                <li><a href = "http://gamersvortex.tk">Gamer's Vortex</a></li>
            </ul>
        </div>

    </div>
    <div class = "row">

        <div class = "col-xs-12 col-sm-8" id = "comments">COMMENTS
            <div class = "col-xs-12 col-sm-4" id = "form">

                <header id = "header">
                    LEAVE A COMMENT
                    <div id = "error"></div>
                </header>
                <form class = "form-inline">
                    <label>
                        Name:
                    </label>
                        <input type="text" class="form-control" id="name" placeholder="Enter Name">
                    <label>
                        Color:
                    </label>
                        <select id = "select1" class = "form-control">
                            <option>Choose A Color</option>
                            <option>Red</option>
                            <option>Blue</option>
                            <option>Pink</option>
                            <option>Turquoise</option>
                        </select>
                </form>
                <form class = "form-horizontal">
                    <label class = "control-label">
                        Title:
                    </label>
                    <input type="text" class="form-control" id="title" placeholder="Enter Comment Title">
                    <label>
                        Background Color:
                    </label>
                    <select id = "select2" class = "form-control">
                        <option>Choose A Color</option>
                        <option>Gold</option>
                        <option>#cc0099</option>
                        <option>Orange</option>
                        <option>Purple</option>
                    </select>
                </form>
                <form id = "radiobtns" class = "form-inline">
                    <label class = "control-label">
                        Icons:
                    </label>
                    <input type="radio" name="icon" value = "imoji.png">
                    <img src="imoji.png" class="icon" />

                    <input type="radio" name="icon" value = "imoji2.png">
                    <img src="imoji2.png" class="icon" />

                    <input type="radio" name="icon" value = "imoji3.png">
                    <img src="imoji3.png" class="icon" />

                    <input type="radio" name="icon" value = "imoji4.png">
                    <img src="imoji4.png" class="icon" />
                </form>
                <label class = "control-label">
                    Comment:
                </label>
                <textarea id = "textarea" class = "form-control" rows = "8"></textarea><br>
                <input class = "btn btn-default" type = "button" value = "Submit Comment" onclick = "validate()">

            </div>
            <div id = "display"></div>
        </div>

    </div>
</div>

很高兴知道Grails是否在此文件中找到并阅读。

1 个答案:

答案 0 :(得分:0)

您可能会在启用Grails调试模式时看到此信息,但您不想这样做。我正在使用以下策略。首先,我检查文件是否存在:

 def externalConfigFile = "${userHome}/.grails/${appName}-config.groovy"
 if (new File(externalConfigFile).exists()) {
     grails.config.locations << "file:" + externalConfigFile
     log.info "External configuration file found at ${externalConfigFile} ..." 
 }

然后在文件中我只记录它已运行:

 log.info 'External config file was loaded.'

所以现在我可以在启动日志中看到我找到了一个外部配置文件,它是一个现有文件,该文件是由Grails运行的,所以它被加载了。

我还没有找到更好的东西。