OFBiz实战(1):整合Groovy+FreeMaker

这是OFBiz实战系列的第一篇文件,该系列的目的是整合Groovy+FreeMaker开发一个图书管理系统,阐述在此过程中碰到的一系列问题,以及如何解决这些问题。第一篇文章说明如何使用Groovy+FreeMaker搭建基本的组件框架,并能够将数据从Groovy传到FreeMaker。|

1. 在hot-deploy目录下创建组件library基本目录结构。
OFBiz实战(1):整合Groovy+FreeMaker

2. 新建文件library/ofbiz-component.xml,编辑内容:

<?xml version="1.0" encoding="UTF-8"?>



<ofbiz-component name="library"

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/ofbiz-component.xsd">



    <resource-loader name="main" type="component"/>

    

    <webapp name="library"

        title="Library"

        server="default-server"

        location="webapp/library"

        base-permission="OFBTOOLS"

        mount-point="/library"

        app-bar-display="false"/>

        

</ofbiz-component>


3. 新建文件library/webapp/library/WEB-INF/web.xml,编辑内容:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">



<web-app>

    <display-name>Library</display-name>



    <context-param>

        <param-name>webSiteId</param-name>

        <param-value>LIBRARY</param-value>

        <description>A unique ID used to look up the WebSite entity</description>

    </context-param>

    <context-param>

        <param-name>localDispatcherName</param-name>

        <param-value>library</param-value>

        <description>A unique name used to identify/recognize the local dispatcher for the Service Engine</description>

    </context-param>

    <context-param>

        <param-name>entityDelegatorName</param-name>

        <param-value>default</param-value>

        <description>The Name of the Entity Delegator to use, defined in entityengine.xml</description>

    </context-param>

    <context-param>

        <param-name>compressHTML</param-name>

        <param-value>false</param-value>

        <description>Remove unnecessary whitespace from HTML output.</description>

    </context-param>



    <filter>

        <filter-name>ContextFilter</filter-name>

        <display-name>ContextFilter</display-name>

        <filter-class>org.ofbiz.webapp.control.ContextFilter</filter-class>

        <init-param><param-name>disableContextSecurity</param-name><param-value>N</param-value></init-param>

        <init-param>

            <param-name>allowedPaths</param-name>

            <param-value>/error:/control:/select:/index.html:/index.jsp:/default.html:/default.jsp:/images:/includes/maincss.css</param-value>

        </init-param>

        <init-param><param-name>errorCode</param-name><param-value>403</param-value></init-param>

        <init-param><param-name>redirectPath</param-name><param-value>/control/main</param-value></init-param>

    </filter>

    <filter-mapping><filter-name>ContextFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>



    <listener><listener-class>org.ofbiz.webapp.control.ControlEventListener</listener-class></listener>

    <listener><listener-class>org.ofbiz.webapp.control.LoginEventListener</listener-class></listener>



    <servlet>

        <servlet-name>ControlServlet</servlet-name>

        <display-name>ControlServlet</display-name>

        <description>Main Control Servlet</description>

        <servlet-class>org.ofbiz.webapp.control.ControlServlet</servlet-class>

        <load-on-startup>1</load-on-startup>

    </servlet>

    <servlet-mapping>

        <servlet-name>ControlServlet</servlet-name>

        <url-pattern>/control/*</url-pattern>

    </servlet-mapping>



    <session-config><session-timeout>60</session-timeout><!-- in minutes --></session-config>



    <welcome-file-list>

        <welcome-file>index.jsp</welcome-file>

        <welcome-file>index.html</welcome-file>

        <welcome-file>index.htm</welcome-file>

    </welcome-file-list>

</web-app>


4. 新建文件library/webapp/library/WEB-INF/controller.xml,编辑内容:

<?xml version="1.0" encoding="UTF-8"?>



<site-conf xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/site-conf.xsd">



    <include location="component://common/webcommon/WEB-INF/common-controller.xml"/>

    <include location="component://common/webcommon/WEB-INF/portal-controller.xml"/>



    <request-map uri="main">

        <event type="groovy" path="component://library/webapp/library/WEB-INF/actions/main.groovy"/>

        <response name="success" type="view" value="main"/>

    </request-map>

    

    <view-map name="main" type="ftl" page="component://library/webapp/library/main.ftl"/>



</site-conf>

 

5. 新建文件library/webapp/library/WEB-INF/actions/main.groovy,编辑内容:

request.setAttribute("name", "奥特曼");



def books = [];

books[0] = ["name": "Java入门", "price": 85];

books[1] = ["name": "Java实战", "price": 70];

books[2] = ["name": "Java中级", "price": 63];

books[3] = ["name": "Java高级", "price": 81];

request.setAttribute("books", books);



return "success";


6. 新建文件library/webapp/library/main.ftl,编辑内容:

你的名字:<b>${requestAttributes.name}</b>



<table border='1'>

    <#list requestAttributes.books as book>

    <tr>

        <td>${book.name}</td>

        <td>${book.price?if_exists}</td>

    </tr>

    </#list>

</table>


7. 启动OFBiz,浏览http://localhost:8080/library/control/main,看到如下的结果:
OFBiz实战(1):整合Groovy+FreeMaker

有遗憾,为什么FreeMaker中每个变量都要带上requestAttributes,太难看了?下一篇文件想办法解决。

你可能感兴趣的:(freemaker)