Struts2框架的见解

来源:互联网 发布:网络被骗 编辑:程序博客网 时间:2024/05/16 10:11

Struts2框架的见解


本篇文章是我在整理struts2框架的详细内容时的一些见解.适合有一定的struts2开发经验的朋友们互相交流.

一:Struts2的运行原理

  • 在 WEB 容器启动的时候加载 web.xml 文件, 核心控制器 FilterDispatcher 会加载并解析 struts.xml.

  • 客户端会发送一个请求到action,FilterDispatcher会根据web.xml中配置的后缀名进行拦截 .

  • FilterDispatcher根据struts.xml的配置文件信息找到某个action对应的某个类里的指定的方法

  • 执行相关的业务逻辑之后,返回一个String.

  • 在struts.xml中的 action 标签中的 result 标签上的 name属性的属性值与返回的 String 字符串进行匹配, 跳转到指定的 Action 或者 jsp (看您的具体实现了) .

二:Struts2中相关的注意事项与映射关系汇总

  • Struts2 的 Action 继承 ActionSupport ,也可以实现 Action 接口,也可以什么都不实现.在 ActionSupport 中也有与Struts1一样的 execute()方法.但是,它的返回值为 String ,且没有 mapping, form, request, response 这四个参数.

  • 重写ActionSupport中的execute()方法,且返回值大部分 情况下为String.

  • 在提交数据的jsp页面中,name的值必须用级联属性赋值.

  • 级联属性赋值.如:student.studentName 其中 student 必须和 action 中 声明的用来接收前台表单参数信息的 javaBean保持一致.其中的 studentName 必须和 Model 中的属性值相同.

  • 在 action 中声明的用来接收前台表单参数信息的 javaBean ,一定要生成对应的get, set 方法.否则接收不到值.

  • result 的 name 的默认值为 success, type 的默认值为 dispatcher 对应的是 forward 跳转. 还可以是 redirect 或者 redirectAction.

  • 在”struts.xml”中通常package 继承与 struts-default, action 中 的name 可以和 method 一样的.

  • 通过 action 中返回的字符串和 result 中 的 name 属性匹配, 来找到跳转的 jsp 页面.

  • 在进行 Ajax请求访问的时候,回调函数中的 data 指的是请求中的访问信息.

  • 在struts.xml中的配置

<!--struts的后缀名-->    <constant name="struts.action.extension" value="do" /><!--struts的编码方式-->    <constant name="struts.i18n.encoding" value="utf-8" />        <!--进行模块划分 -->        <!--可维护性,可读性 -->    <!--包含另外一个配置文件 -->    <include file="conf/struts/struts-login.xml" />        <include file="conf/struts/struts-book.xml" />        <include file="conf/struts/struts-resource.xml" />    <include file="conf/struts/struts-member.xml" /> 

大家有什么好的意见,可以分享出来,大家共同学习,进步.

0 0
原创粉丝点击