WEBX 入门.pptx_第1页
WEBX 入门.pptx_第2页
WEBX 入门.pptx_第3页
WEBX 入门.pptx_第4页
WEBX 入门.pptx_第5页
已阅读5页,还剩76页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、WEBX 入门,业务平台 索尼,课程简介,WEBX与淘宝 WEB框架的本质 WEBX基础教程 WEBX进阶教程 WEBX高级特性,WEBX发展历程,WEBX 1.0 基于 Turbine WEBX 2.0 延续Turbine风格,重写框架 WEBX 3.0 基于Spring扩展而成,淘宝技术一览,淘宝技术体系,淘宝的技术体系 WEBX + Spring + iBatis AO + Manager(BO/Service) + DAO 具体项目 Detail/Sell/Buy 。 有页面的地方就有WEBX,WEBX是什么?,一个MVC框架,WEB框架的本质,为了解决问题 MVC框架需要解决哪些问题

2、? 根据请求路由不同的业务逻辑 渲染页面 参数采集 文件上传 表单验证 国际化 其它,开源的MVC框架,开源的MVC框架有哪些? Spring MVC Struts Struts2 WebWork JSF Turbine,WEBX的特色,页面驱动 先建模板、再建程序 模板中“按需”取得工具对象(pull tools) 约定胜于配置 基于规则展示页面 screen/layout/control,一个简单的WEBX项目,使用MVN创建一个简单的WEBX项目 mvn archetype:generate,页面布局,WebX框架基础,Turbine风格的页面布局,分为四种类型Module:contro

3、l、layout、screen 和 action 实际应用中,大部分的页面都是由两部分构成:模板和Java模块:,Turbine风格的目录结构,实际案例:一个简单的页面,这种页面是怎么展现出来的呢?,实际案例:一个简单的页面,用户输入URL: http:/localhost:8081/index.htm 分析URL取得target: /index.vm 根据target查找screen模板: /screen/index.vm 根据target查找screen模块的类: xxx.module.screen.Index(如果没有找到该类) xxx.module.screen.Default(如果没

4、有找到该类) - 如果还是没有找到就按默认类渲染页面 执行screen类,并渲染screen模板 根据target查找layout模板: /layout/index.vm(找不到) /layout/default.vm(找到) 渲染layout模板 渲染在layout模板中引用的一个control: menu.vm 查找并渲染/control/menu.vm (control可以有对应的类),如何找到些类?,webx-sample.xml, ,访问无模板的screen,假设有下面的URL(注意后缀): http:/localhost:8080/index.do 那么WebX将不会查找index

5、.vm这个模板,而是直接执行screen: xxx.module.screen.Index 什么时候要使用这种URL呢? 不使用模板的情形 模板只是一种文本生成技术,除此之外,还有其它技术。在某些情形下,使用模板不一定是最好的方法。 不需要返回可见的页面的情形 例如一个被机器回调的URL。 重定向到另一个页面的情形 有时一个页面自身不显示内容,而是重定向(内部/外部)到另一个页面。例如:支付宝的商家工具。,WebX框架基础,Turbine Modules,Turbine Modules,Modules是基本编程模块: Screen 用来处理页面显示逻辑的module Control 和scre

6、en类似,但可以被别的screen或layout引用, Action 处理用户提交表单的module 所有module都是一些普通的类: public class Module public void execute(Context context); 需要有一个public void execute()方法,Screen Module的写法,Screen的功能就是显示一个页面,最简单的screen可以这样写: public class SimpleScreen Autowired HttpServletResponse response; public void execute() thro

7、ws WebxException PrintWriter out = response.getWriter(); out.println(hello, world); 但这种写法并不常用,99%的screen使用了模板技术: public class MyTemplateScreen public void execute(Param(name”) String name , Context context) context.put(hello, world); 模板文件myTemplateScreen.vm可以这样写: Hello, $hello 显示结果:hello, world,Cont

8、rol Module的写法,Control和screen的写法完全类似: public class Menu public void execute(Context context) context.put(webName, My Sample Web); Control支持模板技术: Welcome to : $webName Home | 淘宝网 ,Action Module的写法,Action是用来处理用户提交的表单的 $csrfToken.hiddenField #set ($group = $form.simple.defaultInstance) Hello, whats your

