1、 信息 科学与工程 学院毕业设计科技文献翻译 StrutsAn Open-source MVC Implementation Struts一种开源 MVC的实现 姓 名 张 三 丰 学 号 20140000 年 级 2014级 专 业 计算机科学与技术 指导教师 李 双 双 2018年 3月 Struts An Open-source MVC Implementation This article introduces Struts, a Model-View-Controller implementation that uses servlets and JavaServer Pages (
2、JSP) technology. Struts can help you control change in your Web project and promote specialization. Even if you never implement a system with Struts, you may get some ideas for your future servlets and JSP page implementation 1. Introduction If you have worked on a large-scale Web application, you u
3、nderstand the term change. Model-View-Controller (MVC) is a design pattern put together to help control change. MVC decouples interface from business logic and data. Struts is an MVC implementation that uses Servlets 2.2 and JSP 1.1 tags, from the J2EE specifications, as part of the implementation.
4、You may never implement a system with Struts, but looking at Struts may give you some ideas on your future Servlets and JSP implementations. 2. Model-View-Controller (MVC) JSP tags solved only part of our problem. We still have issues with validation, flow control, and updating the state of the appl
5、ication. This is where MVC comes to the rescue. MVC helps resolve some of the issues with the single module approach by dividing the problem into three categories: Model The model contains the core of the applications functionality. The model encapsulates the state of the application. Sometimes the
6、only functionality it contains is state. It knows nothing about the view or controller. View The view provides the presentation of the model. It is the look of the application. The view can access the model getters, but it has no knowledge of the setters. In addition, it knows nothing about the cont
7、roller. The view should be notified when changes to the model occur. Controller The controller reacts to the user input. It creates and sets the model. 3. Struts Summary 3.1 Struts details Displayed in Figure 1 is a stripped-down UML diagram of the org.apache.Struts.action package. Figure 6 shows th
8、e minimal relationships among ActionServlet (Controller), ActionForm (Form State), and Action (Model Wrapper). Figure 1 UML diagram of the relationship of the Command (ActionServlet) to the Model (Action if not, it will create an instance of the class. Struts will set the state of the UserActionForm
9、 using corresponding fields from the HttpServletRequest. No more dreadful request.getParameter() calls. For instance, the Struts framework will take fname from request stream and call UserActionForm.setFname(). The Struts framework updates the state of the UserActionForm before passing it to the bus
10、iness wrapper UserAction. Before passing it to the Action class, Struts will also conduct form state validation by calling the validation() method on UserActionForm. Note: This is not always wise to do. There might be ways of using UserActionForm in other pages or business objects, where the validat
11、ion might be different. Validation of the state might be better in the UserAction class. 3.1.3 The Action class The 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
12、 process() method. The ActionServlet (Command) passes the parameterized classes to ActionForm using the perform() method. Again, no more dreadful request.getParameter() calls. By the time the event gets here, the input form data (or HTML form data) has already been translated out of the request stre
13、am and into an ActionForm class. 3.1.4 The Error classes ActionError encapsulates an individual error message. ActionErrors is a container of ActionError classes that the View can access using tags. ActionErrors is Struts way of keeping up with a list of errors. Figure 2 UML diagram of the relations
14、hip of the Command (ActionServlet) to the Model (Action) 3.1.5 The ActionMapping class An incoming event is normally in the form of an HTTP request, which the servlet Container turns into an HttpServletRequest. The Controller looks at the incoming event and dispatches the request to an Action class.
15、 The Struts-config.xml determines what Action class the Controller calls. The Struts-config.xml configuration information is translated into a set of ActionMapping, which are put into container of ActionMappings. (If you have not noticed it, classes that end with s are containers) The ActionMapping
16、contains the knowledge of how a specific event maps to specific Actions. The ActionServlet (Command) passes the ActionMapping to the Action class via the perform() method. This allows Action to access the information to control flow. 3.2 Struts pros 3.2.1 Use of JSP tag mechanism The tag feature pro
17、motes reusable code and abstracts Java code from the JSP file. This feature allows nice integration into JSP-based development tools that allow authoring with tags. 3.2.2 Tag library Why re-invent the wheel, or a tag library? If you cannot find something you need in the library, contribute. In addit
18、ion, Struts provides a starting point if you are learning JSP tag technology. 3.2.3 Open source You 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. 3.2.4 Sample MVC impleme
19、ntation Struts offers some insight if you want to create your own MVC implementation. 3.2.5 Manage the problem space Divide 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 complex and needs more management.
20、3.3 Struts cons 3.3.1 Youth Struts development is still in preliminary form. They are working toward releasing a version 1.0, but as with any 1.0 version, it does not provide all the bells and whistles. 3.3.2 Change The framework is undergoing a rapid amount of change. A great deal of change has occ
21、urred between Struts 0.5 and 1.0. 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
22、 going to guarantee my examples will work with the version of Struts you download. 3.3.3 Correct level of abstraction Does Struts provide the correct level of abstraction? What is the proper level of abstraction for the page designer? That is the $64K question. Should we allow a page designer access
23、 to Java code in page development? Some frameworks like Velocity say no, and provide yet another language to learn for Web development. There is some validity to limiting Java code access in UI development. Most importantly, give a page designer a little bit of Java, and he will use a lot of Java. I
24、 saw this happen all the time in Microsoft ASP development. In ASP development, you were supposed to create COM objects and then write a little ASP script to glue it all toge ther. Instead, the ASP developers would go crazy with ASP script. 3.3.4 Limited scope Struts is a Web-based MVC solution that
25、 is meant be implemented with HTML, JSP files, and servlets. 3.3.5 J2EE application support Struts requires a servlet container that supports JSP 1.1 and Servlet 2.2 specifications. This alone will not solve all your install issues, unless you are using Tomcat 3.2. I have had a great deal of problem
26、s installing the library with Netscape iPlanet 6.0, which is supposedly the first J2EE-compliant application server. I recommend visiting the Struts User Mailing List archive (see Resources) when you run into problems. 3.3.6 Complexity Separating the problem into parts introduces complexity. There i
27、s no question that some education will have to go on to understand Struts. With the constant changes occurring, this can be frustrating at times. Welcome to the Web. 4. Future of Struts Things change rapidly in this new age of software development. In less than 5 years, I have seen things go from cg
28、i/perl, to ISAPI/NSAPI, to ASP with VB, and now Java and J2EE. Sun is working hard to adapt changes to the JSP/servlet architecture, just as they have in the past with the Java language and API. You can obtain drafts of the new JSP 1.2 and Servlet 2.3 specifications from the Sun Web site. Additional
29、ly, a standard tag library for JSP files is appearing. 外文资料翻译译文 Struts一种开源 MVC的实现 英文作者: Malcolm Davis 翻译:马运东 文献来源 : 按毕业论文参考文献格式注明 该 外文文献来源 这篇文章介绍 Struts,一个使用 servlet和 JavaServerPages技术的一种 Model-View-Controller的实现。 Struts可以帮助你控制 Web项目中的变化并提高专业化。即使你可能永远不会用 Struts实现一个系统,你可以获得一些想法用于你未来的 servlet和 JSP网页的实
30、现中。 1. 简介 如果你开发过大型 Web应用程序,你就理解 “变化 ”这个词语。 “模型 -视图 -控制器 ”(MVC)就是用来帮助你控制变化的一种设计模式。 MVC 减弱了业务逻辑接口和数据接口之间的耦合。Struts是一种 MVC 实现,它将 Servlet2.2和 JSP1.1标记(属于 J2EE规范)用作实现的一部分。你可能永远不会用 Struts实现一个系统,但了解一下 Struts或许使你能将其中的一些思想用于你以后的 Servlet和 JSP实现中。 2. 模型 -视图 -控制器 (MVC) JSP标签只解决了我们问题中的一部分。我们依然有验证、流控制、以及更新应用程序结构的
31、问 题。 MVC 通过把问题分成三类来帮助解决一些与单模块相关的问题: Model(模型) 模块包括应用程序功能的核心。模型封装着应用程序的各个结构。有时它所包含的唯一功能就是结构。 View(视图) 视图提供了模型的演示。它是应 用程序的外表。视图可以进入模型获得者,但是它对于设置者一无所知 。视图仅仅当模型发生改变的时候才被通知。 Controller(控制器) 控制器对于用户的输入做出反应。它创造和设置模型。 3. Struts 概览 3.1 Struts 详细资料 在图 6中展示了一个无其他附属设备的阿帕奇 Struts的 action包的 UML图表。图 1显示了 ActionSer
32、vlet(Controller)、 ActionForm(FormState)和 Action(ModelWrapper)之间的最小关系。 图 1 命令 (ActionServlet)与模型 (Action&ActionForm)之间的关系 的 UML图 3.1.1 ActionServlet类 现在日子好过多了,自从我们有了 Java 技术、 XML、 J2EE 等等之后。 Struts 控制器是一个映射事件(事件通常是一个 HTTPpost)到类的一个 servlet。猜猜怎么着 -控制器用一个配置文件以致于你不必非硬编码这些值。生活变了,但方法依然如此。 ActionServlet是 M
33、VC 实现的命令部分并且它是框架的核心。 ActionServlet(Command)创建并使用 Action、 ActionForm和 ActionForward。正如前面所提及的, Struts-config.xml文件配置Command。在 Web工程创建期间, Action和 ActionForm被扩展用来解决特殊的问题空间。文件 Struts-config.xml指导 ActionServlet如何扩展这些类。这种方法有几个优点: 1)网页设计人员不必费力地通过 Java代码来理解应用程序的流程。 2)当流程发生改变时 Java开发人员不需要重新编译代码。 3)通过扩展 Action
34、Servlet命令函数可以被添加进来。 3.1.2 ActionForm类 ActionForm维持着 Web应用程序 的会话状态。 ActionForm是一个必须为每个输入表单模型创建该类的子类的抽象类。当我说输入表单模型时 ,我就是说 ActionForm 代表了一个由HTML 表单设置或更新的一般意义上的数据。例如,你可能有一个由 HTML 表单设置的UserActionForm。 Struts框架将会: 检查 UserActionForm是否存在;如果不存在,它将会创建该类的一个实例。 Struts将使用HttpServletRequest中相应的域设置 UserActionForm的
35、状态。没有太多糟糕的请求 .getParameter()调用。例如, Struts框架将从请求流中提取 fname并调用 UserActionForm.setFname()。 Struts框架在将在传递它到业务包装 UserAction之前将更新 UserActionForm的状态。在传递它到 Action类之前, Struts将还会对 UserActionForm调用 validation()方法进行表单验证。 备注:这样做通常并不明智。别的网页或业务对象可能有方法使用 UserActionForm,然而验证可能不同。在 UserAction 类中进行状态验证可能更好。 UserAction
36、Form 能够维持一个会话级别。 Struts-config.xml文件控制着 HTML表单请求与 ActionForm之间的映射。多重请求会被映射到 UserActionForm。 UserActionForm可被映射到诸如向导之类的多重页面的东西上。 3.1.3 Action类 Action 类是一个围绕业务逻辑的一个包装器。 Action 类的目的就是将 HttpServletRequest翻译给业务逻辑。要使用 Action,需重写 process()原理。 ActionServlet(命令 )通过使用 perform()原理将参数化的类传递给 ActionForm。此外,没有太多讨厌
37、的 request.getParameter()调用。通过事件到达这里的时间,输入表单数据(或 HTML表单数据)已经被从请求流中翻译出来并进入 ActionForm类中。 备注:扩展 Action类时请注意简洁。 Action类应该控制应用程序的流程,而不应该控制应用程序的逻辑。通过将业务逻辑放在单独的包或 EJB 中,我们就可以提供更大的灵活性和可重用性。 3.1.4 Error类 ActionError 封装了单个错误消息。 ActionErrors 是 ActionError 类的容器, View 可以使用标记访问这些类。 ActionError是 Struts保持错误列表的方式。 图 2 Command(ActionServlet)与 Model(Action)之间的关系的 UML图 3.1.5 ActionMapping类 输入事件通常是在 HTTP 请求表单中发生的, servlet 容器将 HTTP 请求转换为HttpServletRequest。控制器查看输入事件并将请求分派给某个 Action 类。 Struts-config.xml 确