The "forward" element in struts-config.xml describes an ActionForward that is to be made available to an Action as a return value. An ActionForward is referenced by a logical name and encapsulates a URI. A "forward" element may be used to describe both global and local ActionForwards. Global forwards are available to all the Action objects in the module. Local forwards can be nested within an <action> element and only available to an Action object when it is invoked through that ActionMapping. Let us add two forwards for LoginAction “success” and “fail”. After adding the two forwards,the <action> tag looks like this:
<action path="/login" type="com.visualbuilder.struts.action.LoginAction" validate="false"> <forward name="success" path="/manageusers.jsp" /> <forward name="failure" path="/index.jsp" /> </action>
Note the two forwards success and the failure. The path attribute in each forward tag is the resource to which the request will be redirected when forwarded to a particular forward. Let us now program our action to return to “success” if the entered user id and password is admin/admin. Right now we don’t have any action form associated with this action,let’s get the parameters from request as we do in servlets.
String user = request.getParameter("userId"); String password = request.getParameter("password"); if(user!=null && password!=null && user.equals("admin") && password.equals("admin")) return mapping.findForward("success"); return mapping.findForward("failure");
Now create index.jsp so that we have a form to submit values to this form. Add the following form in the body section of the jsp.
<form action="/login.do" method="post"> <table border="0"> <tr> <td>Login:</td> <td><input type="text" name="userId" /></td> </tr> <tr> <td>Password:</td> <td><input type="text" name="password" /></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="Login"/></td> </tr> </table> </form>
We are almost ready to run the project. But let’s create a file manageusers.jsp so that if we enter a valid user id / password,we can see the success page. Add a simple success message “You are logged into this page”.
|