Using Struts to Develop Our Web Application

类别:.NET开发 点击:0 评论:0 推荐:

Author:Holyfair Chen

1. Introduction
 Struts is an open source of  webservice Framework host by ASF as a part of its Jakarta project.
 The primary architect and developer of the Struts framework is Craig R.McClanahan.He is also the
    primary architect of Tomcat 4.
     1) Struts is not only thread-safe but thread-dependent.
     2) ActionForm beans minimize subclass code and shorten subclass hierarchies.
     3) The Struts tag libraries provide general-purpose functionality.
     4) The Struts components are reusable by the application.
     5) The Struts localization strategies reduce the need for redundant JSPs.
     6) Struts is designed with an open architecture.
     7) Struts is lightweight.
     8) Struts is standards compliant.
     9) Struts is open source and well documented.
       10) Struts is model neutral.
2. Some Conception of webservies
 2.1 Serlvet
  Servlet 运行于Servlet容器,可以被Servlet容器动态加载控制服务器提供服务。
 2.2 JSP
  JSP容器对一个JSP文件请求进行语法分析并生成Java Servlet源文件,然后不进行编译产生响应。
 2.3 共享数据的访问范围和生命周期
 2.4 JavaBean
 2.5 Cookies
  Cookies and URL rewriting are two common ways to keep track of users between requests.
  Cookies 经过加密保存在客户端.
 2.6 CGI
  CGI uses standard operating system features, such as environment variables and standard input
  and output, to create a bridge, or gateway,between the web server and other applications on
  the host machine.
  The main drawback to CGI is that it must run a new copy of the CGI-aware program for each
 2.7 Web组件的三种关联关系
  * 请求转发(Forward)
  * URL重定向 (Redirect)
  * 包含 (Include)
 2.8 MVC(Model ,View and Controller)
3. Exploring the Struts Architecture

 2.1 Struts controller components
  To the application developer, a hyperlink is a path to some resource in the application.
   <forward name="welcome" path="/pages/index.jsp"/>
  [HTML forms:ActionForm]
  ActionForms are just JavaBeans with a couple of standard methods to manage the validation
  and revision cycle.
  public final class LogonForm extends ActionForm
   private String username = null;
   public String getUsername()
    return (this.username);
   public void setUsername(String username)
    this.username = username;
   <form-bean name="articleForm" type="org.apache.artimus.struts.LogonForm"/>
  [Custom actions]
  An HTML form uses an action parameter to tell the browser where to send the form’s
  data. The Struts framework supplies a corresponding Action class to receive such data.
  The framework automatically creates, populates, validates, and finally passes the
  appropriate ActionForm to the Action object. The Action can then get the data it needs
  directly from the ActionForm bean. Here’s an example:
   public final class LogonAction extends Action
    public ActionForward perform(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException {
    MyForm myForm = (MyForm) form;
    // ...
    return mapping.findForward("continue");
    <action path ="/update"
             name = "SRSForm"
      type  ="com.presentation.action.UpdateAction"
      scope  ="request"
      validate ="true"
     <forward name="success" path="/" redirect="false"/>
  In a web application, every resource must be referred to through a Uniform
  Resource Identifier (URI). This includes HTML pages, JSP pages, and any custom
  actions. To give the custom Actions a URI, or path, the Struts framework provides
  an ActionMapping object. Like the ActionForwards and ActionForms, the mappings
  are usually defined in the XML configuration file:
  [ActionServlet] & [Localization]
 2.2 Struts Control Flow

3. Configuring Struts Components
 Struts 的配置文件主要包括以下几个:
 web.xml:The web application deployment descriptor required by the Java Servlet specification.
         The servlet/JSP container uses this file to load and configure your application.
 build.xml: A file used by the Jakarta Ant build tool to compile and deploy your application.
     Using Ant is not a requirement, but it is a popular choice among Struts developers.
 struts-config.xml:The framework’s deployment descriptor. It is used to load and configure
     various  components used by the Struts framework. file provides the message resources for your Struts application.
     Like the build.xml file, it is not a strict requirement but is used
     by most Struts applications.
 3.1 web.xml
  <!-- 1 -->
  <?xml version="1.0" encoding="ISO-8859-1"?>
  <!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
   <!-- 2 -->
   <!-- 3 -->
   <!-- 4 -->
   <!-- 5 -->
  <1> 定义为web-app configuration问件.
  <2> 设置ActionServlet参数(只能load一个ActionServlet参数,否则引起冲突)
  <3> 定义Struts Request.
  <4> 设置welcome file.只能是实际存在的file,index.do形式的将不起作用.
      <!DOCTYPE html
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     <title>Struts Demo</title>
     <meta http-equiv="Refresh" content="0; URL=/CXdemo/">
  <5> 设置taglib
 3.2 struts-config.xml
     <!-- ======== global forwards Definitions ================================-->
   <forward name="logoff" path="/"/>
    <!-- ======== Form Bean Definitions =================================== -->
        <form-bean name="LoginForm" type="com.presentation.actionform.LoginForm"/>
    <!-- ========== Action Mapping Definitions ============================== -->
      <!-- Say Hello! -->
      <action    path      = "/login"
                 type      = "com.presentation.action.LoginAction"
                 scope     = "request"
                 validate  = "true"
       <forward name="logon" path="/"/>
    <!-- ========== Message Resources Definitions =========================== -->
    <message-resources null="false" parameter="ApplicationResources" />
    <plug-in className="com.cappuccinonet.strutscx.util.StrutsCXPlugIn">
      <set-property property="config" value="/WEB-INF/strutscx-config.xml" />
 3.3 build.xml
4.Installing the Struts core files
  All the stock files that you need to run Struts are provided in the Struts library
  distribution. These include several JARs, the tag library descriptors,
  DTDs, and standard XML configuration files. This set of stock files, combined
  with the four files you provide, create a complete core Struts configuration.
  Here’s the checklist:
    Download and unzip the Struts library distribution [ASF, Struts].
    Copy all the *.jar files to your application’s /WEB-INF/lib folder.
    Copy all the *.tld files to your /WEB-INF folder.
    Copy all the *.xml and *.dtd files to your /WEB-INF folder.
    Create your deployment descriptor (see 4.2).
    Create your Struts configuration file (see 4.4).
    Create your message resources bundle (see 4.5).
    Create your Ant build file (see 4.6).
  1) Struts模型组件方面
  2) Struts扩展方面
  3) Struts国际化方面
  4) Validator验证框架
  5) 复杂的异常处理
  6) Struts标签库
  7) Title框架
  8) 与EJB和SOAP Web服务
  9) Log
        10) StrutsTestCase测试
        11) 设计模式方面
        12) Struts各方面的各项扩展
 基于篇幅这里就不多录了.具体建议参考<Struts in Action>,不错的书.









 <Struts in Action>
 <精通Struts(基于MVC的Web 设计与开发)>