zoukankan      html  css  js  c++  java
  • spring mvc + freemarker 引入静态文件(css,img,js)

    http://my.oschina.net/duoduo3369/blog/168458

    目录[-]

    1 刚开始搞spring mvc和freemarker,遇到了不少问题,首先是导入静态文件,记录一下,希望能帮助亲们少走弯路吧。
    2 本章不介绍freemarker的神马复杂用法(因为我也不会),也不讲解spring mvc的种种,仅仅关注二者结合后静态文件的导入问题,和最初的一些配置。

    >文件结构一览

    在此输入图片描述

    >jar包一览

    在此输入图片描述

    >web.xml

    关键一句话,就是springmvc的那个servlet,因为要用RESTFul风格,所以拦截/,并且让它去读springMVC来初始化(java /src 根路径)。

    01 <?xml version="1.0" encoding="UTF-8"?>
    02 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    03 xmlns="http://java.sun.com/xml/ns/javaee"
    04 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    05 id="WebApp_ID" version="2.5">
    06 <display-name>justforfun</display-name>
    07 <welcome-file-list>
    08     <welcome-file>index.html</welcome-file>
    09 </welcome-file-list>
    10  
    11 <servlet>
    12     <servlet-name>springMVC</servlet-name>
    13     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    14     <init-param>
    15         <param-name>contextConfigLocation</param-name>
    16         <param-value>classpath*:/springMVC.xml</param-value>
    17     </init-param>
    18     <load-on-startup>1</load-on-startup>
    19 </servlet>
    20 <servlet-mapping>
    21     <servlet-name>springMVC</servlet-name>
    22     <url-pattern>/</url-pattern>
    23 </servlet-mapping>

    >springMVC.xml

    配置ftl config和viewResolver都是网上找的,直接copy过去,关键是mvc:resources,后面的location注意要放到可以被访问的地方(tomcat中WEB-INF下的东东貌似别人是访问不到的),spring的问档中说可以放在跟目录下或者神马著名的可被访问的目录(好像应该这么翻译)META-INF.

    01 <?xml version="1.0" encoding="UTF-8"?>
    02 <beans xmlns="http://www.springframework.org/schema/beans"
    03 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    04 xmlns:context="http://www.springframework.org/schema/context"
    05 xmlns:mvc="http://www.springframework.org/schema/mvc"
    06 xsi:schemaLocation="http://www.springframework.org/schema/beans  
    07 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    08 http://www.springframework.org/schema/tx  
    09 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
    10 http://www.springframework.org/schema/context 
    11 http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    12 http://www.springframework.org/schema/mvc 
    13 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    14  
    15  
    16 <!-- 自动扫描的包名 -->
    17 <context:component-scan base-package="com.app,com.core,JUnit4"></context:component-scan>
    18  
    19 <!-- 默认的注解映射的支持 -->
    20 <mvc:annotation-driven />
    21  
    22 <!-- freemarker config -->
    23 <bean id="freemarkerConfig"
    24     class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    25     <property name="templateLoaderPath" value="/WEB-INF/ftl/" />
    26 </bean>
    27  
    28 <!-- View resolvers can also be configured with ResourceBundles or XML files.
    29     If you need different view resolving based on Locale, you have to use the
    30     resource bundle resolver. -->
    31 <bean id="viewResolver"
    32 class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    33     <property name="cache" value="true" />
    34     <property name="prefix" value="" />
    35     <property name="suffix" value=".ftl" />
    36     <property name="contentType" value="text/html; charset=UTF-8"/> 
    37 </bean>
    38  
    39  
    40 <!-- 拦截器 -->
    41 <!-- <mvc:interceptors> <bean class="com.core.mvc.MyInteceptor" /> </mvc:interceptors> -->
    42 <!-- 对静态资源文件的访问 方案一 (二选一) -->
    43 <!-- <mvc:default-servlet-handler /> -->
    44  
    45 <!-- 对静态资源文件的访问 方案二 (二选一) -->
    46 <mvc:resources mapping="/images/**" location="/META-INF/resources/images/"
    47     cache-period="31556926" />
    48 <mvc:resources mapping="/js/**" location="/META-INF/resources/js/"
    49     cache-period="31556926" />
    50 <mvc:resources mapping="/css/**" location="/META-INF/resources/css/"
    51     cache-period="31556926" />

    >ftl文件

    导入spring.ftl是为了获得一个路径,@s.url,这个东东会自动加上你工程的名字,例如s.url '/css/default.css'/,会变成justforfun/css/default.css,千万注意!!!script一定不能写成<script 某某某 />,应该写成<script 某某某></script>,要不然这行后面的东西在html里面都解析不到了,被这个纠结了好久,不知道为神马,希望大牛看到能告知告知。

    01 <#import "spring.ftl" as s />
    02 <!DOCTYPE>
    03 <html>
    04 <head>
    05     <title>
    06         首页
    07     </title>
    08     <link rel="stylesheet" type="text/css" href="<@s.url '/css/default.css'/>"/>
    09     <script type="text/javascript" src="<@s.url '/js/jquery.min.js'/>">       
    10     </script>       
    11 </head>
    12 <body>
    13     <h1>
    14         首页的试炼
    15     </h1>
    16     <div>
    17        bingo!
    18        <img src = "<@s.url '/images/img1.jpg'/>"/>
    19     </div>
    20 </body>

    >spring的controller

    这个仅仅返回index就好了。

    01 package com.app.controller;
    02  
    03 import org.springframework.stereotype.Controller;
    04 import org.springframework.web.bind.annotation.RequestMapping;
    05 import org.springframework.web.bind.annotation.RequestMethod;
    06  
    07 @Controller
    08 @RequestMapping("/")
    09 public class IndexController {
    10  
    11 @RequestMapping(method=RequestMethod.GET)
    12 public String index(){
    13     return "index";
    14 }

    }

    >最终效果

    可以见到,图片,css,js,都没问题,而且因为springMVC里面设置了UTF-8,所以显示的是中文而不是乱码。 
    最终效果

    ps,增强你的效率vimer

    找到了一个老哥配置的gvim,非常不错,在eclipse里面可以用viplugin这个插件,在环境变量里面加入vim的路径,:vim即可直接启用vim编辑,美中不足的是没有加上command-T这个插件,因为用ruby,windows下各种不成功,可惜。linux下推荐janus这个vim插件管理器很好很强大,而且command-T也默认安装了,:help janus有很大惊喜 另外,用zsh吧,好好配置一下,它的tab功能比bash强好多。 
    http://www.oschina.net/code/snippet_574132_13357

    pps 为什么上面图片不显示???

    在此输入图片描述 
    在此输入图片描述

  • 相关阅读:
    js对象数组(JSON) 根据某个共同字段 分组
    一个 函数 用来转化esSearch 的range 条件
    关于 vuex 报错 Do not mutate vuex store state outside mutation handlers.
    android listview 重用view导致的选择混乱问题
    android SDK和ADT的更新
    Android中adb push和adb install的使用区别
    pycharm中添加扩展工具pylint
    su Authentication failure解决
    Putty以及adb网络调试
    有关android源码编译的几个问题
  • 原文地址:https://www.cnblogs.com/xhqgogogo/p/3673928.html
Copyright © 2011-2022 走看看