1 |
剛開始搞spring mvc和freemarker,遇到了很多問題,首先是導入靜態文件,記錄一下,但願能幫助親們少走彎路吧。 |
2 |
本章不介紹freemarker的神馬複雜用法(由於我也不會),也不講解spring mvc的種種,僅僅關注兩者結合後靜態文件的導入問題,和最初的一些配置。 |
>文件結構一覽

>jar包一覽

>web.xml
關鍵一句話,就是springmvc的那個servlet,由於要用RESTFul風格,因此攔截/,而且讓它去讀springMVC來初始化(java /src 根路徑)。html
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 > |
08 |
< welcome-file >index.html</ welcome-file > |
12 |
< servlet-name >springMVC</ servlet-name > |
13 |
< servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > |
15 |
< param-name >contextConfigLocation</ param-name > |
16 |
< param-value >classpath*:/springMVC.xml</ param-value > |
18 |
< load-on-startup >1</ load-on-startup > |
21 |
< servlet-name >springMVC</ servlet-name > |
22 |
< url-pattern >/</ url-pattern > |
>springMVC.xml
配置ftl config和viewResolver都是網上找的,直接copy過去,關鍵是mvc:resources,後面的location注意要放到能夠被訪問的地方(tomcat中WEB-INF下的東東貌似別人是訪問不到的),spring的問檔中說能夠放在跟目錄下或者神馬著名的可被訪問的目錄(好像應該這麼翻譯)META-INF.java
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"> |
17 |
< context:component-scan base-package = "com.app,com.core,JUnit4" ></ context:component-scan > |
20 |
< mvc:annotation-driven /> |
23 |
< bean id = "freemarkerConfig" |
24 |
class = "org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer" > |
25 |
< property name = "templateLoaderPath" value = "/WEB-INF/ftl/" /> |
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" /> |
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裏面都解析不到了,被這個糾結了很久,不知道爲神馬,但願大牛看到能告知告知。jquery
01 |
<#import "spring.ftl" as s /> |
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'/>"> |
18 |
< img src = "<@s.url '/images/img1.jpg'/>"/> |
>spring的controller
這個僅僅返回index就行了。linux
01 |
package com.app.controller; |
03 |
import org.springframework.stereotype.Controller; |
04 |
import org.springframework.web.bind.annotation.RequestMapping; |
05 |
import org.springframework.web.bind.annotation.RequestMethod; |
09 |
public class IndexController { |
11 |
@RequestMapping (method=RequestMethod.GET) |
12 |
public String index(){ |
}web
>最終效果
能夠見到,圖片,css,js,都沒問題,並且由於springMVC裏面設置了UTF-8,因此顯示的是中文而不是亂碼。
spring
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_13357vim
pps 爲何上面圖片不顯示???
