Struts2学习笔记(一)

   使用Struts2开发都快1个月了,感觉虽然使用它实现了项目的功能,但很多新的东西都没有应用进来,
前一段时间项目催的太紧,所以决定最近系统的对Struts2学习一下,也记录一下学习的过程。
很多细节的例子请参考struts的demo.这里主要介绍一些概念及流程.
另外推荐一个好的strtus2 blog: blogjava.net/max
 
1.先看一下官方网站对Struts2的定义(struts2.org):
Apache Struts 2 was originally known as WebWork 2.
After working independently 
for several years, the WebWork and Struts communities joined forces to create Struts2.
This 
new version of Struts is simpler to use and closer to how Struts was always meant to be.

2.Introduction
  (1) 基于MVC2模式(关于MVC&MVC2,就不罗嗦了)
 (2)主要从从WebWork and XWork 演变过来
Struts is an Open-Source Web Application Framework that simplifies the creation of a Java Web Application.
It is based on the Model
-View-Controller 2 (MVC 2) Architecture which was originally found in a language called SmallTalk.
The recent version of Struts is Struts 
2.0 and it has borrowed most of the concepts in terms of architecture and functionality
from two frameworks namely WebWork and XWork. 

3.Struts2 MVC2 Architecture
  (1)Controller 作为Model&View的mediator.当Client请求过来时,Controller截获请求,再转发给合适的Action处理.
  (2)Model 代表application data以及控制数据的businness logic,当Controller转发请求给Action后,Action调用Model
    处理business logci,application data的状态被改变,Action控制权交给Controller,由Controller决定显示在Client端
    的View.
  (3)View代表显示在客户端的数据表现,可以是JSP、Velocity、Freemaker、XSLT.

4.Struts2 的处理流程
  (1) Client发送请求给Web application.Web Server 寻找该app对应的配置文件web.xml并加载相关Boot-strap Component.
        在Struts2中是Servlet Filter(而在s1中是Action Servlet).
  (2) servlet Filter 从配置文件(struts.xml)中匹配请求的动作,即xxx.action
  (3) Controller 先将request请求交给 Interceptor stack 处理,接着在传给相关Action.
  (4) 请求被传给Action后,基于请求的动作,Action执行合适的业务逻辑处理.
  (5) Action执行完毕,返回结果
  (6) Controller根据Action返回的结果选择具体的View显示在客户端.

  4.1 加载Filter Servlet
    当客户端请求一个Struts2的web application时,web server将查找关于application的配置文件web.xml
<filter>
   <filter-name>Struts2FilterServlet</filter-name>
  
<filter-class>
        org.apache.struts.action2.dispatcher.FilterDispatcher
  
</filter-class>
</filter>
                    
<filter-mapping>
  <filter-name>Struts2FilterServlet</filter-name>
    
<url-pattern>/*</url-pattern>
</filter-mapping>
                    
  
  4.2 拦截器处理请求
     Interceptors 提供pre-processing & post-processing,request在到达framework之前,首先被传给一系列的拦截器.
 可以自定义拦截器并更改配置文件,如下 


import com.opensymphony.xwork2.interceptor.*;

class AuthenticationInterceptor implements Interceptor{

    
public void init(){}

    
public void destroy(){}

    
public String intercept(ActionInvocation invocation) throws Exception{
    
         
// Get the value of user credentials from the Request and Validate it.
       
    }
}

更改struts.xml
<struts>
        
    
<interceptors>
        
<interceptor name = "authentication" 
            class 
= "myinterceptors.AuthenticationInterceptor">        
        
</interceptor>
    
<interceptors>
       
   
</struts>

  4.3 执行Action(详细请参考struts2的例子)
 
import java.servlet.http.*;
import org.apache.struts.*;

class MyAction extends Action{

    
public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        
throws java.lang.Exception {
        
        
// Do some business logic here.
        
    }
}
 
配置文件定义如下
  
                    
<struts>

    
<action name = "Registration" class = "hello.RegistrationAction">
    
<action name = "Login" class = "hello.LoginAction">
    
<action name = "Logout" class = "hello.LogoutAction">

</struts>
                    

4.4 返回结果
package myactions;

public class MyAction{

    
public String execute(){

        
if (createOperation()){
            
return "create";
        }
else if (deleteOperation()){
            
return "delete";
        }
else if( readOperation()){
            
return "read";
        }
else if  (writeOperation()){
            
return "write";
        }
        
return "error";
    }
}
strtus.xml配置:
<struts>
    
<action name = "MyAction" class = "myactions.MyAction">
        
<result name = "create">/myApp/create.jsp</result>
        
<result name = "delete">/myApp/delete.jsp</result>
        
<result name = "read">/myApp/read.jsp</result>
        
<result name = "write">/myApp/write.jsp</result>
    
</action>
</struts>





posted on 2007-06-20 11:26 想飞就飞 阅读(2500) 评论(2)  编辑  收藏 所属分类: J2EE

评论

# re: Struts2学习笔记(一) 2007-06-20 13:23 itkui

我现在在学习struts 1.2.×
等我学精通了再看看struts2的内容。
毕竟struts现在用的还很多呀!
  回复  更多评论   

# re: Struts2学习笔记(一) 2007-06-20 13:25 想飞就飞

@itkui

我也是用了好久的struts1,现在项目要求才转的
不过struts2.0确实先进很多
  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

公告


导航

<2007年6月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

统计

常用链接

留言簿(13)

我参与的团队

随笔分类(69)

随笔档案(68)

最新随笔

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