Error Keys and ApplicationResources |
When an ActionError is created, you probably noticed that it is associated with two text Strings, the first one typically being the name of the field you are validating, and a second ‘dot separated’ text string that describes the error in some mysterious pseudo language. These error Strings, also known as a message keys, such as error.language.case and error.language.unsupported are supposed to map to message strings in the ApplicationResources.properties file. When the Struts framework is asked to output a message associated with an error, it uses this message key, finds the actual message associated with this message key in the ApplicationResources.properties file, and then prints out the associated message. The ApplicationResources.properties file includes entries such as: error.language.case = Use at least one upper case letter error.language.unsupported = Enter a supported language So, when an a user enters an unsupported language, they get an italicized message indicating that their language isn’t supported. |
Editing the ApplicationResources File | |
1. In the Navigator view of the Web perspective, open the PulpJavaWeb project. 2. Drill down under the ‘Java Resources’ folder and open the com.ibm.pulpjavaweb.resources package | |
3. Double-click on the ApplicationResources.properties file to open up the associated editor | |
4. Add the error keys used in the validate method of the LanguageForm Associate your error keys with an appropriate error message | |
error.language.case =Start your language with an upper case letter error.language.unsupported=You entered and unsupported language | |
5. Uncomment the header and footer line for the | |
6. Click File à Save All or CTRL+S to save your changes | |
Providing Feeback to You Users | |
Currently, when validation of user data fails, we simply send the user to a page that informs them that they are in idiot. However, it would be nice to use the message keys and associated messages to provide dynamic feedback to the user. You’ll be amazed how easy it is to do this | |
1. Navigate to the ‘Project Navigator’ view of the Web perspective 2. Find the badlanguage.jsp file under the PulpJavaWeb \ WebContent directory 3. Double-click on the badlanguage.jsp file to bring up the associated WYSIWYG editor, aka ‘Page Designer’ | |
4. In the Design view of the html editor, right-click anywhere on the page and select ‘Page Properties’ | |
5. Move to the ‘JSP Tags’ tab of the ‘Page Properties’ window and make sure the struts-html.tld exists as a taglib directive. Click ‘OK’ to return to ‘Page Designer’ | |
6. On the Design view of the html editor, click to a point where you want error messages to display. 7. From the menu bar, select JSP à Instert Custom… | |
8. From the ‘Instert Custom Tag’ wizard, higlight the struts-html.tld reference in the left pane, and select the errors tag in the right hand pane. 9. Click ‘Insert’ and then ‘Close’ to return to ‘Page Designer’ | |
10. A list of error messages will appear where you inserted your custom tag. This will translate into actual error messages from the ApplicationResources file at runtime. | |
11. Stop and start your test server, and then test your application with good and bad data. When bad data is entered, you should see the error messages that are entered in your ApplicationResources.properties file |
Hibernate Tutorials
Sun Certified Java Associate Mock Exams and Tutorials