最近從事的工作是web方面的,主要j2ee,spring jsp這些內容,由于剛入門,很多的技術都不了解。所謂初生牛犢不怕虎,剛入門,各種不順手,比如寫jsp,總是重復很多的代碼,各種不爽,然后就去看jsp的模板技術,
看來看去也是各種不爽,于是就有了自己寫一個工具的想法。
ok,廢話一堆。先說說jsp模板,最簡單的include,成熟的解決有tiles,還有什么setmesh,當日還有更強大的什么fremaker,volocity(是這樣寫的嗎,記不清楚)等等。
include不說,實在太簡陋,tiles其實是很不錯的,不是很重,學習成本還算好,用的也比較廣吧,當然要說廣,肯定有后面那幾個了。可是這些對我來說,都太重。我要的是什么樣的?我記得rails里面的模板,大概就是layout里面寫一句yield就ok了。
沒錯,這就是我想要的。寫好layout,把什么header、menu什么的搞定,內容里面一個div套著一個yield,你就可以專注于你的子視圖了,框架自動把layout應用到你的視圖上去。
就這個簡單的功能,為什么要那么復雜的框架、插件呢?
不過jsp,看看好像還真沒有。那就自己實現吧,反正我是初入門,什么都不知道,不知者無畏嘛。
屁話一堆,總算到該說些正題了。
那么,怎么實現呢?我的思路是,layout里面寫好布局后,正文部分就用${yield}代替,
當請求子視圖的時候,自動的將子視圖插入到layout中,替換${yield},然后生成一個新的合成視圖,存入緩存文件中,由新的視圖相應前端的請求。
我們知道spring里面有個配置viewresolver的類,可以在有請求的時候,查找視圖,構建視圖,并返回給前端。我們就在這里做我們的事情。
我們繼承一個InternalResourceViewResolver,該resolver會根據視圖查找相應的InternalResourceView來處理jsp視圖。InternalResourceViewResolver有一個buildView的調用,該調用根據視圖名稱構建view。
我們就重載這個方法,根據前面的思路,首先找到模板文件和子視圖,然后替換掉模板文件中的${yield},寫入到新的jsp文件,然后重定向到該文件,就ok了。具體看代碼:
1 package lin.layoutsample.framework; 2 3 import lin.layoutsample.utils.FileUtils; 4 import lin.layoutsample.utils.StringUtils; 5 6 import org.springframework.web.servlet.view.AbstractUrlBasedView; 7 import org.springframework.web.servlet.view.InternalResourceViewResolver; 8 9 public class LayoutViewResolver extends InternalResourceViewResolver {10 11 private static String DEFUALT_YIELD_CMD = "${yield}";12 private static String DEFUALT_LAYOUT_TEMP_DIR = "/WEB-INF/layout/layout_temp";13 private static String DEFUALT_LAYOUT = "/WEB-INF/layout/layout.jsp";14 15 /**16 * layout文件。默認為/WEB-INF/layout/layout.jsp。17 * */18 private String layout = DEFUALT_LAYOUT;19 20 /**21 * 經過替換后的視圖緩存目錄,默認為/WEB-INF/layout/layout_temp。22 * */23 private String temp_path = DEFUALT_LAYOUT_TEMP_DIR;24 25 /**26 * 默認為${yield}。模板中該命令會被子視圖替換。類似rails的yield。27 * */28 private String yield_cmd = DEFUALT_YIELD_CMD;29 30 /**31 * 32 * 函數首先根據指定的view在緩存區生成對應的文件,然后將模板文件(默認為layout.jsp)中的${yield}替換成視圖的內容。33 * 再調用超類的buildview函數生成新的被視圖并返回。這樣就完成了模板的替換和重定向到新生成的視圖。34 * */35 @Override36 protected AbstractUrlBasedView buildView(String viewName) throws Exception {37 String url = getPrefix() + viewName + getSuffix();38 String target_view_real_path = super.getServletContext().getRealPath(39 getTemp_path())40 + url;41 FileUtils.createFile(target_view_real_path);42 String layout_real_path = super.getServletContext().getRealPath(43 getLayout());44 String layout_content = FileUtils.read(layout_real_path);45 String view_content = FileUtils.read(super.getServletContext()46 .getRealPath(url));47 String result_view_content = StringUtils.replace(layout_content, "http://Q"48 + getYield_cmd() + "http://E", view_content);49 FileUtils.write(target_view_real_path, result_view_content);50 51 AbstractUrlBasedView view = super.buildView(getTemp_path() + getPrefix()+ viewName);52 return view;53 }54 55 public String getLayout() {56 return layout;57 }58 59 public void setLayout(String layout) {60 this.layout = layout;61 }62 63 public String getTemp_path() {64 return temp_path;65 }66 67 public void setTemp_path(String temp_path) {68 this.temp_path = temp_path;69 }70 71 public String getYield_cmd() {72 return yield_cmd;73 }74 75 public void setYield_cmd(String yield_cmd) {76 this.yield_cmd = yield_cmd;77 }78 }是不是很簡單?只需要一個方法,然后就是在web的配置里面將默認的InternalResourceViewResolver 替換成我們的就可以了。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" 6 xmlns:security="http://www.springframework.org/schema/security" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/mvc 12 http://www.springframework.org/schema/mvc/spring-mvc.xsd13 http://www.directwebremoting.org/schema/spring-dwr 14 http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd15 http://www.springframework.org/schema/security 16 http://www.springframework.org/schema/security/spring-security-3.2.xsd17 ">18 19 <!-- 自動掃描controller包下的所有類,使其認為spring mvc的控制器 -->20 <context:component-scan base-package="lin.layoutsample.controller" />21 22 23 <!-- 通過注解,把URL映射到Controller上,該標簽默認注冊DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->24 <mvc:annotation-driven />25 26 27 <!-- 對模型視圖名稱的解析,即在模型視圖名稱添加前后綴 -->28 <bean class="lin.layoutsample.framework.LayoutViewResolver">29 <property name="prefix" value="/" />30 <property name="suffix" value=".jsp" />31 <property name="layout" value="/WEB-INF/layout/layout.jsp" />32 </bean>33 </beans>
這里我們可以配置我們的模板文件(一般叫layout.jsp)的路徑,當然你也可以不用${yield}來占位,你可以自己配置成屬性就行了。
完整的layout.jsp文件如下,路徑我一般放在/WEB-INF/layout/下面。
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 4 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 5 6 <!DOCTYPE html> 7 <html> 8 <% 9 String path = request.getContextPath();10 String basePath = request.getScheme() + "://"11 + request.getServerName() + ":" + request.getServerPort()12 + path + "/";13 %>14 <base href="<%=basePath%>">15 <head>16 <%@include file="/WEB-INF/layout/_include.jsp"%>17 <title>花花世界</title>18 </head>19 <body>20 <%@include file="/WEB-INF/layout/_header.jsp"%>21 22 <div class="container-fluid">23 <div class="row">24 <%@ include file="/WEB-INF/layout/_menu.jsp"%>25 <div class="page-content">26 <div class="container-fluid" data-role="main">27 <!-- Main component for a primary marketing message or call to action -->28 29 ${yield}30 31 </div>32 <!-- container-fluid -->33 </div>34 <!-- page-content -->35 </div>36 37 38 </div>39 <!-- /container -->40 41 <%@include file="/WEB-INF/layout/_import.jsp"%>42 </body>43 </html>
子視圖就很簡單了只需要一行示例代碼就行了,文件say-hi.jsp如下。
1 <h2>hi world!!</h2>
我們的控制器完全不需要考慮模板的事情,新建一個子視圖返回就行了。
1 package lin.layoutsample.controller; 2 3 import org.slf4j.Logger; 4 import org.slf4j.LoggerFactory; 5 import org.springframework.stereotype.Controller; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.servlet.ModelAndView; 8 9 10 @Controller11 @RequestMapping("/hello")12 public class HelloController {13 private Logger logger = LoggerFactory.getLogger(HelloController.class);14 15 @RequestMapping("/say-hi")16 public ModelAndView sayHi(){17 logger.debug("HelloController:sayHi");18 ModelAndView mv = new ModelAndView("WEB-INF/views/say-hi");19 return mv;20 }21 }效果如下圖:

是不是很簡單,對開發者來說,只需要配置一下,別的都不需要關心,就可以完成模板布局的應用了。
不過我是初學,入門,哪些商業的模板工具自然有他得用武之地,我這個超輕量級的模板解決方案,寫寫小網頁還是足夠了。
有興趣的tx可以去github上下載我的代碼https://github.com/linbirg/wish。
歡迎大神批評指正。
新聞熱點
疑難解答