简体   繁体   English

GRAILS 3.1.0未显示Bootstrap字形

[英]GRAILS 3.1.0 not showing Bootstrap glyphicons

I am using bootstrap on a GRAILS 3.1.0 application with 我在GRAILS 3.1.0应用程序上使用bootstrap

compile 'org.webjars:bootstrap:3.3.6'

When I run the app both dev and prod from grails ... 当我运行该应用程序时,grails的dev和prod ...

grails prod run-app
grails dev run-app

... all work propertly, both bootstap components and glyphicons, but when I put the war on a Tomcat, the glyphicons are not shown, but the bootstrap components works propertly. ...引导组件和glyphicons都可以正常工作,但是当我对Tomcat进行战争时,没有显示glyphicons,但是引导组件可以正常工作。

I have seen some errors on traces with the form 我在表格上看到了一些错误

No mapping found for HTTP request with URI [/helloworldapplication/fonts/glyphicons-halflings-regular.ttf] in DispatcherServlet with name 'grailsDispatcherServlet'

The files could be found on Tomcat in the folder 这些文件可以在Tomcat上的文件夹中找到

webapps\helloworldapplication\assets\webjars\bootstrap\3.3.6\fonts

This happends in Grails 3.1.0 and 3.1.1 这发生在Grails 3.1.0和3.1.1中

I got the same trouble yesterday. 昨天我也遇到了同样的麻烦。 I tried to copy fonts during the war packaging phase at the right place, but it didn't work. 我试图在战争包装阶段的正确位置复制字体 ,但没有用。 I figured it out by just redirecting the request to the right path. 我只是通过将请求重定向到正确的路径来解决的。 To do so, first, I made an interceptor to catch the request and to redirect it : 为此,首先,我做了一个拦截器来捕获请求并重定向它:

class BootstrapInterceptor {

    int order = HIGHEST_PRECEDENCE
    String bootstrapVersion = null

    public BootstrapInterceptor() {
        match(uri: "/fonts/**")
    }

    boolean before() {
        if(bootstrapVersion == null) {
            setBootstrapVersion(request)
        }

        def to = "/assets/webjars/bootstrap/${bootstrapVersion}${request.getRequestURI()}"
        log.debug("Redirect: ${request.getRequestURI()} to: $to")
        redirect(uri: to)
        false
    }

    boolean after() { true }

    void afterView() {
        // no-op
    }

    private void setBootstrapVersion(def request) {
        def path = request.getServletContext().getRealPath("/")
        def bootstrapSubFolders = new File("${path}assets/webjars/bootstrap/").listFiles()

        if (bootstrapSubFolders?.length == 1) {
            bootstrapVersion = bootstrapSubFolders[0].name
            log.debug("Bootstrap version detected: $bootstrapVersion")
        }
        else if (bootstrapSubFolders?.length > 1) {
            bootstrapVersion = bootstrapSubFolders[0].name
            log.warn("Multiple versions of bootstrap detected, taking $bootstrapVersion")
        }
        else {
            log.warn("Unable to catch the bootstrap version")
            bootstrapVersion = ""
        }
    }
}

You need to tell to Grails to allow requests like /fonts/whatever . 您需要告诉Grails允许/ fonts / whatever之类的请求。 Without that, the interceptor won't be called. 没有那个,拦截器将不会被调用。 So, the little trick is to put that line on the UrlMappings : 因此,小技巧是将这一行放在UrlMappings上

"/fonts/**"(redirect: '/intercepted/by/bootstrap/interceptor')

And it should work (with your tomcat and without, and even if you upgrade your bootstrap version !). 而且它应该可以工作(无论您有没有tomcat,即使您升级了bootstrap版本!)。 With that method, you should not have a fonts folder inside you webapp (because it will be redirected to the bootstrap fonts folder). 使用这种方法,您在webapp内不应有一个fonts文件夹(因为它将被重定向到bootstrap fonts文件夹)。

Hope it helps you ;) 希望它能对您有所帮助;)

PS : if you don't want to make it as an interceptor, you can change your tomcat url mapping to do the same thing. PS:如果您不想使其成为拦截器,则可以更改tomcat url映射以执行相同的操作。

This is because of asset pipeline , the plugin manages the resource itself so it would also help to follow the convention. 这是因为资产管道,该插件本身管理资源,因此也有助于遵循约定。 Seems you have the fonts folder inside the bootstrap folder, on your application create the fonts folder inside the assets folder and rest should work by itself. 似乎您的bootstrap文件夹中有fonts文件夹,在您的应用程序上,在assets文件夹中创建了fonts文件夹,其余的应该可以单独工作。

I put my glyphicon font files inside /grails-app/assets/fonts/. 我将glyphicon字体文件放在/ grails-app / assets / fonts /中。 Then, from here , I did this: 然后,从这里开始 ,我这样做:

Put the fonts directory parallel to stylesheets, images and javascript into the grails-app/assets/ folder. 将与样式表,图像和javascript平行的fonts目录放入grails-app / assets /文件夹中。 For the asset pipeline to know the new directory, specify it in the build.gradle file: 为了使资产管道知道新目录,请在build.gradle文件中指定它:

 assets { minifyJs = true minifyCss = true includes = ["fonts/*"] } 

Then, it seems I also had to do a grails clean before the glyph icons showed up. 然后,似乎我还必须grails clean字形图标,然后才能显示字形图标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM