Configuration . Use global-forwards/results Helps to avoid duplicate jsp files and redundancy forward mapping in all the actions in struts.xml E.gGlobal error pagesSuccess message pages.. Base class . Have a Base class for each of the layers usedThis will help -common operations need to
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
1. BEST PRACTICES - Java
2. Configuration Use global-forwards/results
Helps to avoid duplicate jsp files and redundancy forward mapping in all the actions in struts.xml
Global error pages
Success message pages.
3. Base class Have a Base class for each of the layers used
This will help -
common operations need to be included in all the classes in each layer can be defined/implemented in the base class to reduce code redundancy
E.g. Methods to read data from properties
Have base class implement multiple interfaces required, so that each subclass need not implement them.
E.g. In struts2.0 base action class can implement multiple interfaces like SessionAware, ServletRequestAware, ServletResponseAware, ServletRequestAware, etc.
4. Application start up servlet Create a servlet class and have it invoked while application status up.
Include methods Ė
To load static data from data base which is frequently accessed. This will reduce the DB calls
To read properties from property file. This will eliminate the I/O operation, when ever a property value is need to be read and used.
5. Exception handling Define a Application level Exception handling mechanism, and configure struts global exception handler.
Donít throw multiple exception to the calling method in other layerís class (e.g. business class method calling a DAO class method) to handle, Instead wrap to a user defined Exception and re throw, so that the calling method need not to handle multiple exceptions and also it eliminates the layer coupling.
Donít handle any exception other than in action layer Unless if you know what should be done on the exception condition (e.g it may call another business case on exception)
Donít handle all the exception in action class, let global exception handle the exception and forward to common error page unless if you want to catch a specific exception and show custom error message to the user.
6. Logging Reduce unnecessary logger statements. More the logger statement lesser the performance
Avoid logging same date in multiple places of a single sequence in same class
Use appropriate logger level for logging information.
Donít log each data in a separate logger statement instead concat and log them into a single logger statement.
Have maximum only one INFO level logger statement in layers other than DAO to identify data flow
DAO layer you method start time and end time can be in INFO.
7. Java Server Pages Use a global error page.
Always link to Actions, never to server pages.
Use the action attributes tag to avoid embedding Action extension.
Use s:url to reference HTML assets.
Use s:include to include pages
8. Handle Duplicate Form Submission The problem of duplicate form submission arises when a user clicks the Submit button more than once before the response is sent back.
Handle it by using the Token interceptor, tokenSession. This checks for valid token presence in action, stores the submitted data in session when handed an invalid token and prevents double submission.
Donít use button type as submit while submitting a form thorough script to avoid duplicate form submit.
9. Use html:messages (instead of html:errors) For displaying error messages to the end user, use html:messages instead of html:errors