Let us write a simple action and a view that displays the user list on our manageusers.jsp. First of all,create an Action class named ‘ListAction’ and add the execute method as following.
public class ManageUsersAction extends Action {
public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) { //Get a list of users and save to the request try{ List list = UserManager.getInstance().list(); request.setAttribute("list",list); }catch(SQLException sqle) { ActionMessages errors = new ActionMessages();
ActionMessage error = new ActionMessage("error.generic",sqle.getMessage()); errors.add("error",error); saveErrors(request,errors); } return mapping.findForward("success"); } }
Add the following line in ApplicationResources.properties file.
error.generic = An error prevented the operation. Error detail is:<br>{0}
Add the following code in manageusers.jsp
<%@ page language="java" pageEncoding="ISO-8859-1"%> <%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Listing all users</title> </head>
<body> <center><h3>User List</h3></center> <html:errors/>
<logic:present name="list"> <table border="0" cellspacing="0" cellpadding="0" align="center" width="70%" style="border-collapse:collapse;"> <tr bgcolor="#98AFCC"> <th>User ID</th> <th>First Name</th> <th>Last Name</th> <th>Email</th> </tr> <%boolean even = false; %> <logic:iterate id="user" name="list"> <% even = !even; %> <tr bgcolor="<%=even?"#B7D3F5":"#D6E0F5" %>"> <td> <bean:write name="user" property="userId" /> </td> <td> <bean:write name="user" property="firstName" /> </td> <td> <bean:write name="user" property="lastName" /> </td> <td> <bean:write name="user" property="email" /> </td> </tr> </logic:iterate> <tr> <td colspan="6" align="center"> <a href="adduser.jsp">Add New User</a> </td> </tr> </table> </logic:present>
</body> </html>
Note that in this jsp,we have not performed any loop and still we are iterating over the list. Also note the separation of view from the model (data).
Now We need to update the ‘success’ forward in struts-config.xml to forward to manageusers.do instead of manageusers.jsp. Also we need to add the manageusers action entry in the struts-config.xml. Now the <action-mappings> looks like this.
<action-mappings > <action name="loginForm" path="/login" scope="request" type="com.visualbuilder.struts.action.LoginAction" validate="false"> <forward name="failure" path="/index.jsp" /> <forward name="success" path="/manageusers.do" /> </action> <action path="/manageusers" type="com.visualbuilder.struts.action.ManageUsersAction" > <forward name="success" path="/manageusers.jsp" /> </action> </action-mappings>
On redeploying and running the application,you should be able to see a list of users on successful login. You don’t have any data in the database right now,so you might be viewing a page with only one link “Add New User” but if you add few records manually,you will see the list of users.
The variable even is used to indicate if the current row is even or odd to change the row background.
|