2012-08-13 82 views
1

我想问为什么我的外部css/js没有加载到我的jsp中。 我的struts.xml内容:无法在Struts2中加载外部文件(image,css,js)

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 
    "http://struts.apache.org/dtds/struts-2.3.dtd"> 
<struts> 
<constant name="struts.enable.DynamicMethodInvocation" value="false" /> 
<constant name="struts.devMode" value="true" /> 
<constant name="struts.action.extension" value=""/> 

<package name="default" namespace="/" extends="struts-default"> 

    <default-action-ref name="home" /> 

    <action name="home"> 
     <result name="success">index.jsp</result> 
    </action> 

</package> 

</struts> 

我的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_2_5.xsd" 
     version="2.5"> 

<display-name>Test</display-name> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:applicationContext.xml</param-value> 
</context-param> 

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

<filter> 
    <filter-name>struts2</filter-name> 
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>struts2</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

<welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
</welcome-file-list> 

<error-page> 
    <error-code>404</error-code> 
    <location>error404.jsp</location> 
</error-page> 

</web-app> 

我使用此代码加载我的CSS在我的jsp

<link rel="stylesheet" type="text/css" href="../style/css/style.css"/> 

我也尝试使用此代码

<link rel="stylesheet" type="text/css" href="<s:url value="/style/css/style.css"/>"/> 

甚至,它的出现错误时,我想在我的JSP显示图像

<img src="<s:url value="/style/images/icon/car.png"/>" /> 

有什么不对的,这个代码(web.xml)中

<filter-mapping> 
    <filter-name>struts2</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

的原因是否谢谢您的答复。

+0

它是什么错误? – 2012-08-13 05:11:04

+0

如果你使用绝对路径..?像你的web应用程序/风格/的CSS/style.css“ – 2012-08-13 06:07:16

+0

@BheshGurung,错误,CSS没有加载,当我打开查看页面源,然后我点击CSS链接,它返回一个空白页 – 2012-08-14 08:59:51

回答

0

肯定这是一个路径问题。您可以在网络中使用Firebug来查看浏览器尝试加载的确切URL。之后,您将可以更正css网址。当然,你不能使用一个标签。

+0

“NetworkError:404 Not Found -http:// localhost:8080/vhmon/style/css/style.css” 但是当我更改struts url模式从/ *到/ main(例如),css被加载 – 2012-08-14 09:27:19

2

竟然只是因为这段代码在我的struts.xml

<constant name="struts.action.extension" value=""/> 

当我删除它,一切工作正常。对不起ñ感谢ü

0

您的过滤器阻止这些图像,CSS

0

您的外部文件夹是不是在Web服务器的根;所以它不认识它,那么你必须添加它