一、Struts2概述

  1、什么是Struts2?

  Struts2以WebWork为核心,采用拦截器的机制来处理用户的请求,这样使得业务逻辑控制器能够和ServletAPI脱离开来。

  2、工作原理

  当web容器接收到HttpServletRequest请求后,容器通过web.xml映射请求,并获得控制器的名字,然后容器调用控制器(StrutsPrepareAndExecuteFilter),控制器通过ActionMapper获取Action的信息并调用ActionProxy,ActionProxy读取struts.xml文件获取action和拦截器栈的信息,ActionProxy把请求传递给ActionInvocation,由ActionInvocation依次调用action和interceptor,根据action的配置信息,产生result,result信息返回给ActionInvocation并产生一个HttpServletResponse响应,将响应发送给客户端。

  上述的步骤可由下图表示:

  

二、配置环境的基本步骤

  1、在Eclipse中新建一个动态web工程。首先配置其web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>day_0818</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

  2、向工程中导入满足struts2开发的基本jar包

  

  3、在src目录下配置struts.xml文件,其中的字段有如下:

  package:包,struts2使用package来组织模块

name属性:必须,用于其它的用应用当前包

  extends:当前继承哪个包,继承的,既可以继承其中的所有的配置,通常情况下继承struts-default, struts-default这个包在struts-default.xml文件中定义

  namespace:可选,如果没有给出,默认为“/”,若namespace有一个非默认值,则要想调用这个包里的Action,就必须把这个属性所定义的命名空间添加到有关的URI字符串里

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="index">
<result>/index.jsp</result>
</action>
</package>
<include file="example.xml"/>
<!-- Add packages here -->
</struts>

  4、在服务器上运行该工程,即可显示index.jsp页面内容

  

三、注意事项

  1、具体视图的返回可以由用户自己定义的Action来决定,具体的手段是根据返回的字符串找到对应的配置项,来决定视图的内容,具体的Action实现可以是一个普通的Java类,里面有public String execute方法即可或者实现Action接口,最常用的是从ActionSupport继承,好处在于可以直接使用Struts2封装好的方法。另外,Action执行的时候并一定是要执行execute方法,可以在配置文件中配置Action的时候用method=来指定执行哪个方法,也可以在url地址中动态指定(DMI,推荐使用)。

  

最新文章

  1. display_inline-block_table-cell
  2. 问题-栈S最多能容纳4个元素,现有6个元素按A、B、C、D、E、F顺序进栈,问可能的出栈顺序。
  3. Warchall: Live RCE
  4. 算法积累:解决如何获取指定文件夹路径或者文件路径下所有子文件后缀为.h .m .c的文本的行数
  5. [HDOJ3714]Error Curves(三分)
  6. Python开发简单爬虫 - 慕课网
  7. hdu4639 hehe ——斐波纳契数列,找规律
  8. 查看 dmp 字符集
  9. eclipse&#39;s code assist
  10. IAR使用跳转功能时不正常的情况
  11. 下载图片没有关闭http输入流导致下载超时
  12. vant的坑
  13. golang实现tcp编程
  14. tiny4412 --uboot移植(2) 点灯
  15. (原)使用android studio ndk开发流程
  16. 【xsy1237】 字符转换 矩阵快速幂
  17. px、pt和em的区别
  18. Tomcat源码分析——启动与停止服务
  19. cJSON序列化工具解读一(结构剖析)
  20. codeforces 638B—— Making Genome in Berland——————【类似拓扑排序】

热门文章

  1. android 8 wifi 信号等级
  2. Spring JDBC SqlUpdate类示例
  3. unity-----------------------使用BuildAssetBundle打包
  4. (转)android媒体--stagefright概述【一】
  5. Kilo 版 Keystone 数据库结构
  6. HTTP之referer
  7. vue input 赋值无效
  8. c#包含类文件到csprj中
  9. Scripting.FileSystemObject对象的详细技巧指南
  10. 114自定义UITableViewCell(扩展知识:为UITableViewCell添加动画效果)