9、 name? #if (!$.valid) $.message #end $message: ,Action Module的写法,程序这样写: public void doGreeting(FormGroup(simple) SimpleObject simple, Navigator nav) String name = simple.getName(); nav.redirectTo(sampleLink).withTarget(hello) .withParameter(name, name); 每个action可以处理多个事件,例如:doGree

10、ting、doAdd、doDelete等等。执行那个全凭用户按下哪个HTML按钮。如果一切都不匹配,则抛出找不到。,参数获取,WebX框架基础,哪些参数可以被注入?,一般性注入:Spring支持的注入方式,webx均支持:Autowired注入等。 特殊注入:如request, response, session等。 参数注入:通过data-resolver服务使得一些基于request的数据可以参数的方式注入到screen/action/control中。,获取参数示例,注入示例 public class DataResolverAction Autowired HttpServletReq

11、uest request; Autowired HttpServletResponse response; Autowired HttpSession session; public void doGetNothing(Context context,Navigator navigator) public void doGetParam(Param(name) String name) public void doGetGroup(FormGroup(myGroup) Group group) public void doGetFrom(FormData Form Form) public v

12、oid doGetField(FormField(name = field1, group = myGroup1 , skipIfInvalid = true) Field field) public void doGetFields(FormFields(name = field1, group = myGroup1) Field fields) 参数:skipIfInvalid 假如表单未验证通过,则跳过模块的执行。默认为true只对action模块有效。,如何实现这些参数注入?,webx-component.xml, ,负责注入的参数: TurbineRunData HttpServle

13、tRequest HttpServletResponse HttpSession ServletContext ParameterParser CookieParser Context,具体代码示例,具体代码示例 void doGetRundata(TurbineRunData rundata) void doGetNavigator(Navigator nav) void doGetContext(Context context) void doGetRequestContext(RequestContext requestContext),用来标识一个参数,使之从request 中取得值

14、代码示例: void doGetIntDefault(Param(name = aaa, defaultValue = 123) Integer i) void doGetIntArray(Param(aaa) int i) Void oGetIntegerArrayDefault(Param(name = aaa, defaultValues = “1, “2 ) Integer i) void doGetIntegerList(Param(“n“) List i) void doSetData(Params MyData data),获取Form数据 代码示例: void doGetFor

