Struts2框架篇

来源:互联网 发布:mysql explain type 编辑:程序博客网 时间:2024/06/05 02:02

刘宝宝开始总结了:

第一章 基础

一.Framework概念:
伴随着软件开发的发展,在多层的软件开发项目中,可重用、扩展性的,而且是经过良好测试的软件组织,越来越为人们所青睐。这意味着人们可以将充裕的时间用来分析、构建业务逻辑的应用上,而非繁杂的代码工程上。于是人们把相同类型问题的解决途径进行抽象,抽取成一个应用框架。这也就是我们所说的Framework。
 
特点:
Framework体系提供了一套明确的机制,从而让开发人员很容易的扩展和控制整个Framework开发的结构。通常,Framework的结构中都有一个“命令和控制”组件(“command and control” component)。
通常基于请求相应(request-response)模式的应用Framework,基本上有如下几个表现逻辑结构组成。
控制器(Controller):控制整个Framework中各个组件的协调工作。
业务逻辑层(Bussiness Logic):对Framework本身来说,这里仅仅是概念和几个提供服务的基础组件,真正的实现与客户端的业务逻辑接轨,还需要开发人员在Framework上再次扩展。
数据逻辑层(Data Logic):绝大多数应用系统都需要涉及到数据交互,在一层主要包括了数据逻辑和数据访问接口。

二struts2的概念:

struts2是基于FrameWork的框架。


从某种程度上说,Struts 2并没有继承Struts1的血统,而是继承了WebWork的血统。
WebWork就是web项目的FrameWork框架,简称WebWork。
 

三.struts2的工作原理:

客户端--请求--->FilterDispatcher-->ActionMapper-----拦截器-->Action
 |                                           |                                    |
 |                                     struts.xml                           拦截器
 |                                                                                   |转发
 <--------------------------响应<---------------------------------jsp
struts2工作原理描述?
1)客户端向容器(如Tomcat)提交一个请求
2)请求经过一系列过滤器,核心控制器FilterDispatcher 被调用
3)ActionMapper来决定请求是否需要调用某个Action
4)如果ActionMapper通过struts.xm决定需要调用某个Action,在此之前会依次调用所有配置的拦截器
5)Action执行完毕,根据结果字符串在struts.xml的配置中找到对应的返回结果
6)拦截器被再次执行
7)跳转到指定的jsp 页面
8)响应给客户端

四.搭建一个struts2框架:

搭建strus2框架
1.新建一个web工程;
2.把struts2核心包拷到工程的lib下;
3.在web.xml中配置Struts2的前端控制器FilterDispatcher;
4.在src目录下新建struts.xml文件;struts-2.0.dtd
-------------------------------------------------------
struts2 *.jar包的说明:略

web.xml配置说明:略


五.创建第一个struts例子:

