<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    http://www.ftponline.com/javapro/2002_05/magazine/columns/weblication/default.aspx


    The Jakarta Stuts project takes care of some of the details when combining servlets and JavaBeans with JavaServer Pages
    by Peter Varhol

    May 2002 Issue

    The Model-View-Controller (MVC) architecture leverages the strengths of servlets and JavaServer Pages (JSP), while minimizing their weaknesses. In essence, user requests are sent to a controller servlet, which determines the nature of the request and passes it off to the appropriate handler for that request type. Each handler is associated with a particular model, which encapsulates business logic to perform a specific and discrete set of functions. Once the operation is completed, the results are sent back to the controller, which determines the appropriate view and displays it (see my Weblication column "Strut Your Stuff," April 2002).

    Struts, a Jakarta project, provides a framework for writing applications using the MVC architecture. Struts uses "ActionMapping," which enables the servlet to turn user requests into application actions. ActionMapping usually specifies a request path, the object type to act upon the request, and other properties as needed.

    The Action object used as a part of the ActionMapping is responsible for either handling the request and sending the response back to the appropriate view (normally a Web browser), or passing the request along to the appropriate model.

    The bridge between the model and the view is a form bean that can be created by subclassing org.apache.struts.action.ActionForm. The form bean can be used to hold data from the user prior to processing, or from a model prior to display back to the user. Struts includes custom tags that can automatically populate fields from the form bean created.

    In practice, here's an outline of how Struts may work. A user enters a request on a JSP page for, say, information on train schedules between two cities. The controller servlet receives the request and determines where in the application it can be processed. The Action object passes the request on to a JavaBean that contains the appropriate schedule-retrieving business logic. That business-logic bean will connect to and query the database, receive the results, and return the results to the Action object. The Action object stores the result in a form bean as a part of the request. Once all of the data needed to fulfill the request has been collected, it's ready to be formatted and displayed. The last step is when the JavaServer Page displays the result to the view in HTML form.

    The Controller, Model, and View
    The primary component of the controller in Struts is the servlet defined from the class ActionServlet, which is configured by the ActionMappings. The ActionMapping class represents the name and location of the Action object. When a request comes into the controller, it maps the path of the request to the location of the Action, and the request is passed off to that Action. Struts' ActionMapping classes may also contain other information that may be unique to your application, like local variables, environment-specific data, or other URIs.

    The activities surrounding the controller are the key to Struts. The Struts controller servlet maps events to classes (an event generally being an HTTP POST, GET, or similar request). ActionServlet is the command part of the MVC design pattern and is the core of the Struts framework. ActionServlet creates and uses Action, an ActionForm, and ActionForward. The struts-config.xml file configures the Action. During the creation of the Web application, you extend Action and ActionForm to solve the problem of how to respond to a user's request. The struts-config.xml file instructs ActionServlet on how to use the extended classes. You can also extend ActionServlet to provide your Struts application with custom features.

    This approach has several advantages. First, the entire logical flow of the application is in a hierarchical text file. This makes it easier to view and understand, especially with large applications. Second, the HTML writer doesn't have to search through Java code to understand the flow of the application to make page changes, and the Java developer doesn't have to recompile code when making flow changes.

    ?ActionForm maintains the session state for the Web application. ActionForm is an abstract class that is subclassed for each input form model. It represents a general concept of data that is set or updated by an HTML form. For instance, your application may have a UserActionForm that is set by an HTML Form. The Struts framework will check to see if a UserActionForm exists; if not, it creates an instance of the class. Struts will set the state of the UserActionForm using corresponding fields from the HttpServletRequest. The Struts framework updates the state of the UserActionForm before passing it to the business wrapper UserAction.

    The Struts model consists of the state of the system and the actions that can be performed on it. You can use a wide variety of structures to represent the model (other servlets or JSP, for example), but most of the time you'll use JavaBeans. The JavaBean properties—or data drawn from external data sources in the case of Enterprise JavaBeans (EJBs)—represent the state, while the methods represent the actions that can be performed. The actions do not need to be defined by JavaBean methods; in simple cases, the actions can be embedded into the Action object, although this tends to blur the distinction between processing and orchestration.

    The view of a Struts MVC application typically is constructed using JSP, which provide for a way of statically formatting pages using HTML or XML, plus a method for dynamically inserting customized content in response to a user request. A key aspect of Struts is its custom tag library, which provides a way to create user interfaces easily using JSP.

    ?The Struts framework includes custom tag libraries, which are used in a variety of ways. Although these libraries aren't required to use the framework, they contain tags that will be useful in many of your applications. Some of the Struts tag libraries included are:

    ??? * struts-html tag library. This library can be used for creating dynamic HTML user interfaces and forms.
    ??? * struts-bean tag library. This library provides substantial enhancements to the basic capability of bringing JavaBean code into a JSP page, which is provided by <jsp:useBean> tag.
    ??? * struts-logic tag library. This library can manage conditional generation of output text, looping over object collections for repetitive generation of output text, and application flow management.
    ??? * struts-template tag library. This library contains tags that are useful in creating dynamic JSP templates for pages that share a common look and feel, or common format.

    You use these tag libraries just as you would any such library. Because the library is already written, all you have to do is tell the servlet engine about it. In Tomcat, you use the <taglib> tag in the web.xml file to specify the URI of the tag library, and the location of the tag library descriptor file on the Web server system.

    The Small Print
    The Jakarta project enables you to download either a binary distribution of Struts, or build it directly from source code. The binary usually works just fine, but if you have an unusual software platform, or want to build it as a learning experience, it's not difficult to do. Whichever you decide, Struts has several software prerequisites:

    #? Java Development Kit (JDK). You have to download and install a Java 2 JDK implementation for your operating-system platform.
    # Servlet container. You also have to download and install a servlet container that is compatible with the Servlet API specification, version 2.2 or later, and the JSP specification, version 1.1 or later. One good choice is to download Apache's Tomcat, which provides the ability to both serve Web pages and run servlets and JSP.
    # XML parser. Struts requires the presence of an XML parser that is compatible with the Java API for XML Parsing (JAXP) specification, 1.1 or later.
    # Servlet API classes. To compile Struts, or applications that use Struts, you will need a servlet.jar file containing the servlet and JSP API classes. Most servlet containers include this JAR file.
    # JDBC 2.0 optional package classes. Struts supports an optional implementation of javax.sql.DataSource, so it requires that the API classes be compiled. You can download these package classes from http://java.sun.com/products/jdbc/download.html.

    To use Struts to build an application, you need to follow these steps. First, copy the files lib/commons-*.jar and lib/struts.jar from the Struts distribution into the WEB-INF/lib directory of your application. Then copy the entire tag library descriptor file in lib/struts-*.tld from the Struts distribution into the WEB-INF directory of your Web application.

    Once you have the files copied over, you can modify the web.xml file for your Web application to include a <servlet> element to define the controller servlet, and a <servlet-mapping> element to establish which request URIs are mapped to this servlet. If you are doing a standard installation with default directories, you can use the web.xml file from the Struts example application for an example of how to do this. Modify the web.xml file of your Web application to include the appropriate tag library declarations. Once again, you can follow the example of these declarations in the Struts example application.

    After finishing the web.xml file, create a file called struts-config.xml in the WEB-INF directory that defines the action mappings and other characteristics of your specific application. Last, at the top of each JSP page that will use the Struts custom tags, add lines declaring the Struts custom tag libraries used on that particular page.

    Struts was named for the type of supporting wires and frameworks used in buildings and old airplanes. Its intent is to provide a software framework to help you overcome the time-consuming aspects of applying the MVC design pattern in Web applications. You still have to learn and apply the framework, but it will accomplish some of the heavy lifting. If you want to build scalable applications combining the advantages of both servlets and JSP, Struts can get you a good part of the way there.

    About the Author
    Peter Varhol is a technical evangelist for Compuware Corporation. You can reach him at Peter.Varhol@ compuware.com.

    posted on 2006-12-14 16:36 華宗林 閱讀(467) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
     
    主站蜘蛛池模板: 啦啦啦中文在线观看电视剧免费版 | 国产免费人成视频在线播放播| 影音先锋在线免费观看| 亚洲伊人久久大香线蕉| 日韩不卡免费视频| 亚洲AV日韩综合一区尤物 | 最近免费中文字幕mv电影| 久久夜色精品国产噜噜亚洲AV| 免费无码又爽又刺激高潮视频| 亚洲2022国产成人精品无码区| 色欲国产麻豆一精品一AV一免费| 老司机亚洲精品影院无码| 国产一卡2卡3卡4卡无卡免费视频| 亚洲国产成人无码av在线播放| 免费做爰猛烈吃奶摸视频在线观看| 亚洲色欲色欱wwW在线| 手机看片久久国产免费| 一级A毛片免费观看久久精品| 亚洲深深色噜噜狠狠爱网站| 久久精品视频免费播放| 亚洲一级高清在线中文字幕| 日本免费一区尤物| 国产VA免费精品高清在线| 亚洲福利在线观看| 久久久久久99av无码免费网站| 亚洲精品GV天堂无码男同| 亚洲国产精品自产在线播放| 久久精品一区二区免费看| 亚洲性69影院在线观看| 免费人成在线观看播放国产| 怡红院免费全部视频在线视频| 久久亚洲精品无码VA大香大香| 成年女人18级毛片毛片免费观看| 福利片免费一区二区三区| 久久精品国产精品亚洲艾草网| 91麻豆最新在线人成免费观看| 国产成人人综合亚洲欧美丁香花 | 亚洲精品无码久久久久久久| 青青草国产免费久久久下载| a级片免费在线播放| 中文有码亚洲制服av片|