【问题标题】:External file (image, css, js) can not loaded in Struts2Struts2 无法加载外部文件(图像、css、js)
【发布时间】:2012-08-13 04:50:51
【问题描述】:

我想问为什么我的外部 css/js 没有加载到我的 jsp 中。 我的 struts.xml 内容:

<?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>

感谢您的回复。

【问题讨论】:

  • 如果你使用绝对路径..怎么办?喜欢你的 web-app/style/css/style.css"
  • @BheshGurung,css未加载的错误,当我打开查看页面源然后我点击css链接时,它返回一个空白页面
  • @UmeshAwasthi,这个项目我是用 maven 构建的,所以没有 web-app 文件夹

标签: jsp struts struts2


【解决方案1】:

原来只是因为我的struts.xml中有这段代码

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

当我删除它时,一切正常。对不起,谢谢你

【讨论】:

    【解决方案2】:

    这肯定是路径问题。您可以使用 Firebug with Network 查看浏览器尝试加载的确切 URL。之后,您将能够更正 css URL。当然你不能使用insite标签。

    【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      您的外部文件夹不在网络服务器的根目录中;所以它不认识它,那么你必须添加它

      【讨论】:

        猜你喜欢
        • 2020-07-12
        • 1970-01-01
        • 2018-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-08
        • 1970-01-01
        • 2015-06-14
        相关资源
        最近更新 更多