Jodd

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群提问

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Jodd - HelloWorld