struts2.0

来源:互联网 发布:zipline自定义数据 编辑:程序博客网 时间:2024/05/05 01:39

 



===============
   <package>
===============

struts.xml 中的package的name可以随意定义,只做继承时使用。

===============
  <constent>
===============

struts.xml 中
用<constent name="" value=""/>
来改变default.proerties 中key对应的值

==============================================
覆盖默认的资源文件 -- default.properties
==============================================

也可以在clasess(/src)下建立struts.properties的方式,覆盖默认的配置文件default.proerties
内容如:
struts.i18n.encoding=gbk      设置字符集
struts.action.extension=do    设置请求后缀名
struts.configuration.files=struts-default.xml,struts-plugin.xml,struts.xml     Struts2自动加载的配置文件


==============
    抽象包
==============
<package name="..." abstract="true">
表示该包中不能有action定义,只能通过包来继承来定义

=============
  命名空间
=============
<package name="..." namespace="">
将不同的应用分割开
如果不配置,表示当前包在一个默认的命名空间里

namespace="/hello"
请求路径就改变为 /hello/register

需要更改表单路径 并且一定要手工加.action
<s:form action="/hello/register.action">

寻找方式,如果namespace为默认,而表单路径为/hello/register.action
当struts没有找到该命名空间是,自动到默认的命名空间找

=============================
   <include>  模块化的配置 
=============================

<include file="struts_1.xml"></include>
<include file="struts_2.xml"></include>

struts_1.xml和struts_2.xml都是标准的strust的xml配置文件
include后,struts会在启动后将struts_1.xml和struts_2.xml的配置信息一同加载到内存中。


===============
  Struts2 MVC
===============
FilterDispatcher ----- 核心控制器

每个action ------- 业务控制器 
--------- 调用业务逻辑组件 ---- 调用DAO ---- 数据库交互

 

 

=================
    分模块开发
=================

src下建立
struts1.xml
struts2.xml

不同的开发人员,只需要写自己的xml配置文件
只需要加入到struts.xml中
<include file="struts1.xml"/>
<include file="struts2.xml"/>

----------------
struts1.xml
xml声明,DTD信息...  结构与struts.xml相同
<struts>
 <package name="" ...>
  ...
 ...
...

----------------
struts2.xml
xml声明,DTD信息...  结构与struts.xml相同
<struts>
 <package name="" ...>
  ...
 ...
...

 

================
   模型驱动
================

**属性驱动**:表单的字段都作为Action的属性存在

**模型驱动**:用一个JavaBean来装载对象 类似于ActionForm


模型Bean
public class User {
 private String username;
 private String password;
 private String repassword;
 private int age;
 private Date date;

 // setter... getter...
}

### ValueStack ### --值栈

ModelDriven<T> 该接口使用的泛型,
  模型是什么类型那个的泛型就是什么类型
------ T getModel()

Action 需要实现 ModelDriven接口


public class RigesterAction extends ActionSupport 
 implements ModelDriven<User> {

 private User user = new User();
 public User getModel() {
  return user;
 }

 @Override
 public String execute() throws Exception {

  return SUCCESS;
 }
}

 

====================
   Preparable 接口
====================

Preparable接口中有一个void prepare() throws Exception方法,实现该接口的Action类,将在执行这个action所以方法之前执行prepare(),称为action准备方法。

 

=========================
    显示特定的出错信息
=========================

<s:fielderror cssStyle="color:red">
 <s:param>username</s:param>
</s:fielderror>

去掉前面的“原点”
需要自己修改 fielderror.ftl模板
去掉前面的点代码

 


 

================================================
 动态方法调用 dynamic method invocation dmi
================================================

一个action中写若干个业务逻辑方法,不同的请求调用不同的方法

-1- 第一种 

页面调用不同的请求路径
配置文件中声明多个action,并且加上method参数指定对应的方法
action中编写多个同构于execute()的业务逻辑方法


-2- 页面中配置 

--------
jsp页面
--------

<s:form action="login!hello.action">

!前面的login匹配struts.xml中actionname
!
后面的hello匹配actin类中的hello()方法

----------
struts.xml
----------

<action name="login" class="....LoginAction">
 ...
...

---------
action中
---------

public String hello() throws Exception {

 ......
}

-- 3 -- 使用通配符

--------
jsp页面
--------

<s:form action="helloLogin">
 ...
...


----------
struts.xml
----------

用 * 进行模糊匹配 即:helloLogin请求找到 Login这个action
然后 通过method的参数 指定 请求的模糊匹配部分为对应到action类中的方法名
即:hello,该请求将会调用action类的hello()方法。

<action name="*Login" class="....LoginAction" method="{1}">
 ...
...

---------
action中
---------

public String hello() throws Exception {

 ......
}

 

 

=======================================
   <result>的type属性---定义结果类型
=======================================

可以在struts2-core包中的struts-default.xml中查看都有什么类型
包括:

chain   action链
   将多个action作为一个链进行处理,处理完一个紧接着处理下一个action

dispatcher (默认) 请求转发
freemarker
httpheader
redirect         重定向到另外一个视图资源  (request失效)
redirectAction  重定向到另外一个action    (request失效)
stream
velocity
xslt
plainText

 

====================
     动态结果
====================

有些时候,在Action执行完毕之后才知道Result的指向,可以使用如下的方法:

1.Action

private String nextAction;

public String getNextAction() {

    return nextAction;

}

public String execute() throws Exception {

  return “next”;

}

 

2.struts.xml

<action name="fragment" class="FragmentAction">

  <result name="next" type="redirect-action">${nextAction}</result>

</action>

 

 

原创粉丝点击