java - jsp 中的源 javascript 文件与 spring mvc <3.0.4

标签 java javascript jsp spring-mvc servlets

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

我的 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 security url匹配器进行过滤。 这是我的 web.xml:

web.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>

这是 spring-servlet.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 security相关的配置都已在applicationContext.xml中定义并通过contextListener添加

我无法使用

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

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

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

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

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

他们破坏了 Spring Security 默认登录页面加载。 Spring Security 登录页面出现资源未找到错误。

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

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

请帮忙,如何在没有 mvc:resource mapping 的情况下将 javascript 文件包含在 jsp 中,或者使用 mvc:resource mapping 而不更改所有 spring 框架版本,只使用不同的 xmlns schemaLocation并且不使用默认servlet带来Spring安全性

有没有办法包含这些文件而不将它们视为网址?我究竟做错了什么? 非常感谢您的帮助,因为我花了很多时间来解决这个问题。 谢谢

最佳答案

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

关于java - jsp 中的源 javascript 文件与 spring mvc <3.0.4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28332768/

相关文章:

java - 使用观察者模式是否合适?

javascript - 替换数组内容的简短方法

javascript - 如果域名 = 则更改 div 的类

javascript - 如何从 Javascript 更改 HTML 单选按钮选择?

java - 更改 JSF h :selectManyCheckbox on event 的选定项目

java - 如何在 Netbeans 中使用打开 WAR 文件夹

java - 访问 JavaFX 中的 Controller ,然后执行另一个类中的函数

Java - 根据请求关闭并发线程

java - 测量 session 属性对象的大小

java - Java SE 中的 JPA 与 Java EE 性能对比