站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2EE 专题Introduction of Struts

Introduction of Struts

摘要: An introduction to Struts Framework.
Hi,Every one.This is article is introduce STRUTS framework which I communicate with my colleagues.

An Introduction to Struts Framework
1. Start to Struts Framework
M-V-C (Model-View-Controller)
Just a Software Design Pattern, You can use any technology to implement it if you want to, Struts Framework is just one way Implement, But it is very good.

Model: Model is the main body of the Application, It stand for Business Data and Business Logic. One Model can support data to more View to reuse. So it let Application Reusable.

View: View is take account for Interact with the User.(UI)Just user interface .It display the relevant data to the user, Also receive the data from the user. But it won’t do any Business Process.

Controller: Control receive user’s input, then invoke Model and View to perform the user’s request.



Struts use what component to implement the M-V-C ?
Model: JavaBean and EJB( if the large Application).

View: JSP (use Struts taglib and Customize taglib), ActionForm Bean.(Like the common Bean, But have reset() and Validate() method.)

Controller: ActionServlet , Action

Struts Configure File: Struts-config.xml


2. Configure Struts Application. Model, View, Controller.
Struts in focus on the Controller’s Design, It take the Servlet part into FormBean and ActionServlet. Also add Servlet API:Incude Action, Actionform, Action Mapping.
Every FormBean need extends from ActionForm Class.FormBean is encapsulation of the web request. It support the data to the ActionBean. For further process.

The core of the Struts is ActionServlet,And the Core of the ActionServlet is Struts-config.xml,So if you master the Struts-config.xml you already master Struts.


Web.xml:

1,Configure ActionServlet(only one),let it can receive the Application’s request.

action
org.apache.struts.action.ActionServlet



action
*.do



2,configure the init-param,set it to this format:

config
/WEB-INF/struts-confg.xml



3,use struts’s taglib you need configureinclude:

/WEB-INF/struts-bean.tld
/WEB-INF/struts- bean.tld



/WEB-INF/struts-html.tld
/WEB-INF/struts-html.tld


/WEB-INF/struts-logic.tld
/WEB-INF/struts- logic.tld





Struts-config.xml:

1. Every part’s configure specifiction in Struts-config.xml all depend on DTD file.The DTD file info is @ the File header.So read DTD is the best start way to configure the Struts-config.xml .Just as:
struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

The different edition will get the different property .

2. The parameter’s order in Struts-config.xml is predefined,So you need put the element in the correct palce, But System will give you’re a template based on the DTD file.


Commonly include below list::
(1) data-sources: Configure the Data-source. Then you can get it in program,So you only need define it once, For easy maintenance.
(2) form-bean :Configure the Formbean which will transfer the data to the Action
(3) global-forwards :Configure the global-forward.The application will first find forward from config,If not find,then find it from here.
(4) action-mapping :Configure the action,Let Container know how to handle the request.
In ,there are some property:
1.Name -------specify the corresponding actionform,This must had defined in <form-bean>
2.Input ----------specify the path,If there have exception occur then forward to this path,commonly it is the JSP file which you submit your info o the server.

3.path --------required,unique,specify the action,corresponding with the request,start with “/” if the request is http://localhost:8080/category1/abc.do,then the path is :”/abc”

4.Scope ------- default value is “session”.This specify the form’s life domain.

5.Type ---------required,extends from or.apache.struts.action.Action class’s fullname path java Class name.Just your Business logic process class.

6.Validate ----------default vale is true,It’s Boolean.Specify if the formBean’s validate() method need invoke.

7.Paramater --------commonly use with other component,Just like Forward Action or DispatchAction

(5) message-resource :Configure the message-resource file,Your can use it to realize Internationalization. Comonly use

The more important thing is if you modify the Struts-config.xml file,Then you need to restart application to let it have effect.


The Struts framework’s Controller component

The main component of the Struts Framework act as Controller is:
ActionServlet Component:just as Central controller of the Struts Framework.
RequestProcessor Component: Act as request processor for the every module.
Action Component: Take account for process one particular Business.


3. About Struts Taglib
There are so many tag in Struts,Below is some example,But I think if you need use,you can refer from any book which introduce the TagLib of the Struts.

this tag will display the Error info other component produce in Struts Framework.
use to create HTML form.Can get form relevant with the corresponding ActionFormBean.
use to create textbox in the Form.
use to output the locale text file.Just message-resource file.
use to output the JavaBean Property’s value.
use to determine if the specify JavaBean’s exist in the particular scope.
And so on……….


4. Struts Framework integrated with EJB and Web Services.
Ok,think about you need more complex logic,then you need manage your session and the transaction,you need manage your DB’s connection and more,You will take the EJB to help you to resolve this problem.

↑返回目录
前一篇: 有效地记录日志可以简化企业的开发过程
后一篇: 探索Laszlo的类、属性及事件