1.新建一个Action类,完成从action到jsp功能;
2.在struts.xml里进行配置;
3.编写jsp页面,接收action中传的值;
4.测试效果。
--------------------------------------------------------
配置web.xml
  <filter>
     <filter-name>struts2</filter-name>
     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
 
  <filter-mapping>
     <filter-name>struts2</filter-name>
     <url-pattern>/*</url-pattern>
  </filter-mapping>


src下
struts.xml
struts-2.0.dtd


配置struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  "struts-2.0.dtd">
 
<struts>
  <package name="main" extends="struts-default">
     <action name="hello" class="com.bw.action.HelloAction">
       <result name="ha">/hello.jsp</result>
     </action>
  </package>
</struts>


<package name="main主包名" extends="struts-default  struts2自带的默认拦截器">
     <action name="hello(请求路径.action)" class="com.bw.action.HelloAction(完整的action类)">
       <result name="ha(转发名与action里的转发相对应)">/hello.jsp(转发地址)</result>
     </action>
</package>




第一个struts2例子
hello

六.struts.xml配置:

struts.xml
<package name="main(包的名称)" namespace="/main(包的命名空间)" extends="struts-default(要继承的包)">
 <action name="请求路径" class="action的完整类名">
 <result name="转发名与action里的转发相对应" type="结果类型">/转发地址</result>
 </action>
</package>
/user/user!add.action
包与包之间可以继承
<package name="main1" extends="main">


</package>


引入其它配置文件:用于分模块
<include file="struts-student.xml" />
<include file="struts-user.xml" />
<include file="struts-book.xml" />


----------------------------------------
具体说明:
<package>元素有如下属性:
name:包的名称。必须配置。不可重复。 [必选]
extends:要继承的包,后面配置的是被继承的包的名称。[必选]
namespace:包的命名空间。[可选]
abstract:定义包为抽象的,也就是不能包含Action的定义。[可选]


namespace配置的是包的命名空间,同一个命名空间里面不能有同名的Action,当然不同的命名空间里面是可以有同名的Action的。类似于Java的包的功能,namespace可以有效的防止action重名的冲突,因为配置了namespace后,在访问action的时候就需要添加namespace来作为action的前缀。


七.Action

1.Action说明:

在Struts2中,一个Action类代表一次请求或调用,每个请求的动作都对应于一个相应的Action类,一个Action类是一个独立的工作单元。
也就是说,用户的每次请求,都会转到一个相应的Action类里面,由这个Action类来进行处理,因此说一个Action类代表了用户的一次请求或调用。
换句简单的话来说,Action就是用来处理一次用户请求的对象。

2.编写Action类三种方式:

编写Action类三种方式
第一种:直接写action
第二种:实现Action接口
第三种:继承ActionSupport
-------------------------------------------------------
编写Action类三种方式
1、编写普通action类,定义名称为execute()的方法,在访问action时,会默认访问此方法。
2、编写action类,实现Action接口,实现execute()方法,在访问时,默认访问此方法。
3、编写action类,继承ActionSupport父类,重写execute()方法,在访问时,默认访问此方法。


注意:在导入Action接口的时候,有好多同名不同包的类都叫Action,比如javax.swing.Action,
一定要注意,我们需要使用Xwork2中的Action接口。


Struts2的ActionSupport类提供了对__表单数据验证和本地化________________________      的支持。

3.Action接口:五个常量,一个执行方法

Action接口:五个常量,一个执行方法
SUCCESS:表示Action执行成功,显示结果视图给用户,值为字符串"success"。
NONE:表示Action执行成功,不需要显示视图给用户,值为字符串"none"。
ERROR:表示Action执行失败,显示错误页面给用户,值为字符串"error"。
INPUT:表示执行Action需要更多的输入信息,回到input对应的页面,值为字符串"input"。
LOGIN:表示因用户没有登陆而没有正确执行,将返回该登陆视图,值为字符串"login"。

public String execute() throws Exception;     //执行方法
 
4.Action中接收数据的二种方式

属性驱动(FieldDriven)和模型驱动(ModelDriven)。
属性驱动又有二种
1.简单数据类型属性驱动
2.域对象的属性驱动***javaBean**
-------------------------------------------------------------------------------------
具体使用方法:
一、属性驱动(对应的是基本数据类型)
1.属性驱动
2.域对象(javaBean风格)


二、模型驱动(ModelDriven)
---------------------------------------------
一、属性驱动(FieldDriven)(对应的是基本数据类型):属性驱动有两种


1.第一种属性驱动--简单数据类型
action类中写
private String name;
设置setter getter方法


index.jsp
页面上取值${name}


2.第二种属性驱动--域对象(javaBean)
1.编写Model类-用于存储对象
2.action类中写
private LoginModel loginModel;
设置setter getter方法
//收集数据需要用setter getter方法


表单页面
域对象.属性
<form action="login2.action" method="post">
 登录名2:<input type="text" name="loginModel.login_name"><br>
 密码2:<input type="password" name="loginModel.pwd"><br>
 <input type="submit" value=" 登录 ">
</form>


页面上取值${loginModel.name}
-----------------------------------------------------------
二、模型驱动(ModelDriven)
1.编写Model类-用于存储对象


2.action类implements ModelDriven
//模型驱动
private LoginModel loginModel = new LoginModel();
//实现模型驱动的方法,返回Model对象
//只要实现模型驱动ModelDriven中 getModel()方法 ,就能收集表单数据
public Object getModel() {
 return loginModel;
}


3.表单页面
 <!-- 使用模型驱动取值 因为模型就是loginModel所以就不需要加loginModel了 直接用属性就行 -->
<form action="login3.action" method="post">
 登录名3:<input type="text" name="login_name"><br>
 密码3:<input type="password" name="pwd"><br>
 <input type="submit" value=" 登录 ">
</form>


4.页面上取值
${login_name }---${pwd }


-----------------------------------------------------------
总结:
1.Action中基本的数据对应方式,一般都使用域对象。
2.少用模型驱动。因为模型对象只能实现一个getModel()方法
3.模型驱动不需要用setter getter方法


4.作用域对象的好处:对象.属性能区分是哪一个模块的
<form action="student.action" method="post">
 姓名:<input type="text" name="studentModel.name"><br>
 密码:<input type="password" name="studentModel.pwd"><br>
 <input type="submit" value=" 登录 ">
</form>


<form action="teacher.action" method="post">
 姓名:<input type="text" name="teacherModel.name"><br>
 密码:<input type="password" name="teacherModel.pwd"><br>
 <input type="submit" value=" 登录 ">
</form>

5.Action中的动态方法使用及调用

语法:xxx!方法名.action
struts1:user.do?method=add
struts2:user!add.action
Action的默认方法是execute();
http://localhost:8080/struts2/main/hello.action




http://localhost:8080/struts2/main/hello!add.action   对应HelloAction类中的add方法
public String add() {
  System.out.println("-------helloaction的add方法------");
  return "add";
}


http://localhost:8080/struts2/main/hello!del.action   对应HelloAction类中的del方法
public String del() {
  System.out.println("-------helloaction的del方法------");
  return "del";
}
 

6.通配符的使用

xml中name路径的通配符的使用


<!-- 通配符的使用 -->
    *代表任意方法名
<action name="hello_*" class="com.bw.action.HelloAction" method="{1}">
 <result name="ha">/hello.jsp</result>
</action>
http://localhost:8080/struts2/main/hello_add.action 对应HelloAction类中的add方法
http://localhost:8080/struts2/main/hello_del.action 对应HelloAction类中的del方法


*代表UserAction中的某个方法的名字
1代表匹配第一个*号


user!add.action
user_add.action
--------------


<action name="*_*" class="com.bw.action.{1}Action" method="{2}">
 <result name="ha">/hello.jsp</result>
</action>
http://localhost:8080/struts2/main/User_add.action   通过配置文件可以对应 UserActioin类
http://localhost:8080/struts2/main/Book_add.action   通过配置文件可以对应 BookActioin类

八.struts2的作用域(耦合与解耦合)

-------ActionContext和ServletActionContext-可以取到request session application----------------------------------
-------如何取到requst session application(掌握)---------------------------------------------------------
//第一组(解耦合)ActionContext
ActionContext ac = ActionContext.getContext();
Map request = (Map) ac.get("request");
Map session = ac.getSession();
Map application = ac.getApplication();


Map request = (Map)ActionContext.getContext().get("request");
Map session = ActionContext.getContext().getSession();
Map application = ActionContext.getContext().getApplication();
    


//第二组(耦合)ServletActionContext
PageContext pageContext = ServletActionContext.getPageContext();
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
HttpSession session = request.getSession();
ServletContext application = ServletActionContext.getServletContext();  
ActionContext ac = ServletActionContext.getContext();


九.Result基础

Reult能干什么和做什么?
简单的说,Result是Action执行完后返回的一个字符串,它指示了Action执行完成后,下一个页面在哪里
具体页面在哪里,是在struts.xml里面配置的。
就是每个<action>元素里面,配置的<result>子元素,例如:
<action name="hello" class="com.bw.action.HelloAction">
   <result name="add" type="dispatcher">/hello.jsp</result>
   <result name="del">/hello.jsp</result>
</action>

1在Struts2中,预定义了一些Result的字符串常量

SUCCESS:表示Action执行成功,显示结果视图给用户,值为字符串"success"。
NONE:表示Action执行成功,不需要显示视图给用户,值为字符串"none"。
ERROR:表示Action执行失败,显示错误页面给用户,值为字符串"error"。
INPUT:表示执行Action需要更多的输入信息,回到input对应的页面,值为字符串"input"。
LOGIN:表示因用户没有登陆而没有正确执行,将返回该登陆视图,值为字符串"login"。

2预定义的ResultType

ResultType
1.chain---ActionAction
 (ActionAction)
 ActionAction


2.redirect---重RL。3.dispatcher---默4.plainText---直5.stream---直6.redirectAction:ction
----------------------------------------
 <result>中ype如ispatcher”sp页<action name="hello" class="com.bw.action.HelloAction">
 <result name="add" type="dispatcher">/hello.jsp</result>
 <result name="del">/hello.jsp</result>
</action>




演示in
当te="chain"时,能ion中取到 <action name="hello" class="com.bw.action.HelloAction">
 <result name="ha" type="chain">
     <param name="actionName">toIndex</param>
 </result>
 </action>
      actionName是底stts2提供的, <action name="toIndex" class="com.bw.action.ToIndexAction">
 <result name="ha">/hello.jsp</result>
 </action>
说明:et与局部ret:
1.result元素放在an,就是局部rt。
2.esult元素放在gl-results,就是全局rt。
总结尽t---只有在多个n都具有某个t时,才考虑t。


!-- type="chain"
 <package name="news" extends="struts-default">
 <action name="news" class="com.baidu.action.NewsAction">
  <result name="success" type="chain">
    <param name="actionName">index</param>
  </result>
 </action>
 <action name="index" class="com.baidu.action.IndexAction">
  <result name="index">/index1.jsp</result>
 </action>
 </package>
 -->
  <!-- type="redirect"
 <package name="news" extends="struts-default">
 <action name="news" class="com.baidu.action.NewsAction">
  <result name="success" type="redirect">/index.action</result>
 </action>
 <action name="index" class="com.baidu.action.IndexAction">
  <result name="index">/index1.jsp</result>
 </action>
 </package>
 -->
  <!-- type="redirectAction" -->
  <package name="news" extends="struts-default">
    <action name="news" class="com.baidu.action.NewsAction">
       <result name="success" type="redirectAction">index</result>
    </action>
    <action name="index" class="com.baidu.action.IndexAction">
       <result name="index">/index1.jsp</result>
    </action>
  </package>




<action name="user-*" class="com.user.action.UserAction" method="{1}" >
       <!-- 引入拦截器 --     <interceptor-ref name="defaultStack" />
       <interceptor-ref name="aaa">
         <param name="includeMethods">add</param>
       </interceptor-ref>
       <result name="error" type="chain">
         <param name="actionName">user-toAdd</param>
       </result>
      
       <result name="toadd" type="dispatcher">/WEB-INF/user/user_add.jsp</result>
       <result name="success" type="redirect">/user/user-list.action</result>
       <result name="list" type="dispatcher">/WEB-INF/user/user_list.jsp</result>
       <result name="toupdate" type="dispatcher">/WEB-INF/user/user_update.jsp</result>
    </action>


第二章待续。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
                                                      




1 1
原创粉丝点击