java struts 入门_Struts2 入门

前言

Struts2是一个相对流行的MVC框架。在说Struts2之前,我们先回顾一下传统的servlet/jsp实现的MVC架构,其运行机制是:jsp页面(视图view)发送消息到servlet(控制器Controller),servlet获取请求数据,处理业务逻辑,分发转向。这样的话,servlet就太臃肿了,什么活儿都是servlet在干,而且业务逻辑全是硬编码,代码都写死了不利于维护。

Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。Struts2是Struts的下一代产品,这个框架充分发挥了Struts1和WebWork这两种技术的优势,抛弃原来Struts1的缺点,使得Web开发更加容易。

struts2还有以下优点:

项目开源,使用及扩展方便 - 天生优势;

提供Exception处理机制;

Result 方式的页面导航,通过 Result 标签很方便的实现重定向和页面跳转;

通过简单、集中的配置来调度业务类,使得配置和修改都非常容易;

提供简单、统一的表达式语言来访问所有可供访问的数据;

提供标准、强大的验证框架和国际化框架;

提供强大的、可以有效减少页面代码的标签;

提供良好的Ajax支持;

拥有简单的插件,只需要放入响应的 jar 包,任何人都可以扩展 Struts2 框架。

Struts2框架的MVC分别对应:

V:jsp+OGNL

C:action类

M:javabean+ModelDriven

Struts2框架的控制器将“获取请求”和“分发转向”代码抽取出来写在配置文件里,这样一来,控制器(action类)就能专注于业务逻辑的处理了。

Struts2具体实现:

由上图看出,整个框架的运行又紧紧围绕着核心过滤器StrutsPrepareAndExecuteFilter展开工作。深入到filter的源码中会对理解有所帮助。下面几篇博客对filter的介绍很好。

(1),struts2请求过程源码分析:博客1

(2),Struts 2配置文件加载顺序:博客2

3,默认拦截器

struts-default.xml配置文件中定义了一个默认拦截器栈,这些拦截器就是动作方法执行之前的要执行的。常用的有封装用户表单数据到javabean的modelDriven拦截器,用于输入验证的validation拦截器,等等。

4,View和Controller之间的交互

从视图页面每次发来的用户请求都会产生一些数据,这些数据都存放在哪儿呢?实际上,每次动作类执行前,核心过滤器StrutsPrepareAndExecuteFilter都会创建2个对象:ActionContext和ValueStack,这2个对象存储了动作访问期间用到的所有数据。这些数据又可以在jsp页面上通过strut标签和OGNL表达式来取得。

(1),ActionContext是一个map数据结构,其中的key是一些常见的域对象(application,session,request等),而value又是一个map。也就是说ActionContext是一个大map包裹着一些小map。

(2),ValueStack是一个ArrayList数据结构,并且是一个栈结构,每次都在栈顶存取数据。

5,Controller与Model之间的交互

C和M之间的交互比较简单,利用Struts框架提供的拦截器:ModelDriven,即可实现将用户表单提交的数据封装到对应的javabean中。要点:(1)javabean类自己编写。(2)动作类要实现ModelDriven接口。(3)实现抽象方法getModel()。

具体参考:Struts2学习(1):Struts2框架结构详解_PHP_欢迎来到Jimmy的博客-CSDN博客​blog.csdn.net44809656ec9a47a4901489ac01bd3c33.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Struts Logic标签库中包含的标签列表 Tag name Description empty 如果标签parameter,propertie等属性所指定的变量值为null或空字符串,则处理标签包含的内容 equal 如果标签parameter,propertie等属性所指定的变量的值等于标签value属性所指定的值,则处理标签所包含的内容, 如: <logic:equal value="modify" property="action" name="projectForm"> <bean:message key="project.project_modify"/> </logic:equal> 上面的示例表示,如果projectForm的action属性等于modify,则处理<bean:message key="project.project_modify"/ >语句。 forward Forward control to the page specified by the ActionForward entry. greaterEqual Evaluate the nested body content of this tag if the requested variable is greater than or equal to the specified value. greaterThan Evaluate the nested body content of this tag if the requested variable is greater than the specified value. iterate Repeat the nested body content of this tag over a specified collection. lessEqual Evaluate the nested body content of this tag if the requested variable is less than or equal to the specified value. lessThan Evaluate the nested body content of this tag if the requested variable is less than the specified value. match Evaluate the nested body content of this tag if the specified value is an appropriate substring of the requested variable. messagesNotPresent Generate the nested body content of this tag if the specified message is not present in this request. messagesPresent Generate the nested body content of this tag if the specified message is present in this request. notEmpty Evaluate the nested body content of this tag if the requested variable is neither null nor an empty string. notEqual Evaluate the nested body content of this tag if the requested variable is not equal to the specified value.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值