如何使用tiles3在Spring MVC 3中加载js和css文件

时间:2014-03-03 07:33:46

标签: css spring jsp spring-mvc tiles

我无法使用带有tiles3的Spring MVC 3将Js和Css文件加载到jsp中。有人能帮我吗。这是我的配置。

项目文件夹结构

-SpringWithTiles
----webapp
--------resouces
----------script
--------------js
---------------myscript.js
--------------css
---------------style.css
----WEB-INF
-------jsp
---------signIn.jsp

servlet.xml中

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

 <mvc:resources mapping="/resources/script/js/**"  location="/webapp/resources/script/css" />
<mvc:resources mapping="/resources/script/css/**" location="/webapp/resources/script/js" />`

tiles.xml

 <tiles-definitions>  
   <definition name="baseLayout" template="/WEB-INF/layout/simpleLayout.jsp">  
       <put-attribute name="title" value="" />  
       <put-attribute name="header" value="/WEB-INF/layout/header.jsp"/>  
       <put-attribute name="body" value="" />  
       <put-attribute name="footer" value="/WEB-INF/layout/footer.jsp" />
  </definition>  
 <!--   <definition name="baseLayoutWithLeftPane" template="/jsp/layout 
/layoutWithLeftPane.jsp">  
       <put-attribute name="title" value="Learn - Educate" />  
       <put-attribute name="header" value="/jsp/layout/header.jsp" />  
       <put-attribute name="navigation" value="/jsp/layout/navigation.jsp" />  
       <put-attribute name="body" value="" />  
       <put-attribute name="footer" value="/jsp/layout/footer.jsp" />  
  </definition>  
   <definition name="welcome" extends="baseLayoutWithLeftPane">  
       <put-attribute name="title" value="Learn - Educate" />  
       <put-attribute name="body" value="/jsp/home.jsp" />  
  </definition>   -->
  <definition name="siginIn" extends="baseLayout">  
       <put-attribute name="title" value="siginIn" />  
       <put-attribute name="body" value="/WEB-INF/jsp/siginIn.jsp" />  
  </definition>       

的web.xml

      <servlet-mapping>
    <servlet-name>babwitU</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
    <welcome-file-list>
    <welcome-file>/index.jsp</welcome-file>
  </welcome-file-list>

的index.jsp

<a href="redirectToSiginIn">Click Here</a>

Contoller.java

@RequestMapping("/redirectToSiginIn")
public ModelAndView siginIn(){
    ModelAndView mav=new ModelAndView();
    mav.setViewName("/siginIn");
    return mav;
}

3 个答案:

答案 0 :(得分:0)

在servlet.xml中更改映射,如此location =“/ resources / script / css” 并从您要加载ur js或css

的resources / fileNameToLoad中提供路径

在location属性中,来自/ resources / **的路径不是来自/ webapps /

答案 1 :(得分:0)

正确的映射应该是:

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

属性/中的第一个location表示${webappRoot}已经存在。

答案 2 :(得分:0)

我的问题通过以下配置得到解决..感谢所有

<强>的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">
  <display-name>babwitU</display-name>
  <servlet>
    <servlet-name>babwitU</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/coreJava.xml,/WEB-INF/home.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>babwitU</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
    <welcome-file-list>
    <welcome-file>/index.jsp</welcome-file>
  </welcome-file-list>

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

</web-app>

<强> servelt.xml

    <context:component-scan base-package="com.babwitU.life.dream.controller" />
    <mvc:annotation-driven />  
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <!-- <property name="location" value="classpath:jdbc.properties" /> -->
    </bean>

<!-- <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>  -->
    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" id="viewResolver">  
    <property name="viewClass">  
    <value>  
        org.springframework.web.servlet.view.tiles3.TilesView  
    </value>  
     </property>  
</bean>  
    <bean id="tilesConfigurer"  
  class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">  
  <property name="definitions">  
        <list>
            <value>/WEB-INF/tiles.xml</value>
        </list>
    </property>
</bean>

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

<强> controller.java

@RequestMapping(value ="redirectToSiginIn", method = RequestMethod.GET)
    public ModelAndView viewSiginIn(){
        ModelAndView mav=new ModelAndView();
        mav.setViewName("siginIn");
        return mav;
    }
    @RequestMapping(value ="home", method = RequestMethod.GET)
    public ModelAndView viewHome(){
        ModelAndView mav=new ModelAndView();
        mav.setViewName("home");
        return mav;
    }
    @RequestMapping(value ="java", method = RequestMethod.GET)
    public ModelAndView viewJava(){
        ModelAndView mav=new ModelAndView();
        mav.setViewName("java");
        return mav;
    }

<强> myjsp.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> <tiles:getAsString  name="title" ignore="true"></tiles:getAsString >
</title>
<!-- <script type="text/javascript" src="resources/script/js/appjs.js"></script>
 --><link rel="stylesheet" type="text/css" href="resources/script/style.css">
</head>
<body>

文件夹结构

  -springwithtiles
    ---webapp
    -----resources
    -------images
    ----------one.jpg
    -------script
    ------------style.css
    ------------myjs.js
    -----web-inf
    --------jsp