Struts2介绍.doc_第1页
Struts2介绍.doc_第2页
Struts2介绍.doc_第3页
Struts2介绍.doc_第4页
Struts2介绍.doc_第5页
已阅读5页,还剩16页未读, 继续免费阅读

下载本文档

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

文档简介

简单介绍Struts2与struts1相比,是一个全新的MVC框架。FilterDispatcher,将接收所有请求,并根据ActionMapper(请参考:docsactionmapper.html)所提供的信息,决定Action是否应该被调用或哪些方法将被调用(如判断其URL扩展名是否符合要求,URL是否符合某些特定的语法规则(如foo!bar.action将调用foo类的bar方法)等,请参考相关的文档),当然,对于ActionMapper而言,由于它的判断需要依赖于struts2的配置,所以,它也要用到ConfigurationManager来获得配置信息。接下来,针对每个Action类,将创建ActionProxy对象,它的创建依赖于ConfigurationManager(即配置文件)。接下来执行ActionProxy对象的execute方法,如果给Action配置了一系列拦截器,那么这个Action的代理类将逐个执行这些拦截器。当ActionProxy调用Action的方法,执行完之后,它将获得相关的Result对象,并调用Result.的execute方法来产生response对象。Struts2的请求过程包依赖基础包:1. commons-logging-1.1.jar2. freemarker-2.3.8.jar3. ognl-2.6.11.jar4. struts2-core-2.0.8.jar5. xwork-2.0.3.jar入门级例子1、 首先拷贝需要的基础依赖包(去除重复的jar包,保留高版本的)2、 在web.xml中,配置FilterDispatcher过滤器 struts2 org.apache.struts2.dispatcher.FilterDispatcher struts2 /* 3、编写一个简单的Action类,这个类简单到不能再简单了!public class VerySimpleAction public String hello()return success;在struts2中,Action类不必一定要继承一个父类(虽然也可以这样做,你可以继承ActionSupport类)。当然,你需要返回一个String类型的值,这个值代表的意思是,你的下一步要到哪里去。4、在类路径下,添加struts.xml配置文件,这个配置文件,是对Action的配置: /struts2/test/index.jsp 5、在相应位置提供JSP文件世界你好世界你好6、打开IE浏览器,在地址栏输入:http:/localhost:端口/Context Path/simple/hello.action至此,第一个struts2程序完成了。下面,是关于这个程序的问题的解答,以及进一步的讨论:为什么需要输入.action结尾的URL地址?而不是.do?Struts2在默认情况下,会将.action结尾的请求进行处理。我们可以在struts.xml配置文件中,指定可以使用的扩展名:比如:上述配置文件改为如下所示: /struts2/test/index.jsp 这将使得struts2,对于所有以.action或.do或.webwork扩展名结尾的请求均进行处理!当然,它也可以在web.xml中或其它地方进行配置,struts2提供了非常灵活的方式,请【参考相应的文档(docsconstant-configuration.html)】或参考struts2-core-2.0.8.jarorgapachestruts2下面的perties文件(这个文件在struts的核心jar包中)。Struts2都有哪些配置文件?Struts.xml是主要的配置文件,当然通过include标签可以将它拆分。比如,在struts.xml加入:即可添加一个example.xml文件,这个文件的配置与struts.xml文件的配置一致。往往大型应用中会按模块来划分配置文件。 另外还可以配置一些配置文件,请【参考docsconfiguration-files.html】通过使用annotation(JDK5的新特性),也可以完全取消这些配置文件。达到零配置文件的目的。也请参考相关的文档说明。Struts2配置【请参考:docsconfiguration-elements.html】相关概念:Package:用来组织Action及其一系列配置的方式Namespace: 页面访问的起始路径Action:处理请求Result: 结果转向拦截器:将要使用哪些拦截器Include: 可以将文件进行拆分,即通过include标签将一个配置文件包含进另外一个配置文件中Struts2 的Action类?Struts2的ACTION类可以是普通的POJO对象。方法只要返回字符串类型即可。这些字符串,正代表了下一步的转向。这些Action类,每次请求都会创建一个新的对象。Struts2的Action返回参数(result)?用一个字符串代表result,然后可以定义一个视图(比如某个JSP页面),与之匹配。定义在标签内部的result是局部result,也可以定义全局result: /Login.action /Exception.jsp 默认的result是JSP视图,我们可以通过指定result的类型来指定其使用不同的视图技术,如:/struts2/test/freemarker.ftl另外还有各种result 类型。【请参考:docsresult-types.html】如何从浏览器向ACTION传递参数?在Action类中定义属性,并提供getters/setters方法即可。更常用的做法是在action中持有要取参数的实体类的引用,action实现ModelDriven接口并实现其getModel()方法(如果不实现接口和方法则需要在jsp中用对象导航到属性)private Person person;/该方法把该对象压入到了ConpoundRoot,使它成为root,所以在jsp中直接按照属性名称传递就可以。不用使用进行导航。public Object getModel() if(person = null) person = new Person();return person;public Person getPerson() return person;如何将参数从action传递到JSP?在JSP中,使用:引入struts2的taglib调用:这样的标签,可以把值,从Action的属性中取出。或者使用ActionContext.getContext().put()方法来传值。或通过ServletActionContext.getRequest()/getSession()等方法往request/session中传值,在页面上可以通过: or or这样的方式来取值,【请参考:docsognl.html】Struts2中文参数传递可以在xeb。Xml中添加filter过滤,也可以在struts.xml中添加传递参数的内部机制是什么?(ValueStack)概述当请求到达ActionProxy进行处理之前,它会将界面传递过来的参数,经过转换之后,设置到Action对象中。 这种转换是通过OGNL进行的。OGNL(Object Graph Navigation Language)是一种强大的对象导航语言,通过OGNL可以用一种非常简易的方式来给对象设值或从对象取值。而Struts2正是依赖于OGNL的这种设值和取值机制,来将页面数据转换为相应的类型并设置到Action对象中;或者从页面中读取数据的。Struts2的OgnlValueStack,正是对Ognl的封装。ValueStack中会包含有当前环境中的各种对象信息。我们也可以通过调用ActionContext.getContext().put()方法,来往这个ValueStack中添加对象。Struts2会将这个ValueStack放入request scope中,并取值为:struts.valueStackOGNLl contextcontext是一个Map结构,可以自定义变量,并在ognl中的表达式中引用。使用#开头来引用这些2222222222222222222变量l root根对象,对于根对象,无需使用#开头来访问其属性值根据实际例子来讲解OGNL的使用package com.bjsxt.struts;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import junit.framework.TestCase;import ognl.Ognl;public class OgnlTest extends TestCase/ * root 对象的概念 */public void testOgnl_01() throws ExceptionUser user = new User();user.setUsername(张三);/Ognl.getValue(OGNL表达式,root对象);String value = (String)Ognl.getValue(username, user);log(value);public void testOgnl_02() throws ExceptionUser user = new User();Person p = new Person();p.setName(李四);user.setPerson(p);/Ognl.getValue(OGNL表达式,root对象);String value = (String)Ognl.getValue(, user);log(value);public void testOgnl_03() throws ExceptionUser user = new User();Person p = new Person();p.setName(李四);user.setPerson(p);/Ognl.getValue(OGNL表达式,root对象);String value = (String)Ognl.getValue(#, user);log(value);/ * Context的概念 */public void testOgnl_04() throws ExceptionUser user = new User();user.setUsername(张三);Person p = new Person();p.setName(李四);/* * 为了在OGNL的表达式中引用更多的对象,由于OGNL不支持多个根对象,所以 * 需要Context的概念,在context(实际上就是一个Map)中放入多个对象, * 再把context对象传入OGNL,我们就可以在表达式中引用那些对象了 */Map context = new HashMap();context.put(u, user); context.put(p, p);String value = (String)Ognl.getValue(#u.username + , + #,context, new Object();log(value);public void testOgnl_05() throws ExceptionUser user = new User();user.setUsername(张三);Person p = new Person();p.setName(李四);User u2 = new User();u2.setUsername(王五);Map context = new HashMap();context.put(u, user);context.put(p, p);String value = (String)Ognl.getValue(username + , + #u.username + , + #,context, u2);log(value);/ * OGNL中的赋值操作 */public void testOgnl_06() throws ExceptionUser user = new User();Ognl.setValue(username, user, 张三);log(user.getUsername();public void testOgnl_07() throws ExceptionUser user = new User();Map context = new HashMap();context.put(u, user);Ognl.setValue(#u.username,context, new Object(), 张三);log(user.getUsername();public void testOgnl_08() throws ExceptionUser user = new User();Map context = new HashMap();context.put(u, user);Ognl.getValue(#u.username = 张三,context, new Object();log(user.getUsername();public void testOgnl_09() throws ExceptionUser user = new User();Person p = new Person();Map context = new HashMap();context.put(u, user);context.put(p, p);Ognl.getValue(#u.username = 张三,# = 李四,context, new Object();log(user.getUsername();log(p.getName();/ * OGNL调用对象的方法 */public void testOgnl_10() throws ExceptionUser user = new User();user.setUsername(张三);String value = (String)Ognl.getValue(hello(username), user);log(value);public void testOgnl_11() throws ExceptionUser user = new User();user.setUsername(张三);String value = (String)Ognl.getValue(getUsername(), user);log(value);public void testOgnl_12() throws ExceptionUser user = new User();Ognl.getValue(setUsername(李四), user);String value = (String)Ognl.getValue(getUsername(), user);log(value);/ * 调用静态变量、方法 * /public void testOgnl_13() throws ExceptionUser user = new User();Ognl.getValue(setUsername(李四), user);Ognl.getValue(Systemout.println(getUsername(), user);public void testOgnl_14() throws ExceptionUser user = new User();Ognl.getValue(setUsername(zhangsan), user);Ognl.getValue(Systemout.println(com.bjsxt.struts2.UtilstoUpperCase(getUsername(), user);public void testOgnl_15() throws ExceptionUser user = new User();Ognl.getValue(setUsername(ZHANGSAN), user);Ognl.getValue(Systemout.println(new com.bjsxt.struts2.Utils().toLowerCase(getUsername(), user);/ * OGNL访问集合元素 * /public void testOgnl_16() throws ExceptionObject root = new Object();Map context = new HashMap();/用ONGL创建List对象List listvalue = (List)Ognl.getValue(123,sdfsdf,cccc, context, root);context.put(listvalue, listvalue);/用OGNL创建数组int intarray = (int)Ognl.getValue(new int23,45,67,context,root);context.put(intarray,intarray);/用OGNL创建MapMap mapvalue = (Map)Ognl.getValue(#listvalue:#listvalue,intarray:#intarray,context, root);context.put(mapvalue, mapvalue);Ognl.getValue(Systemout.println(#listvalue1),context, root);Ognl.getValue(Systemout.println(#intarray0),context, root);Ognl.getValue(Systemout.println(#mapvalue.listvalue1),context, root);Ognl.getValue(Systemout.println(#mapvaluelistvalue1),context, root);public void testOgnl_17() throws ExceptionList root = new ArrayList();User user = new User();user.setUsername(张三);Person p = new Person();p.setName(李四);root.add(user);root.add(p);log(Ognl.getValue(#root0.username, root);log(Ognl.getValue(#, root);private void log(Object s)System.out.println(s);ValueStackStruts2中的类型转换器的使用类型转换器的定义:- 继承StrutsTypeConverter即可局部类型转换:即针对某个Action的属性定义转换器1、需按某种规则定义注册文件(类名-perties),并放置在与Action一个包下面 (参考VerySimpleAperties)2、文件内容的定义是:属性名=转换器类名package com.bjsxt.struts;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Map;import org.apache.struts2.util.StrutsTypeConverter;public class DateConverter extends StrutsTypeConverter private static SimpleDateFormat format = new SimpleDateFormat(yyyy-MM-dd);Overridepublic Object convertFromString(Map context, String values, Class toClass) /判断前置条件,比如values的长度,是否非空,toClass类型是否正确if(!toClass.equals(Date.class)return null;try return format.parse(values0); catch (ParseException e) e.printStackTrace();return null;Overridepublic String convertToString(Map context, Object o) return format.format(Date)o);全局类型转换:即针对整个系统中同样类型的属性定义转换器1、需在类路径根目录下定义perties文件2、在文件中用这样的格式定义:属性类型的全路径类名=转换器全路径类名 参考(perties)与Spring如何集成?1、 将struts2-spring-plugin-2.0.8.jar加入类路径2、 在配置文件中,添加属性:3、即可以在spring中定义Action类,同时把struts2的配置文件中的class属性改为spring配置中的id值即可如何处理异常?通过类似如下配置可以定义出现一个异常的时候,应该转向什么页面来呈现: /struts2/test/exception.jsp 当出现SystemException异常之后,将转向exception.jsp页面,在exception.jsp页面上,可以这样写:这是异常信息页面!【】从上面简单的例子知道,exception对象本身被添加到了ValueStack中!【请参考:docsexception-configuration.html】Struts2标签库主题,模板,标签主题是模板的集合默认情况下,使用在template目录下的xhtml(主题名称)目录内的tag标签(这些模板是freemarker编写的模板文件)在不改动任何配置的前提下,我们可以在类路径中添加template目录,并且把xhtml目录拷贝到template目录下,这样,我们可以随意修改这些模板文件,直到符合我们的需要。Struts2将自动使用这些标签模板文件。【请参考:docsthemes-and-templates.html】# Standard UI theme# Change this to reflect which path should be used for JSP control tag templates by defaultstruts.ui.theme=xhtmlstruts.ui.templateDir=template#sets the default template type. Either ftl, vm, or jspstruts.ui.templateSuffix=ftlStruts2编程中的一些常见陷阱:在使用modelDriven接口的时候,更新对象时要注意因为ModelDriven把对象压入了ConpoundRoot,使它成为root 而我们在更新时需要首先加载对象并显示时,不能直接覆盖Action中的Person,因为这样不会更新ConpoundRoot中的Person,页面中取出来的还是原来的Person,所以需要拷贝属性到person中,也可以在页面中进行对象+. 进行导航。(注意:在使用类型转换器的时候,如果需要转换的属性是该对象的属性需要将其配置文件定义为该实体类的名字命名,然后把配置文件和实体类放在一期)/person = PersonManager.findPersonById(person.getId();Person p = PersonManager.findPersonById(person.getId();person.setName(p.getName();person.setSex(p.getSex();person.setAge(p.getAge();return Action.INPUT;Struts2拦截器的定义和使用方法参看项目:public class LoginInterceptor extends AbstractInterceptor Overridepublic String intercept(ActionInvocation invocation) throws Exception String username = (String)invocation.getInvocationContext().getSession().get(login);if(username = null)return Action.LOGIN;return invocation.invoke();Struts2与Spring的集成在struts2中集成spring的支持【即将Action对象的创建交给spring来完成】:1、将struts2-spring-plugin-2.0.8.jar和spring.jar加入类路径2、在配置文件中,添加属性:3、即可以在spring中定义Action类,同时把struts2的配置文件中的class属性改为spring配置中的id值即可- 【注意】即使集成了spring,如果我们在定义Action的class属性时,仍然定义的是Action的全路径 类名

温馨提示

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

评论

0/150

提交评论