带有Spring MVC的JavaScript不起作用

问题描述:

我下载了使用CSS和JavaScript的登录模板.当我以默认的html格式启动它时,显示为ok,但是当我将相同的代码放入Spring MVC 3应用程序并将格式更改为jsp时,javascript部分被禁用.我可以通过网络浏览器访问外部js文件,所以我不知道问题出在哪里.

I downloaded login template which uses css and JavaScript. When I start it in default html format it displays ok, but when I put the same code to my Spring MVC 3 app and change format to jsp, javascript part is disabled. I can access external js files through my web browser, so I don't know where may be the problem.

这是正确的外观:

但这是我得到的:

<script language="javascript" type="text/javascript" src="js/niceforms.js"></script>
<link rel="stylesheet" type="text/css" media="all" href="css/niceforms-default.css" />

我只更改了外部js和CSS资源的位置,但是正如我所说的,这些文件是可访问的.

I only changed the location of external js and css resources but these files are accessable as I said.

这是我的结构:

在我的web.xml中,有以下几行:

In my web.xml I have these lines:

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/root-context.xml,
        /WEB-INF/spring/security-context.xml,
    </param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<!-- Spring Security -->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

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

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.txt</url-pattern>
</servlet-mapping>

我也使用Apache tile2

I also use Apache tiles2

<tiles-definitions>
<definition name="base.definition" template="/WEB-INF/views/layout/layout.jsp">
    <put-attribute name="body" value="" />
</definition>

<definition name="contact" extends="base.definition">
    <put-attribute name="body" value="/WEB-INF/views/contact.jsp" />
</definition>

<definition name="login" template="/WEB-INF/views/login/login.jsp">
</definition>

谢谢.

我知道问题出在哪里,但仍然不知道如何解决...问题出在外部CSS文件中的路径中.我有这样的内容:body {background-image:url('images/bg.jpg');},但是在萤火虫中找不到错误,因为它试图在 localhost:8080/sheedo/css/images/bg.jpg 中以及当我设置绝对值时找到资源资源路径,例如body {background-image:url('/images/bg.jpg');},我也遇到相同的错误,但它尝试在 localhost:8080/images/bg.jpg 中找到它.当我使用./images/bg.jpg没问题时,但是是否存在任何其他解决方案,而不是重写css文件中的所有路径?我的意思是像<mvc:resources ... >这样有可能吗?

I know where is the problem now, but still don't know how to solve it... Problem is in paths in external css files. I have something like this: body {background-image:url('images/bg.jpg');} but I get not found error in firebug because it tries to find the resource in localhost:8080/sheedo/css/images/bg.jpg and when I set absolute path of resource like body {background-image:url('/images/bg.jpg');} I get the same error too but it tries to find it in localhost:8080/images/bg.jpg. When I use ./images/bg.jpg it's ok, but does exist any other solution instead of rewrite all paths in css files? I mean something like <mvc:resources ... > is it possible ?

<script type="text/javascript" src="${pageContext.request.contextPath}/js/niceforms.js"></script>
<link href="${pageContext.request.contextPath}/css/niceforms-default.css" rel="stylesheet" />

使用上面的代码..您需要动态指定它,而不是静态地指定它..

Use the above code.. You need to specify it dynamically, not statically..