Jodd-madvoc Integration with beetl

original
2013/12/07 22:22
Reading number 928

Jodd-madvoc Is a very good web mvc framework. However, only jsp is used as the view technology, which obviously pulls the back of madvoc. This article will show you how to integrate betl into jodd.

Step 1: Configure web.xml: on the basis of madvoc, you need to add the initialization of beetl


 <filter> <filter-name>madvoc</filter-name> <filter-class>jodd.madvoc. MadvocServletFilter</filter-class> <init-param> <param-name>madvoc.webapp</param-name> <param-value> com.test. AppWebApplication</param-value> </init-param> </filter> <filter-mapping> <filter-name>madvoc</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> </filter-mapping>



Init param is a user-defined madvoc initialization class. We will complete the initialization of beetl and register an ActionResult to process view rendering


Step 2: Complete the AppWebApplication class:

 public class AppWebApplication extends WebApplication { protected void initResults(ResultsManager actionManager) { actionManager.register(new BeetlActionResult()); } public void configure(MadvocConfigurator configurator) { AutomagicMadvocConfigurator auto = (AutomagicMadvocConfigurator)configurator; auto.setExcludedJars("**/beetl.1.25.03.jar"); super.configure(configurator); } }




Reloading initResults will register a BeetlActionResult, which is used to initialize Beetl and render specific views. We will mention later

Overloaded Confgure, auto.setExcludedJars are used to let jodd not scan the beetl class, because the jodd configuration is based on the scan class to obtain the configuration information. Because beetl also integrates struts, spring and other mvc frameworks, it is better not to let it scan to avoid errors


The third step is to implement BeetlActionResult. Jodd provides the mechanism to allow the registration of view rendering classes, such as json, binary data, etc. The base class is ActionResult

 package com.test; import java.util.Enumeration; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import jodd.madvoc.ActionRequest; import jodd.madvoc.result.ActionResult; import org.bee.tl.core.GroupTemplate; import org.bee.tl.core.Template; import org.bee.tl.ext.WebConfig; import org.bee.tl.ext.WebPathKit; import org.bee.tl.ext.spring.SessionWrapper; import org.bee.tl.ext.spring.WebVariable; public class BeetlActionResult  extends ActionResult{ public static final String NAME = "beetl";	 GroupTemplate group = null; public BeetlActionResult() { super(NAME); try{ WebConfig config = new WebConfig(); config.updateRootPath(WebPathKit.getWebRootPath()); // Update Template Path group = config.createGroupTemplate(); group.setCharset("UTF-8"); }catch(Exception ex){ throw new RuntimeException(ex.getMessage()); } }	 @Override public void render(ActionRequest actionRequest, Object resultObject, String resultValue, String resultPath) throws Exception { HttpServletRequest request = actionRequest.getHttpServletRequest(); HttpServletResponse response = actionRequest.getHttpServletResponse(); response.setContentType("text/html;charset=UTF-8"); String target = resultPath; Template t = group.getFileTemplate(resultPath); Enumeration en = request.getAttributeNames(); while (en.hasMoreElements()) { String key = (String) en.nextElement(); t.set(key, request.getAttribute(key)); } WebVariable webVariable = new WebVariable(); webVariable.setRequest(request); webVariable.setResponse(response); webVariable.setSession(request.getSession()); t.set("servlet", webVariable); t.set("request", request); t.set("ctxPath", request.getContextPath()); t.setRawValue("session", new SessionWrapper(request.getSession())); t.getText(response.getWriter()); } }




Step 4, test:

Write a Controller:

 package com.test; import jodd.madvoc.meta.Action; import jodd.madvoc.meta.In; import jodd.madvoc.meta.MadvocAction; import jodd.madvoc.meta.Out; @MadvocAction public class IndexAction { @In String name; @Out String value; @Action("/index.html") public String world() { value = "Hello World! "; return "beetl:ok.html"; } }



According to the definition of Jodd, if you access/index.hmtl, the system will use its own world method, which returns a string beginning with beetl. We once registered this class, which is the above BeetlActionResult. Ok.html will be defined according to Jodd, and its view is/index.ok.html. Since Beetl's GroupTemplate does not have a template root directory configured, it defaults to Web inf/template as the root directory, and there is an index.ok.html file under its directory (of course, you can configure how to map to the view file according to Jodd). The content is very simple, as follows

 hello, China ${value}



I have put relevant codes and projects on git. If necessary, I can access https://github.com/javamonkey/jodd-madvoc-beetl.git
If you have any questions, you can ask questions through the beetl qq group



Expand to read the full text
Loading
Click to join the discussion 🔥 (6) Post and join the discussion 🔥
Reward
six comment
two Collection
zero fabulous
 Back to top
Top