struts2教程实例

  • 格式:pdf
  • 大小:193.31 KB
  • 文档页数:10

struts2教程实例

1.第⼀个struts2项⽬

建议:参考官⽅配置操作⼀遍,因为技术不断更新,不同版本的struts的类可能不同,⽼版本的多个类可能在新版本中集成了⼀个

2.struts2⼯作流程原理

2.1步骤

1.创建Web Project

2.导⼊Jar(使⽤maven控制的话,配置pom.xml)

3.在web.xml配置struts2的过滤器

4.创建Struts核⼼xml⽂件

5.创建action类继承与ActionSupport

6.配置struts.xml

2.2流程

1、客户端浏览器发出HTTP请求

2、该请求被StrutsPrepareAndExecuteFilter接收

3、根据struts.xml配置,找到需要调⽤的Action类和⽅法, 并通过IoC⽅式,将值注⼊给Aciton

4、Action调⽤业务逻辑组件处理业务逻辑

5、Action执⾏完毕,根据struts.xml中的配置找到对应的返回结果result,并跳转到相应页⾯

6、返回HTTP响应到客户端浏览器

2.3原理

struts2

org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter

struts2

/*

注意:在struts2.1.2后FilterDispatcher被替换为了StrutsPrepareAndExecuteFilter

1、 客户端初始化⼀个指向Servlet容器(例如Tomcat)的请求

2、 这个请求经过⼀系列的过滤器(Filter)(这些过滤器中有⼀个叫做ActionContextCleanUp的可选过滤器,这个过滤器对于Struts2和其他框架的集成很有帮助,例如:SiteMesh

Plugin)

3、 接着被StrutsPrepareAndExecuteFilter(能够拦截请求对象ServletRequest和ServletResponse结合Struts.xml构建独⽴于servlet的ActionContxt)调

⽤,StrutsPrepareAndExecuteFilter询问ActionMapper(含有struts.xml中Action配置的name,namespce,result等的HashMap)来决定这个请是否需要调⽤某个Action

4、 如果ActionMapper决定需要调⽤某个Action,StrutsPrepareAndExecuteFilter把请求的处理交给ActionProxy

5、 ActionProxy通过Configuration Manager询问框架的配置⽂件struts.xml,找到需要调⽤的Action类

6、 ActionProxy创建⼀个ActionInvocation的实例。

7、 ActionInvocation实例使⽤命名模式来调⽤,在调⽤Action的过程前后,涉及到相关拦截器(Intercepter)的调⽤。

8、 ⼀旦Action执⾏完毕,ActionInvocation负责根据struts.xml中的配置找到对应的返回结果。返回结果通常是(但不总是,也可 能是另外的⼀个Action链)⼀个需要被表⽰的JSP或

者FreeMarker的模版。在表⽰的过程中可以使⽤Struts2 框架中继承的标签。在这个过程中需要涉及到ActionMapper

3.拦截器介绍

3.1Interceptor基础介绍

拦截器基础介绍以及与过滤器的对⽐见

3.2 计时拦截器实例

TimerAction.java

TimerInterceptor.javapublic class TimerAction extends ActionSupport{

public String excute(){

//下⾯为耗时代码段

int sum = 0;

for(int i =0;i<10000;i++){

sum+=i;

}

return SUCCESS;

}

}struts.xml配置

项⽬路径(maven管理):

4.深⼊struts

4.1 Action搜索顺序

第⼀步:判断package是否存在,如:path1/path2/path3/

第⼆步:如果package存在,则判断该package中action是否存在,如果不存在则去默认namespace的package⾥⾯寻找action

第三步:如果package不存在,检查上⼀级路径的package是否存在(直到默认namespace),重复第⼀步 第三步:如果没有则报错

4.2 动态⽅法调⽤

⽬的:⼀个action对应多个请求的处理,避免action过多

举例:下述为类为action.helloworld的action,该action可以处理请求../add.action和../update.action,下⾯介绍动态调⽤的⼏种常见⽅法

4.2.1 method⽅法

在struts.xml中的配置如下:public class TimerInterceptor extends AbstractInterceptor{

public String intercept(ActionInvocation invocation) throws Exception {

//1.执⾏action之前

long start = System.currentTimeMillis();

//2.执⾏下⼀个拦截器,如果已经是最后⼀个拦截器,则执⾏⽬标Aciton

String result = invocation.invoke();

//3.执⾏Action之后

long end = System.currentTimeMillis();

System.out.println("执⾏Action花费时间:"+(end-start+"ms"));

return result;

}

}

"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"

"http://struts.apache.org/dtds/struts-2.5.dtd">

/success.jsp

public class helloworld extends ActionSupport{

public String add(){

/* */

return SUCCESS;

}

public String update(){

/* */

return SUCCESS;

}

}

缺点:当⼀个action中⽅法过多的时候哦,配置过于冗余

4.4.2 感叹号⽅式

修改entity为:

在struts.xml中配置如下

4.4.3 通配符⽅式(推荐使⽤)

在struts.xml中配置如下

或者如下:

4.3 指定多个配置⽂件

⽬的:为了解决在struts.xml中配置过多,或者为了在不同的xml中配置实现更好的分类

要求:多个xml配置都必须遵守struts的dtd规范,同时要注意编码⽅式要相同

举例:

/add.jsp

/update.jsp

public class helloworld extends ActionSupport{

public String add(){

/* */

return "add";

}

public String update(){

/* */

return "update";

}

}

/add.jsp

/update.jsp

/{1}.jsp

/{2}.jsp

"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"

"http://struts.apache.org/dtds/struts-2.5.dtd">