我有一个带有 struts2 框架的 Java 网络应用程序。在我的 index.jsp
文件,我链接了 CSS 文件,但它没有显示在我的页面和 Error 404: not found
显示在 Firebug
.我在没有使用 Struts 的情况下测试了一个应用程序,一切正常。但是当我添加 struts2 库和配置时 struts.xml
和 web.xml
文件,CSS 不显示 :(
这是我的 index.jsp:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="styles/main.css" rel="stylesheet" type="text/css"/>
</head>
当然我加了<%@ taglib prefix="s" uri="/struts-tags" %>
并尝试了 <s:url>
在 href 中,但看不到任何效果。
这是 web.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<display-name>HotelSystem</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
这是我的 struts.xml 的一部分:
<package name="default" namespace="/" extends="struts-default">
<action name="index.jsp" class="Actions.Test">
<result name="success">/index.jsp</result>
</action>
</package>
注意: Action.Test
是一个测试 Action ,总是返回成功。
最佳答案
您应该将 .css 类型映射到 web.xml 文件中,如下所示!
<filter-mapping>
<filter-name>expires-filter</filter-name>
<url-pattern>*.css</url-pattern>
</filter-mapping>
关于css - 无法在 Struts2 中应用外部 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21038957/