If you want to use your Custom Request Processor along with your Tiles Request Processor it will give the error.
This error comes when you extend your custom request processor from the RequestProcessor class. This is because of the bug in Tiles Specification which forces us to write our own custom request processor, for which the TilesRequestProcessor must be the superclass. So if you want to use your custom request processor, say for example using Struts Faces, you must extend the TilesRequestProcessor.
For example, I want to use the JavaServer Faces along with the Struts. So I am using Struts Faces to integrate both Struts & JavaServer Faces. So every request from the jsp should be processed by the Faces Request Processor. So in my web.xml, I write the controller for them as below. Note that the controller processorClass has the value FacesTilesRequestProcessor which extends the TilesRequestProcessor.
<controller>
<!-- The "input" parameter on "action" elements is the name of a local or global "forward" rather than a subapp-relative path -->
<set-property value="true" property="inputForward">
<set-property value="org.apache.struts.faces.application.FacesTilesRequestProcessor" property="processorClass">
</controller>
<plug-in classname="org.apache.struts.tiles.TilesPlugin">
<set-property value="/WEB-INF/tiles-defs.xml" property="definitions-config">
<set-property value="true" property="moduleAware">
<set-property value="true" property="definitions-parser-validate">
</plug-in>
The most ccommon exception when using Tiles in a Struts application is theJasper Exception. For example look into the exception below.
javax.servlet.ServletException: Can't get definitions factory from context
This exception can occur if the Tiles Servlet entries are wrong. We can easily solve the exception by modifying the Tiles servlet with the correct entries.
Example:
<servlet>
<servlet-name>TilesServlet</servlet-name>
<servlet-class>org.apache.struts.tiles.TilesServlet</servlet-class>
<init-param>
<param-name>definitions-config</param-name>
<param-value>/WEB-INF/tiles-defs.xml</param-value>
</init-param>
<init-param>
<param-name>definitions-parser-validate</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
- Fujaba Tool Suite combines UML Class diagrams and UML Behaviour diagrams(Story Diagrams) to a powerful, easy to use, yet formal system design and specification language.
- Fujaba Tool Suite supports the generation of Java sourcecode out of the whole design which results in an executable prototype.
- Moreover the way back is provided, too (to some extend so far, not for productive use), so that Java sourcecode can be parsed and represented within UML.
- The Fujaba Tool Suite RE Edition is especially configured with plugins for Reverse Engineering and Design Pattern recognition.
Here is the sample structure of the Application:
formbeans: which stores the form data of the user
dto : The form we used for our business logic. It is recomonded to have no int etc. variables in form beans as it leads to conversion of user data which may fail if user enter wrong data. So we move this validation part to our dto's. All the fields of dto are clones of form beans except the return type.
mapper: these will map the form bean data to dto. We can use copyproperties() method of BeanUtils class to copy the data from form bean to dto.
actions: This package contains all the acction classes
dao: This package contains the Interfaces to invoke the backend data
dao-implementation: This package is the implementation of dao package.
service: This package is used to access the backend logic from our actions etc.
All jsp pages should go under some other directory either under root or WEB-INF.
Always use comments for your class, methods and variables
One of the best practice is using comments wherever posible. It will make your more understandable not only for you, but also for other developers who use your code.
The another advantage of using the comments is Java Doc. You can generate the Java Doc with a click of your mouse in any IDE which supports it like Eclipse.
Here are the few examples how to use comments in a better way.
Class Comments:
/**
* This class is used to perform operations on database at back-end.
* 1. Updating the database
* 2. Retrieving the data from database
*/
public class DatabaseOperations{
...
}
Method Comments:
/**
* This method is used to updating the database with the data
* given in the form of xml as a parameter.
* @params strXMLQuery the operation to be performed - in the form of xml.
* @returns boolean ...
*/
public boolean DatabaseUpdate(String strXMLQuery){
...
}
Variable Comments:
/** Status of the email with the database*/
private static final boolean DATABASE_STATE_EMAIL;
Always use meaning full names for your classes, methods and variables
Using meaningful names to your methods or variables not only simplyfies your coding tasks but also it makes your code more readable and undestandable.
For example, to declare a method which performs uploading a file from client machine to server, rather than declaring it as upload() declare it as uploadFileToServer() which conveys better meaning and simplifies the task of writing other methods which uses this method.
Do not extend a line to more than acceptable number of characters
The source files should be more readable in order to improve your productivity. One of such activity is writing code in a way such that no line shoud exxtend more than the screen width. i.e you should not drag the horizontal scrollbar in order to see the entire line.
For this you should follow strict line width. Say, Maximum 80 characters per line. If the line exceeds this width, simply devide that line into two parts. That can be done in two ways.
- Simplify your code so that one line of code can be written in two lines.
- Wrap the line to second line.
The first way will increase the ease in your code but at the same time you should be very careful. Using this technique excessively will make your code unmanageable.
boolean result = ((test1 != var1 && test2 == var2) || test3 > var3) && test4 == var4)
The above code can be written as below
boolean flag1 = (test1 != var1 & test2 == var2)
boolean flag2= flag1 || (test3 > var3)
boolean result = flag2 && (test4 == var4)
The second technique is far simpler and recomonded. For example, to write
String strResult = (null != strVar1 && null != strVar2 && strVar1.equalsIgnoreCase(strVar2)) ? true : false;
Simply expand it to multiple line as below..
boolean strResult = (null != strVar1 &&
null != strVar2 &&
strVar1.equalsIgnoreCase(strVar2)
) ? true : false;
Use a formatter like jelopy, if posible, to format the java files
Formatting the code using a tool will save lots of time as well as makes the code beautiful so that we can easily understand the code whenever we see it. The tools like Jaloppy are very useful in this aspect. They are not only freeware but also very powerful.
Always use Constants on left side while comparing with variables
Using variables on left side in a comparision will lead to unpredictable results in some cases. For example the code below explains how it is.
intResult = strVar01.equals(CONST_VAR02);
Tips while using multiple comparisions
Is it always suggested that before performing any opertions on an Object, first check posible errors which can be avoided. Say for example, you are comparing a string value to check whether it is empty or null. It is good to test for null first before empty value. This will avoid the null pointer exceptions.
If you use to test the empty value first, if the value is null then there is a probability to occur the error. If you test null value before that then this case can be avoided.
Example:
boolResult = (null == strVar01 strVar01.equals(""))?true:false;
Never use generic exception to catch exceptions
Always use the speccific exception for the perticular operation so that you can easily debug the error and easily solve if any bug is detected. This will drastically reduce the maintainability. It is a good habit to catch exceptions like null pointer exception wherever possible.
Example:
try{
intResult = Integer.parseInt(strVar01);
}catch(NullPointerException objNullPointerException){
//Do what you want to do..
}