我没有用spring mvc抓住我的javascript文件

时间:2013-11-18 15:03:16

标签: javascript spring jsp spring-mvc

我有一个使用spring 2.5.6开发的应用程序网站。但我的javascript文件有问题,因为我的应用程序没有捕获这些文件。我不和他们合作,而且我做的更多,我找不到解决方案。

这是我的结构

src
   java
      controller
   test
   resource
   static
      js
        files.js
   WEB_INF
      pages
      spring
      lib 
      web.xml

这些是我的档案 的web.xml

<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>Web Logistic</display-name>

  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/root-context.xml,
            /WEB-INF/spring/spring-beans.xml
        </param-value>
    </context-param>

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

    <servlet>
        <servlet-name>mvc-dispatcher-servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
         <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/root-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:/log4j.properties</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <!-- 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>
</web-app>

根context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="
          http://www.springframework.org/schema/beans      
          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
          http://www.springframework.org/schema/context  
          http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 

...

<!--     Configuramos Spring MVC -->
    <import resource="mvc-dispatcher-servlet.xml"/>

</beans>

MVC-调度-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
          http://www.springframework.org/schema/beans      
          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
          http://www.springframework.org/schema/context   
          http://www.springframework.org/schema/context/spring-context-2.5.xsd">  

...

    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver" >
        <property name="viewClass">
            <value> org.springframework.web.servlet.view.tiles2.TilesView</value>
        </property>
    </bean>
    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/layouts/layouts.xml</value> 
            </list>
        </property>
    </bean>

    <!-- View Resolvers -->
<!--    Resuelve la ubicion de las vistas .jsp de @Controllers en la ruta /WEB-INF/pages -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    ...

</beans>

index.jsp(调用javascript文件)

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>    
<html>
<head>
    <script type="text/javascript" src="../static/js/md5.js"></script>
    <script type="text/javascript">

        function passwordToMD5()
                {
                    alert("estamos en el metodo de MD5");
                    var aleat = document.forms[0].aleat;
                    var jqPassClear = document.forms[0].password;
                    alert(aleat.value);
                    alert(jqPassClear.value);
                    //document.forms[0].user.value = $.trim(document.forms[0].user.value);

                    if(jqPassClear.value != "")
                    {
                        alert("entramos en el método jqPassClear");
                        alert($.trim(jqPassClear.value)));
                        document.forms[0].password.value = hex_md5(hex_md5($.trim(jqPassClear.value)));
                        //document.forms[0].password.value = hex_md5(aleat.value);
                        //document.forms[0].password.value = hex_md5(aleat.value);
                        alert(document.forms[0].password.value);
                    }
                    else
                    {
                        document.forms[0].password.value = "";
                    }

                    //document.forms[0].passwordClear.value = document.forms[0].user.value;
                }
    </script>
</head>
<body>
    <form:form modelAttribute="login" action="" method="post">
        <form:errors path="*" cssClass="errorblock" element="div" />
        <table>
            <tr>
                <td><spring:message code="message.user.userName" text="default text" /></td>
                <td><form:input path="name" />
                </td>
                <td><form:errors path="name" cssClass="error" />
                </td>
            </tr>
            <tr>
                <td><spring:message code="message.user.password" text="default text" /></td>
                <td><form:password path="password" />
                </td>
                <td><form:errors path="password" cssClass="error" />
                </td>
            </tr>
            ...
            <tr>
                <td colspan="3"><input type="submit" onclick="passwordToMD5();" /></td>
            </tr>
        </table>        
    </form:form>
</body>
</html>

这是我的过滤器

public class ProvisionResourceDispatcherServlet extends HttpServlet
{
    /**
     * 
     */
    private static final long serialVersionUID = -7319714368828547681L;

    @Override
    public void init () throws ServletException
    {
    }

    @Override
    public void doGet (HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException
    {
        String servletPath = req.getServletPath (); // /resource

        String url = servletPath;
        String lastPath = StringUtils.substringAfterLast (servletPath, "/");
        String extension = StringUtils.substringAfterLast (lastPath, ".");

        try
        {
            RequestDispatcher dispatcher = null;
            if (!StringUtils.isEmpty (extension)
                    && ("js".equals (extension) || "css".equals (extension) || "jpg".equals (extension))
                    || "bmp".equals (extension) || "gif".equals (extension))
            {
                dispatcher = req.getRequestDispatcher (url);
            }

            if (dispatcher != null)
            {
                dispatcher.include (req, rsp);
            }
            else
            {
                rsp.sendError (404);
            }
        }
        catch (Exception e)
        {

            if (!rsp.isCommitted ())
            {
                rsp.sendError (500);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

我相信你的问题是你将所有请求“/”映射到调度程序servlet。没有用于处理资源网址的控制器,因此您可能会从spring获得错误,因为它无法将网址映射到控制器。

如果您使用的是spring 3,那么您可以将此行添加到servlet上下文中。

<mvc:resources mapping="/resources/**" location="/resources/" />

这假设您的所有资源都位于名为“resources”的文件夹中,该文件夹位于WebRoot文件夹中,而不是WEB-INF!如果你使用maven那么它将是/ src / main / webapp / resources。不要将资源放在WEB-INF中,这是一个隐藏客户端内容的私有文件夹。如果您通过控制器显示JSP,则只能将JSP放在WEB-INF中,控制器可以看到此文件夹但客户端/浏览器不能。

你遇到了问题,因为你使用的是不支持资源标签的旧版spring。我认为您需要做的是将资源的servlet映射覆盖到默认的servlet而不是spring front controller。

像这样的东西。

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

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

这假定与另一个示例具有相同的文件夹结构。您可以调整自己项目结构的映射,但只记得它应该在WebRoot而不是WEB-INF中。

答案 1 :(得分:0)

静态文件(css,js,..)不能保留在WEB-INF文件夹中。它们应该公开供浏览器阅读。拿出来