外文翻译----struts开发的最佳实践(编辑修改稿)内容摘要:

named BP3Form by extending . 2. In BP3Form, add methods with access modifiers as in public abstract void toSession(SessionData sessionData) and void fromSession(SessionData sessionData). 3. In every ActionForm, extend BP3Form and implement the abstract methods in which the form data is transported to and from the session. 4. The corresponding Action class may determine the order in which these methods are called. For example, you could invoke method toSession() on the ActionForm just before actionForward is determined. When to use this practice This practice is most useful when session data is maintained as a single object and/or every page manipulates or uses session data. Best Practice 4. Handle exceptions effectively Conventionally, when an application exception occurs in an Action class, the exception is first logged. Then the class creates anActionError and stores it in the appropriate scope. This Action class then forwards control to the appropriate ActionForward. Listing 3 shows how Action class handles exceptions. Listing 3. Exception handling in an Action class try { //Code in Action class } catch (ApplicationException e) { //log exception ActionErrors actionErrors = new ActionErrors()。 ActionError actionError = new ActionError(())。 (, actionError)。 saveErrors(request, actionErrors)。 } While conventional exception handling procedures save exception information in every Action class, best practice 4 aims to avoid redundant code while handling exceptions. To use this practice, Struts remends following these steps: 1. Create an Action class, say BP4Action, by extending . 2. Create all other Action classes in your Web application by extending BP4Action. 3. In BP4Action, declare variable ActionErrors actionErrors = new ActionErrors()。 . 4. In BP4Action, create a method performTask() as in public abstract ActionForward performTask(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, ActionErrors actionErrors) throws IOException, ServletException. 5. In BP4Action, declare method perform() as final. Then invoke generic methods, which must always be called before processing the request. Now you can call the method performTask() created in the previous step. 6. While implementing method performTask() in every Action class (by extending BP4Action), handle application exceptions as shown in Listing 4. Listing 4. Using ActionErrors effectively try { //Code in Action class } catch(ApplicationException appException) { //Log exception //Add error to actionErrors (, new ActionError(()))。 } In BP4Action, after invoking the method performTask(), save the ActionErrors using saveErrors(request, errors). Advantages This practice39。 s main advantage is that it avoids code redundancy in every Action class that handles ActionErrors. In conclusion Building an easily maintainable Web application can be one of the most challenging tasks for a development team. Using a mature framework like Struts helps you implement the infrastructure code normally associated with building an application. The St。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。