OFBiz进阶之HelloWorld(一)创建热部署模块
创建热部署模块
参考文档 https://cwiki.apache.org/confluence/display/OFBIZ/OFBiz+Tutorial+-+A+Beginners+Development+Guide
In this part, you will learn how to create and load your own custom component and add first screen that shows “This is practice application”
1 在目录hot-deploy下创建目录practice(即为要创建的模块名称)
2 在目录hot-deploy/practice下创建文件ofbiz-component.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <ofbiz-component name="practice" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/ofbiz-component.xsd"> 5 <resource-loader name="main" type="component"/> 6 <webapp name="practice" 7 title="Practice" 8 server="default-server" 9 base-permission="OFBTOOLS" 10 location="webapp/practice" 11 mount-point="/practice" 12 app-bar-display="false"/> 13 </ofbiz-component>
2.1 ofbiz-component.xml文件说明:
a. ofbiz通过ofbiz-component.xml文件识别此模块
b. 节点(resource-loader name)可以灵活设置,此处设置为main。type告诉ofbiz我们要加载一个component
1 <resource-loader name="main" type="component"/>
c. 节点webapp有很多属性,通常如下:
1 <webapp name="practice" 2 title="Practice" 3 server="default-server" 4 base-permission="OFBTOOLS" 5 location="webapp/practice" 6 mount-point="/practice" 7 app-bar-display="false"/>
name : web应用名称。
title : 应用的标题,将被展示在顶部导航中。
server : ofbiz通过此处值知道使用的是什么server。
base-permission :
location : 默认跟路径
mount-point : 访问此服务的URL,本例中应为:localhost:8080/practice。
app-bar-display :
3 创建 web app
3.1 创建webapp文件夹
在目录hot-deploy/practice下创建文件夹webapp,这个目录下包括模块的所有webapp相关文件。
webapp下内容遵循J2EE webapp标准。
3.2 创建practice文件夹
在目录hot-deploy/practice/webapp下创建文件夹practice(为webapp名称)。
注意:一个模块可以有多个webapp,例如marketing模块有两个webappp(marketing和sfa);
3.3 创建WEB-INF文件夹
在目录hot-deploy/practice/webapp/practice下创建WEB-INF文件夹:
一个OFBiz web application 需要放置两个配置文件(controller.xml 和 web.xml )。
a. controller.xml : 告诉OFBiz一个请求过来该如何处理,通过哪个action去处理,通过那个page去渲染。
b. web.xml : 告诉OFBiz 此web applation 可用的资源(database and business logic access),怎样去处理web相关问题,比如欢迎页面、重定向、错误页面等。
3.4 创建web.xml(web.xml遵循J2EE的webapp规格)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> 3 <web-app> 4 <context-param> 5 <param-name>webSiteId</param-name> 6 <param-value>PRACTICE</param-value> 7 <description>A unique ID used to look up the WebSite entity to get 8 information about catalogs, etc.</description> 9 </context-param> 10 <context-param> 11 <param-name>localDispatcherName</param-name> 12 <param-value>practice</param-value> 13 <description>A unique name used to identify/recognize the local 14 dispatcher for the Service Engine</description> 15 </context-param> 16 <context-param> 17 <param-name>mainDecoratorLocation</param-name> 18 <param-value>component://practice/widget/PracticeScreens.xml</param-value> 19 <!-- change the path to the following if the above doesn't work for you --> 20 <!-- <param-value>component://practice/webapp/practice/widget/PracticeScreens.xml</param-value> --> 21 <description>The location of the main-decorator screen to use for this 22 webapp; referred to as a context variable in screen def XML files.</description> 23 </context-param> 24 25 <filter> 26 <filter-name>ContextFilter</filter-name> 27 <display-name>ContextFilter</display-name> 28 <filter-class>org.ofbiz.webapp.control.ContextFilter</filter-class> 29 <init-param><param-name>disableContextSecurity</param-name><param-value>N</param-value></init-param> 30 <init-param> 31 <param-name>allowedPaths</param-name> 32 <param-value>/error:/control:/select:/index.html:/index.jsp:/default.html:/default.jsp:/images:/includes/maincss.css</param-value> 33 </init-param> 34 <init-param> 35 <param-name>errorCode</param-name> 36 <param-value>403</param-value> 37 </init-param> 38 <init-param> 39 <param-name>redirectPath</param-name> 40 <param-value>/control/main</param-value> 41 </init-param> 42 </filter> 43 <filter-mapping> 44 <filter-name>ContextFilter</filter-name> 45 <url-pattern>/*</url-pattern> 46 </filter-mapping> 47 48 <servlet> 49 <servlet-name>ControlServlet</servlet-name> 50 <display-name>ControlServlet</display-name> 51 <description>Main Control Servlet</description> 52 <servlet-class>org.ofbiz.webapp.control.ControlServlet</servlet-class> 53 <load-on-startup>1</load-on-startup> 54 </servlet> 55 <servlet-mapping> 56 <servlet-name>ControlServlet</servlet-name> 57 <url-pattern>/control/*</url-pattern> 58 </servlet-mapping> 59 60 <session-config> 61 <session-timeout>60</session-timeout><!-- in minutes --> 62 </session-config> 63 64 65 <welcome-file-list> 66 <welcome-file>index.jsp</welcome-file> 67 <welcome-file>index.html</welcome-file> 68 <welcome-file>index.htm</welcome-file> 69 </welcome-file-list> 70 </web-app>
3.5 创建controller.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <site-conf xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/site-conf.xsd"> 4 <include location="component://common/webcommon/WEB-INF/common-controller.xml"/> 5 <description>Practice Component Site Configuration File</description> 6 <owner>Copyright 2001-2009 The Apache Software Foundation</owner> 7 <handler name="screen" type="view" class="org.ofbiz.widget.screen.ScreenWidgetViewHandler"/> 8 <!-- Request Mappings --> 9 <request-map uri="main"> 10 <security https="false" auth="false"/> 11 <response name="success" type="view" value="main"/> 12 </request-map> 13 <!-- end of request mappings --> 14 <!-- View Mappings --> 15 <view-map name="main" type="screen" page="component://practice/widget/PracticeScreens.xml#main"/> 16 <!-- change the path to the following if the above doesn't work for you --> 17 <!-- <view-map name="main" type="screen" page="component://practice/webapp/practice/widget/PracticeScreens.xml#main"/> --> 18 19 <!-- end of view mappings --> 20 </site-conf>
3.6 创建error文件夹
在目录hot-deploy/practice/webapp/practice下创建文件夹error,并且在此文件夹创建error.jsp
3.7 创建widget文件夹
在目录hot-deploy/practice下创建文件夹widget,并且创建PracticeScreens.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <screens xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/widget-screen.xsd"> 4 <screen name="main"> 5 <section> 6 <widgets> 7 <label text="This is first practice"/> 8 </widgets> 9 </section> 10 </screen> 11 </screens>