Jodd-madvoc 是一个很不错的web mvc框架。但只使用了jsp作为视图技术,这显然拉了madvoc的后腿。本文将告诉你如何将beetl集成到jodd上。
第一步:配置web.xml:在基于madvoc的基础配置上,需要增加beetl的初始化工作
1<filter> 2 <filter-name>madvoc</filter-name> 3 <filter-class>jodd.madvoc.MadvocServletFilter</filter-class> 4 <init-param> 5 <param-name>madvoc.webapp</param-name> 6 <param-value> com.test.AppWebApplication</param-value> 7 </init-param> 8 </filter> 9 <filter-mapping> 10 <filter-name>madvoc</filter-name> 11 <url-pattern>/*</url-pattern> 12 <dispatcher>REQUEST</dispatcher> 13 </filter-mapping>
init-param 是自定义一个madvoc 的初始化类,我们将完成初始化beetl,以及注册一个ActionResult来处理视图渲染工作
第二步: 完成AppWebApplication类:
1public class AppWebApplication extends WebApplication { 2 protected void initResults(ResultsManager actionManager) { 3 actionManager.register(new BeetlActionResult()); 4 } 5 6 public void configure(MadvocConfigurator configurator) { 7 AutomagicMadvocConfigurator auto = (AutomagicMadvocConfigurator)configurator; 8 auto.setExcludedJars("**/beetl.1.25.03.jar"); 9 super.configure(configurator); 10 } 11 12}
重载了initResults 将注册一个BeetlActionResult,这是用来初始化Beetl,以及负责渲染具体视图,我们稍后会提到
重载了confgure ,auto.setExcludedJars 用于让jodd别扫描beetl类,因为jodd配置是基于扫描类来获取配置信息的。因为beetl也集成struts,spring等mvc框架,还是别让他扫描,以免出错
第三步,实现BeetlActionResult, jodd提供机制允许注册视图渲染类,如json,二进制数据等,基类是ActionResult
1package com.test; 2import java.util.Enumeration; 3 4import javax.servlet.http.HttpServletRequest; 5import javax.servlet.http.HttpServletResponse; 6 7import jodd.madvoc.ActionRequest; 8import jodd.madvoc.result.ActionResult; 9 10import org.bee.tl.core.GroupTemplate; 11import org.bee.tl.core.Template; 12import org.bee.tl.ext.WebConfig; 13import org.bee.tl.ext.WebPathKit; 14import org.bee.tl.ext.spring.SessionWrapper; 15import org.bee.tl.ext.spring.WebVariable; 16 17public class BeetlActionResult extends ActionResult{ 18 19 public static final String NAME = "beetl"; 20 GroupTemplate group = null; 21 public BeetlActionResult() { 22 super(NAME); 23 24 try{ 25 26 WebConfig config = new WebConfig(); 27 config.updateRootPath(WebPathKit.getWebRootPath()); //更新模版路径 28 group = config.createGroupTemplate(); 29 group.setCharset("UTF-8"); 30 31 }catch(Exception ex){ 32 throw new RuntimeException(ex.getMessage()); 33 } 34 } 35 36 37 @Override 38 public void render(ActionRequest actionRequest, Object resultObject, String resultValue, String resultPath) throws Exception { 39 40 HttpServletRequest request = actionRequest.getHttpServletRequest(); 41 HttpServletResponse response = actionRequest.getHttpServletResponse(); 42 response.setContentType("text/html;charset=UTF-8"); 43 String target = resultPath; 44 45 46 Template t = group.getFileTemplate(resultPath); 47 Enumeration en = request.getAttributeNames(); 48 while (en.hasMoreElements()) 49 { 50 String key = (String) en.nextElement(); 51 t.set(key, request.getAttribute(key)); 52 } 53 54 WebVariable webVariable = new WebVariable(); 55 webVariable.setRequest(request); 56 webVariable.setResponse(response); 57 webVariable.setSession(request.getSession()); 58 59 t.set("servlet", webVariable); 60 t.set("request", request); 61 t.set("ctxPath", request.getContextPath()); 62 t.setRawValue("session", new SessionWrapper(request.getSession())); 63 t.getText(response.getWriter()); 64 65 66 } 67 68}
第四步,测试一下:
写一个Controller:
1package com.test; 2import jodd.madvoc.meta.Action; 3import jodd.madvoc.meta.In; 4import jodd.madvoc.meta.MadvocAction; 5import jodd.madvoc.meta.Out; 6 7@MadvocAction 8public class IndexAction { 9 10 @In 11 String name; 12 13 @Out 14 String value; 15 16 @Action("/index.html") 17 public String world() { 18 19 value = "Hello World!"; 20 return "beetl:ok.html"; 21 } 22}
按照Jodd定义,如果访问/index.hmtl,系统将自行world方法,其返回一个字符串以beetl开头,我们曾经注册个这个类,就是上面的BeetlActionResult。ok.html将根据Jodd定义,其视图是/index.ok.html,由于Beetl的GroupTemplate没有配置模板根目录,因此默认是Web-inf/template作为根目录,其目录下有一个index.ok.html文件(当然,你可以根据jodd配置如何映射到视图文件),内容非常简单,如下
hello,中国 ${value}
我已经将相关代码和工程放到git上,如果需要,可以访问 https://github.com/javamonkey/jodd-madvoc-beetl.git
有任何疑问,可以通过beetl qq群提问