WEB开发网
开发学院软件开发Java IntroductionofStruts 阅读

IntroductionofStruts

 2007-12-23 12:27:18 来源:WEB开发网   
核心提示:Hi,Every one.This is article is introdUCe STRUTS framework which I communicate withmy colleagues.an Introduction to Struts Framework 1.Start 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.
<servlet>
<servlet-name>action</servlat-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

2,configure the init-param,set it to this format: <param-name><param-value>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-confg.xml</param-value>
</init-param>

3,<taglib>use struts’s taglib you need configureinclude:
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts- bean.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/WEB-INF/struts-Html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts- logic.tld</taglib-location>
</taglib>

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:
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
<!DOCTYPE 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 <action> config,If not find,then find it from here.
(4)  action-mapping :Configure the action,Let Container know how to handle the request.
        In <action>,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 <Bean:message key=”msgKey”/>

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.

<html:errors> this tag will display the Error info other component produce in Struts Framework.
<html:form> use to create HTML form.Can get form relevant with the corresponding ActionFormBean.
<html:text> use to create textbox in the Form.
<bean:message> use to output the locale text file.Just message-resource file.
<bean:write> use to output the JavaBean Property’s value.
<logic:present> 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.进入讨论组讨论。

(出处:http://www.cncms.com)


Tags:IntroductionofStruts

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接