15、mNoAnnotation(Form form) void doGetFormDefaultAnnotation(FormData Form form) void doGetFormDontSkipAction(FormData(skipIfInvalid = false) Form form),获取Group里面的数据 代码示例: void doGetGroup(FormGroup(myGroup1) Group group) void doGetGroupInstanceBean(FormGroup(name = myGroup1, instanceKey = aaa) MyData da

16、ta) void doGetGroups(FormGroups(myGroup1) Group groups) void doGetGroupsBeans(FormGroups(myGroup1) MyData data) void doGetGroupsBeansDontSkipAction(FormGroups(name = myGroup1, skipIfInvalid = false)Collection data),获取某一个Field里面的数据 代码示例: void doGetField(FormField(name = field1, group = myGroup1) Fiel

17、d field) void doGetFieldDontSkipAction(FormField(name = field1, group = myGroup1, skipIfInvalid =false) void doGetFieldInstanceValue(FormField(name = field1,group = myGroup1, groupInstanceKey = aaa) String s) void doGetFieldInstanceValueDontSkipAction(FormField(name = field2, group = myGroup1,groupI

18、nstanceKey = aaa, skipIfInvalid = false) Integer i),获取某多个Fields里面的数据 代码示例: void doGetFields(FormFields(name = field1, group = myGroup1) Field fields) void doGetFieldsBeans(FormFields(name = field1, group = myGroup1) int data) void doGetFieldsBeans2(FormFields(name = field1, group = myGroup1) int dat

19、a) void doGetFieldsBeansDontSkipAction(FormFields(name = field1, group = myGroup1, skipIfInvalid= false) List data),扩展DataResolver,扩展思路 添加一个Annotation注解,表明是你的数据类型 添加一个ResolverFactory (implements DataResolverFactory ) 添加一个Resolver (implements DataResolver ) 配置文件 扩展阅读 ,文件上传,框架已集成配置下就搞定 如何获取 跟普通的参数获取方式

20、一样 使用apached的commons-fileupload的FileItem 代码示例 doUpload(Param(my_file) FileItem file), . 请选择文件: ,一个具体的文件上传列子,public void doUpload(Param(my_file) FileItem file, Context context) if(file = null | file.getSize() uploaded-file-validator,Custom Error,由action来验证数据 配置文件 “$userId”已经被抢了,换个吧! JAVA代码 public voi

21、d doRegister(FormField(name = userId, group = register) CustomErrors err) Map params = createHashMap(); params.put(userId, user.getUserId(); err.setMessage(duplicatedUserId, params); ,验证进阶 - 条件验证,条件验证 单分支验证 、多条件分支验证、多值验证等等 代码示例 ,实用技巧 编辑页面赋值,编辑页面 $group.mapTo($user) /将用户信息赋值给Group #set ($group = $for

22、m.register.defaultInstance) $group.mapTo($user) 用户注册 . . . ,实用的技巧 Group 继承与导入,Group继承 Group导入 继承与导入区别 继承只能有一个base group,而导入可以有多个import; 继承会合并同名的fields,而导入时是禁止同名fields的。假如group2已经有了field1,那么再次导入group1.field1将会报错。,扩展阅读,批量创建与修改表单 自己扩展Validator 国际化支持 附件:webx3_from.docx,Pull Tools,WEB框架基础,什么是Pull Tools?,

23、将对象置入模板中供模板使用 #set ($group = $form.register.defaultInstance) Home 生命周期 global :就是singleton,在系统启动时创建实例 request :在每个request的第一次访问该tool时,自动创建实例,如何置入?,webx-component.xml 框架自带的工具类 com.alibaba.citrus.util.Utils,如何扩展,直接注入自己的工具类 实现ToolFactory或ToolSetFactory接口并注入 示例 ,URL的解析与生成,WebX框架基础,URL 解析,WebX的URL是怎样的呢?以

24、下面的URL为例: http:/localhost:8081/index.htm URL scheme(协议):http: Server name:localhost Server port:8081 Context path: Component path:默认 Servlet path:/index.htm Target:/index.vm 分析URL是由AnalyzeURLValve完成的(后面会讲到),因此以上URL分析的规则是完全可以被改变的。,URL生成 - URIBrokers,URL是由URIBroker来动态生成的。 使用URIBroker有什么好处呢? 集中管理 全网站的U

25、RL均可在同一个配置文件中管理 可靠 动态生成,不容易出错 规范 例如在生成query string时,会自动URL encoding 透明 应用程序、模板不需要知道最终生成的URL的样子,修改URL就变得很简单,URIBrokers, / , Home | 淘宝网 ,Autowired URIBrokerService brokerService; public void execute(Context context) context.put(javaUriBroker, brokerService.getURIBroker(taobaoSite).render(); ,URIBroker

26、s,有哪些? turbine-uri 、uri content-uri 、servlet-uri、servlet-content-uri uri-bean Interceptors ,小技巧,在模板中重用uri #set ($newuri = $myuri.setPath(xxx).fork() #foreach ($i in 1.10) . #end 用fork提升性能 #set ($newuri = $myuri.setPath(xxx) / 1 #set ($newuri = $myuri.setPath(xxx).fork()/ 2 fork uri的性能要优于不fork。即第二种写法的性能要好于第一种。,Pipeline,WEBX进阶,Pipeline是什么?,一根布满了阀门水管 阀门:Valve 配置文件:common/pipeline.xml . ,创建一个简单的Valve,实现Value的接口 在pineline.xml里面进行配置 示例 主要用途 权限控制 全局检查CsrfToken等等,ResourceLoader,WEBX进阶,为什么要使用?,重命名资源 取消资源名称和环境的关联性 重定向资源 将部分资源名称,指向另外的地址,一个简

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论