(Reposted) Struts Best Practices 1

xiaoxiao2021-03-06  51

Struts Best Practices

Build The Best Performing Large Applications

SummaryMultiple options are available for solving problems with Struts. When deciding among these alternatives, the choice must be based on parameters such as the scale of work and availability of time. However for large applications and the best quality-of-service needs, every decision becomes crucial and extra efforts are required to choose the appropriate solution to help you make these decisions, Puneet Agarwal discusses some of the best practices for developing Struts-based applications (2,800 words; September 13, 2004)..

By Puneet Agarwal

Page 1 of 4

True to the literal meaning of the word, "Struts" provides supporting building blocks and infrastructure components to build a Web-based application. It is an MVC-based (Model View Controller) open source framework developed and supported by the Apache Software Foundation. Because of its support for extensibility and plug-ins, the framework has picked up stupendous popularity among J2EE-based application developers. The framework can be extended and customized to suit a particular application need.

.

The Primary Sources of Information For this Article Are The Struts Users 'Mailing List, The Struts Developers' Mailing List, And My Experience With struts-based application.

The Article Discusses The Following Main Points:

Screens with dynamic fields Safeguarding JSP pages Error categorization Validation of service requester Application security Prepopulation Stack maintenance (for bread crumbs) Context-related problems Form-bean scope Data transfer object implementation Exceptions Action chainingScreens with dynamic fields Problem The Java Community Process (JCP) has released the Java Metadata Interface Specification, and some programmers are involved in the open source project Beehive. Both of these projects strive to reduce coding. However, the question is whether Struts has a facility that can be used for writing a generic JSP (JavaServer Pages ) page for specific types of screens in an application so that a separate JSP page does not have to be written for each screen. for example, to reduce our coding efforts, we might want to develop a generic JSP page for all search screens in .

Form Beans Are Classes That Must Have Getter and setter Methods for Every Field in JSP, and The Problem is how to write..

Struts Best Practice Possible Solutions Are:

Let the JSP page have fields in a specific pattern such as field1, field2, field3, and so on, and provide their getter and setter methods in the form bean. Here, the number of fields that can appear on the screen can not be more than . .. Utilize the indexed getter and setter method.

In the second approach, an increase in the number of fields in JSP requires no alteration in any component; therefore, it is the recommended best practice The implementation details follow:. Assuming an array of strings carries the resource IDs for all the dynamic fields in The form bean, the JSP page can be written as: Declare two methods in the form bean, as shown below These methods will work as the getter and setter methods for all the dynamic fields in the JSP page Whatever appears in small brackets- ().. -in front of dynaProperty (in the JSP page as shown above), is taken as key, and either the getDynaProperty () or setDynaProperty () method from the form bean is called. These values ​​should be stored in a HashMap against the key, Which can la RETRIEVEDIEVEDIAP AGAINST The key. public class testvar Form extends ActionForm {private HashMap hMap = new HashMap (); public testVarForm () {} public void setDynaProperty (String key, Object value) {this.hMap.put (key, value);} public Object getDynaProperty (String key) { Return this.hmap.get (key); public hashmap getHashmap () {returnim (HashMap newhmap) {this.hmap = newhmap;}}

Safeguard your JSP pages Problem When developers use Web-based applications, they often try to break into the security. The most common habit is to view the source of HTML in the browser and somehow determine the path of JSP pages and access them. The intent is to highlight the vulnerability of JSP pages accessible without authorization. Users who lack authorization to view the source might observe the source URL while sitting with another user who is authorized to work on that specific screen. Later, this unauthorized user could log in to the Application and Type The Url in The Browser. in Some Cases, Such Users Are Able To Make Their Way THROUGH.STRUTS BEST PRACTICE THE POSSIBLE SOLUTIONS THISPEM:

Do not let users access any JSP page directly The starting page can be an HTML document Add the following lines to the web.xml file to prevent users from accessing any JSP page directly:.. ... no_access *. jsp ... The MOST POPULAR OPTION IS TO Keep JSP Pages Behind The Web-Inf Folder. This Has A Few TradeOffs. for Example, You Cannot Take The JavaScript / CSS (Cascading Style Sheets) files behind WEB-INF, and if using Struts modules, you may encounter some context-related problems. Refer to the section "context-Related Problems," which appears later in this article, to circumvent such issues.

转载请注明原文地址:https://www.9cbs.com/read-117530.html

New Post(0)