gpt4 book ai didi

twitter-bootstrap - GRAILS 3.1.0 不显示 Bootstrap 字形

转载 作者:行者123 更新时间:2023-11-28 22:27:44 26 4
gpt4 key购买 nike

我在 GRAILS 3.1.0 应用程序上使用 bootstrap

compile 'org.webjars:bootstrap:3.3.6'

当我从 grails 运行开发和生产应用程序时 ...

grails prod run-app
grails dev run-app

...所有工作正常,bootstap 组件和 glyphicons,但是当我将 war 放在 Tomcat 上时,glyphicons 没有显示,但 bootstrap 组件工作正常。

我在表格的痕迹上看到了一些错误

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

Tomcat 上的文件夹中可以找到这些文件

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

这发生在 Grails 3.1.0 和 3.1.1 中

最佳答案

我昨天遇到了同样的问题。我试图在正确的位置复制 war 打包阶段 期间的字体,但没有成功。我通过将请求重定向到正确的路径来解决这个问题。为此,首先,我创建了一个拦截器 来捕获请求并将其重定向:

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 = ""
}
}
}

您需要告诉 Grails 允许像/fonts/whatever 这样的请求。没有它,拦截器将不会被调用。所以,小技巧是将那行放在 UrlMappings 上:

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

它应该可以工作(无论有没有你的 tomcat,即使你升级了你的 Bootstrap 版本!)。使用该方法,您的 webapp 中不应有 fonts 文件夹(因为它将被重定向到 bootstrap fonts 文件夹)。

希望对你有帮助;)

PS:如果你不想让它成为一个拦截器,你可以改变你的 tomcat url 映射来做同样的事情。

关于twitter-bootstrap - GRAILS 3.1.0 不显示 Bootstrap 字形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35302652/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com