The Struts framework provides a set of built-in Tag libraries that allow you to build the view part of the MVC without embedding Java code directly within your application JSPs. Some commonly used struts tag libraries are described below.
The Bean Tags The Tags within the Bean Library are used for creating and accessing JavaBeans and a few other general purpose uses. Although these tags work with any standard JavaBean,they are often used with Objects that extend the Struts ActionForm class.
The HTML Tags The Tags within the Struts HTML Tag Library are used to create input forms for your application. There are also a few other useful Tags used in the creation and rendering of HTML-based user interfaces.
The Logic Tags The Logic Tag Library contains tags that are helpful with iterating through collections,conditional generation of output,and application flow.
We have already used the ActionForm,but we actually have not enjoyed the blessings of struts. Let’s program our action to return an appropriate error message called ‘ActionError’ when the user fails to login successfully. Also re-arrange our view (the index.jsp) to use the struts tag libraries to manipulate the ActionForm values,and display the appropriate error message on error.
After adding the mentioned code,the execute method of LoginAction becomes:
public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) { User userForm = (User)form; String user = userForm.getUserId(); String password = userForm.getPassword(); if(user.equals("admin") && password.equals("admin")) return mapping.findForward("success"); ActionMessages errors = new ActionMessages(); ActionMessage error = new ActionMessage("login.failed"); errors.add("error",error); saveErrors(request,errors); return mapping.findForward("failure"); }
Note that we have created a new ActionMessage with a key “login.failed”. This key must exist in the ApplicationResources.properties file. Add the following line in the resource file.
login.failed = The supplied user name / password does not match
And the index.jsp becomes:
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@taglib prefix="html" uri="/WEB-INF/struts-html.tld" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Welcome to the registration case study</title> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body>
<html:errors/>
<html:form action="/login"> <table border="0"> <tr> <td>Login:</td> <td><html:text property="userId" /></td> </tr> <tr> <td>Password:</td> <td><html:text property="password" /></td> </tr> <tr> <td colspan="2" align="center"><html:submit value="Login"/></td> </tr> </table> </html:form> </body> </html>
As you can see,we have declared a tag library ‘struts-html.tld’. This is the Html tag library we talked about a while ago. The tag <html:errors/> is responsible for displaying the message that we may have saved in the request or the session. We can specify the scope as an attribute to this tag.
Now re-deploy the application and see yourself the difference.
Did you notice the field values after submitting the invalid values? Yes! The values again appear in the field when the error message is displayed. This is particularly important when submitting huge forms. And we have achieved all this without any extra programming.
|