如何在java spring中自定义font-faces

时间:2014-11-30 16:53:01

标签: java css spring twitter-bootstrap font-face

在java spring项目中有映射文件的特殊指令, 我已经尝试映射我的css文件并且它工作正常,但是在bootstrap.css中内置的@ font-faces无法正常工作  见例子:

web.xml

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>
                   org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>
                  org.springframework.web.context.ContextLoaderListener
            </listener-class>
</listener>

在mvc-dispatcher-servlet.xml

<mvc:resources mapping="/resources/**" location="/resources/" />
在header.jsp中

<link rel="stylesheet" type="text/css" href="<c:url value="${baseURL}/resources/css/bootstrap.css" />" />
<link rel="stylesheet" type="text/css" href="<c:url value="${baseURL}/resources/css/bootstrap-rtl.min.css" />" />

bootstrap.css

@font-face {
  font-family: 'Glyphicons Halflings';

  src: url('../fonts/glyphicons-halflings-regular.eot');
  src: url('../fonts/glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'), url('../fonts/glyphicons-halflings-regular.woff') format('woff'), url('../fonts/glyphicons-halflings-regular.ttf') format('truetype'), url('../fonts/glyphicons-halflings-regular.svg#glyphicons_halflingsregular') format('svg');
}

在实例上部署字体时,结果显示:

enter image description here

参考正确实现glyphicon iconic font:twitter-bootstrap documentation

1 个答案:

答案 0 :(得分:0)

您应该在web.xml中指定字体的url映射和mime类型

对于映射字体,您可以在web.xml中执行类似的操作

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/font/*</url-pattern>
</servlet-mapping>

并且为了映射mime类型,你可以做类似的事情

<mime-mapping>
    <extension>eot</extension>
    <mime-type>application/vnd.ms-fontobject</mime-type>
</mime-mapping>

对于其他类型的字体,您可以执行类似

的操作
<mime-mapping>
    <extension>woff</extension>
    <mime-type>application/x-font-woff</mime-type>
</mime-mapping>
<mime-mapping>
    <extension>ttf</extension>
    <mime-type>application/octet-stream</mime-type>
</mime-mapping>

这件事在我的案例中起作用,希望这也适用于你。