昨天熟悉了一下nutz框架感觉不错,体积很小而且学习的门槛低,不像ssh2那样臃肿,但是他默认提供的视图不支持freemarker。
我是freemarker用久了已经离不开了,所以就按照它提供的视图接口做了个插件。
代码的一部分是参照struts2写的。
[b]安装[/b]
1.把插件jar包和freemarker包放在lib目录下。
2.把freemarker的配置文件freemarker.properties放在classes目录下(不放这个文件会出错)
3.在主入口函数上加:@Views({FreemarkerViewMaker.class}) 注释
@Modules( { HostAction.class }) @IocBy(type = JsonIocProvider.class, args = { "ioc/dao.js"}) @SetupBy(MvcSetup.class) @Views({FreemarkerViewMaker.class}) @Localization("msg") @Fail("json") public class MainModule {}
4.如果想让freemarker支持jsp标签就在web.xml中放上一个servlet。这点和struts2设置是一样的。
<servlet> <servlet-name>JspSupportServlet</servlet-name> <servlet-class> org.nutz.mvc.view.JspSupportServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet>
到此就大功告成了。
[b]使用[/b]
转向@Ok("jsp:html.info")注释就可以改成@Ok("fm:/WEB-INF/html/info.html")。
@At //@Ok("jsp:html.info") @Ok("fm:/WEB-INF/html/info.html") public String list(@Param("word") String word,HttpServletRequest request){ if(word!=null) return word; return "测试"; }
至于它的本地化字符串直接在模板中 ${Session["msg"].save} 取即可。
[b]例子[/b]
模板代码:
[#assign fck=JspTaglibs["/WEB-INF/FCKeditor.tld"]]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>info.html</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> </head> <body> <!--fckeditor编辑器start--> [@fck.editor id="content" basePath="${base}/jfeditor/" width="800" height="300" toolbarSet="edit"]${obj}[/@fck.editor] <!--fckeditor编辑器end--> <input type="submit" value="${Session["msg"].save}" /> <input type="reset" value="${Session["msg"].reset}" /> </body></html>
[b]效果[/b]
[img]http://dl.iteye.com/upload/attachment/176741/24418f85-ae61-330c-89c7-ce48045f8160.jpg[/img]
[b]2009-12-16更新[/b]
更新个小的地方,模板中request取值直接可以${key}而不用${Request.key}。
相关资源:JAVA上百实例源码以及开源项目源代码