【正文】
A great deal of change has occurred between Struts and . You may want to download the most current Struts nightly distributions, to avoid deprecated methods. In the last 6 months, I have seen the Struts library grow from 90K to over 270K. I had to modify my examples several times because of changes in Struts, and I am not going to guarantee my examples will work with the version of Struts you download. Manage the problem spaceDivide and conquer is a nice way of solving the problem and making the problem manageable. Of course, the sword cuts both ways. The problem is more plex and needs more management. Struts cons Open sourceYou have all the advantages of open source, such as being able to see the code and having everyone else using the library reviewing the code. Many eyes make for great code review. Use of JSP tag mechanismThe tag feature promotes reusable code and abstracts Java code from the JSP file. This feature allows nice integration into JSPbased development tools that allow authoring with tags. UserActionForm can be mapped over multiple pages for things such as wizards. The Action classThe Action class is a wrapper around the business logic. The purpose of Action class is to translate the HttpServletRequest to the business logic. To use Action, subclass and overwrite the process() method. The ActionServlet (Command) passes the parameterized classes to ActionForm using the perform() method. Again, no more dreadful () calls. By the time the event gets here, the input form data (or HTML form data) has already been translated out of the request stream and into an ActionForm class. Note: Think thin when extending the Action class. The Action class should control the flow and not the logic of the application. By placing the business logic in a separate package or EJB, we allow flexibility and reuse.Another way of thinking about Action class is as the Adapter design pattern. The purpose of the Action is to Convert the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn_t otherwise because of inpatibility interface (from Design Patterns Elements of Reusable OO Software by Gof). The client in this instance is the ActionServlet that knows nothing about our specific business class interface. Therefore, Struts provides a business interface it does understand, Action. By extending the Action, we make our business interface patible with Struts business interface. (An interesting observation is that Action is a class and not an interface. Action started as an interface and changed into a class over time. Nothing39。 The file controls which HTML form request maps to which ActionForm. Before passing it to the Action class, Struts will also conduct form state validation by calling the validation() method on : This is not always wise to do. There might be ways of using UserActionForm in other pages or business objects, where the validation might be different. Validation of the state might be better in the UserAction class. Struts will set the state of the UserActionForm using corresponding fields from the HttpServletRequest. No more dreadful () calls. For instance, the Struts framework will take fname from request stream and call (). Check to see if a UserActionForm exists。 The page designer does not have to wade through Java code to understand the flow of the application. HTML embedded into code is ugly. For the Web, the classical form of MVC needed to change. Figure 1 displays the Web adaptation of MVC, also monly known as MVC Model 2 or MVC 2. Figure 1. MVC Model 2Struts, an MVC 2 implementationStruts is a set of cooperating classes, servlets, and JSP tags that make up a reusable MVC 2 design. This definition implies that Struts is a framework, rather than a library, but Struts also contains an extensive tag library and utility classes that work independently of the framework. Figure 2 displays an overview of Struts. Figure 2. Struts viewStruts overview l Client browser An HTTP request from the client browser creates an event. The Web container will respond with an HTTP response. l ControllerThe Controller receives the request from the browser, and makes the decision where to send the request. With Struts, the Controller is