spring mvc + freemarker 引入静态文件(css,img,js)

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

###>文件结构一览javascript

在此输入图片描述

###>jar包一览css

在此输入图片描述

###>web.xmlhtml

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

<!-- lang: 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/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>justforfun</display-name>
<welcome-file-list>
	<welcome-file>index.html</welcome-file>
</welcome-file-list>

<servlet>
	<servlet-name>springMVC</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:/springMVC.xml</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
	<servlet-name>springMVC</servlet-name>
	<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>jquery

###>springMVC.xmllinux

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

<!-- lang: xml -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans   
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
http://www.springframework.org/schema/tx   
http://www.springframework.org/schema/tx/spring-tx-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">


<!-- 自动扫描的包名 -->
<context:component-scan base-package="com.app,com.core,JUnit4"></context:component-scan>

<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />

<!-- freemarker config -->
<bean id="freemarkerConfig"
	class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
	<property name="templateLoaderPath" value="/WEB-INF/ftl/" />
</bean>

<!-- View resolvers can also be configured with ResourceBundles or XML files. 
	If you need different view resolving based on Locale, you have to use the 
	resource bundle resolver. -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
	<property name="cache" value="true" />
	<property name="prefix" value="" />
	<property name="suffix" value=".ftl" />
	<property name="contentType" value="text/html; charset=UTF-8"/>  
</bean>


<!-- 拦截器 -->
<!-- <mvc:interceptors> <bean class="com.core.mvc.MyInteceptor" /> </mvc:interceptors> -->
<!-- 对静态资源文件的访问 方案一 (二选一) -->
<!-- <mvc:default-servlet-handler /> -->

<!-- 对静态资源文件的访问 方案二 (二选一) -->
<mvc:resources mapping="/images/**" location="/META-INF/resources/images/"
	cache-period="31556926" />
<mvc:resources mapping="/js/**" location="/META-INF/resources/js/"
	cache-period="31556926" />
<mvc:resources mapping="/css/**" location="/META-INF/resources/css/"
	cache-period="31556926" />

</beans>spring

###>ftl文件vim

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

<!-- lang: html -->
<#import "spring.ftl" as s />
<!DOCTYPE>
<html>
<head>
    <title>
        首页
    </title>
    <link rel="stylesheet" type="text/css" href="<@s.url '/css/default.css'/>"/>
    <script type="text/javascript" src="<@s.url '/js/jquery.min.js'/>">        
    </script>        
</head>
<body>
    <h1>
        首页的试炼
    </h1>
    <div>
       bingo! 
       <img src = "<@s.url '/images/img1.jpg'/>"/>
    </div>
</body>

</html>

###>spring的controller

这个仅仅返回index就行了。

<!-- lang: java -->
package com.app.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/")
public class IndexController {

@RequestMapping(method=RequestMethod.GET)
public String index(){
    return "index";
}

}

###>最终效果

能够见到,图片,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 为何上面图片不显示??? 在此输入图片描述 在此输入图片描述

相关文章
相关标签/搜索