带有 Spring MVC <3.0.4 的 JSP 中的源 JavaScript 文件

source javascript file in jsp with spring mvc <3.0.4

本文关键字:JSP 文件 JavaScript MVC Spring 带有      更新时间:2023-09-26

我知道有人问过这个问题,但这并没有解决我的问题,并且尝试了很多事情都没有成功:

我的 Spring Webapp 项目目录非常标准,看起来像:

/webapp/js/* eg.  /webapp/js/query.jeditable.js
/webapp/WEB-INF/jsp/*
/webapp/WEB-INF/web.xml+
/webapp/WEB-INF/spring-servlet.xml - dispatcher servlet config
/webapp/WEB-INF/spring-security.xml - spring security config

我正在尝试从 jsp url 中获取 js 中的 javascript 文件,该文件也通过 spring 安全 url 匹配器进行过滤。这是我的网站.xml:

网络.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
    <display-name>Welcome to projectBananaStand</display-name>
    <description>
            Welcome to 'projectBananaStand' Add Event Test
    </description>
    <context-param>
        <param-name>jmxLogEnabled</param-name>      
        <param-value>false</param-value>
    </context-param>
  <servlet>  
  <servlet-name>spring</servlet-name>  
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
  <load-on-startup>1</load-on-startup>  
 </servlet>  
 <servlet-mapping>  
  <servlet-name>spring</servlet-name>  
  <url-pattern>*.htm</url-pattern>
 </servlet-mapping>

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

    <listener>
            <listener-class>com.jpmorgan.tyger.listeners.JMXLog4JContextListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>registerName</param-name>       
        <param-value>projectBananaStand</param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/applicationContext.xml
            /WEB-INF/spring-security.xml
        </param-value>
    </context-param>

        <!-- 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>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <error-page>
        <error-code>500</error-code>
        <location>/custError.jsp</location>
    </error-page>
    <distributable />
</web-app>

这是我尝试获取 js 文件的 jsp 代码:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
        <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
        <!-- DataTables CSS -->
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.4/css/jquery.dataTables.css">
<!-- DataTables -->
<script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.4/js/jquery.dataTables.js"></script>
<!-- editable datatables -->
<script src="/js/jquery.jeditable.js" type="text/javascript"></script>
<script src="/js/jquery.dataTables.editable.js" type="text/javascript"></script>
<script src="/js/addEvent.js" type="text/javascript"></script>
<script src="http://cdn.jsdelivr.net/jquery.validation/1.13.1/jquery.validate.js" type="text/javascript"></script>

这是弹簧奴仆.xml

<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"  
 xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans   
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
  http://www.springframework.org/schema/context  
  http://www.springframework.org/schema/context/spring-context-3.0.xsd
  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd  
  http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

 <context:annotation-config />  
 <!-- declaring base package  -->
 <context:component-scan base-package="com.banana.controller" />

<mvc:annotation-driven/>

 <!-- adding view resolver to show jsp's on browser -->
 <bean id="viewResolver"  
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix">
  <value>/WEB-INF/jsp/</value></property>
  <property name="suffix">
  <value>.jsp</value>
  </property>
 </bean>  
     <bean class="org.springframework.context.support.ResourceBundleMessageSource"
        id="messageSource">
        <property name="basename" value="messages" />
    </bean>

  </beans>  

所有应用程序级别的 bean 和 spring 安全相关的配置都已在 applicationContext 中定义.xml并通过 contextListener 添加

我不能使用

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

因为我使用的是 Spring 3.0.3 并且不想升级整个框架。

我尝试只使用 spring mvc 3.1 的 xmlns 定义,但这仍然导致错误 - mvc:找不到资源声明。

我尝试使用 url 模式映射默认 servlet

*.js, /js/**, and /somethingelse/* 

他们破坏了 Spring 安全默认登录页面加载。Spring 安全登录页面的"找不到资源"错误。

在jsp文件中尝试了不同的标签,包括

source="<c:url value="something" />"

请帮忙,如何在不mvc:resource mapping的情况下将javascript文件包含在jsp中,或者在不更改所有spring框架版本的情况下使用mvc:resource mapping,只需使用不同的 xmlns schemaLocation,不带弹簧安全与default servlet

有没有办法包含文件而不将它们视为 url?我做错了什么? 帮助将不胜感激,因为我花了很多时间与之作斗争。谢谢

将 js 文件夹放入/WEB-INF/jsp/文件夹中,这可能是我没有尝试过的工作。