Struts2学习笔记(一)
使用Struts2开发都快1个月了,感觉虽然使用它实现了项目的功能,但很多新的东西都没有应用进来,
前一段时间项目催的太紧,所以决定最近系统的对Struts2学习一下,也记录一下学习的过程。
很多细节的例子请参考struts的demo.这里主要介绍一些概念及流程.
另外推荐一个好的strtus2 blog: blogjava.net/max
1.先看一下官方网站对Struts2的定义(struts2.org):
2. Introduction
(1) 基于MVC2模式(关于MVC&MVC2,就不罗嗦了)
(2)主要从从 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
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
4.3 执行Action(详细请参考struts2的例子)
配置文件定义如下
< struts >
< action name = " Registration " class = " hello.RegistrationAction " >
< action name = " Login " class = " hello.LoginAction " >
< action name = " Logout " class = " hello.LogoutAction " >
</ struts >
4.4 返回结果
前一段时间项目催的太紧,所以决定最近系统的对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.
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.
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>
< 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 >
< 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.
}
}
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配置:
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 " ;
}
}
<
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 >
< 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 >