HOME | J2ME | Struts | AJAX | SOAP | SOA MEDIA STREAMING AXIS |
Struts for Begginers
Introduction
MVC Design Pattern
Installation
Action Servlets
Hello World
Forms & ActionForms
Messages Resources
Data Bases
Exceptions
Acess ActionForms from JSP
Logic Tag

 

 

Struts for Beginners: Logic Tag Library

Back | Tutorial Home

Let us assume that we have to display a number of fields in a table view from the database. This will need iteration through the result set which should be done using java code in your JSP pages or use of JSTL tags. Struts gives you a ready made tag library called “logic” for this kind of display logic.

Let’s modify our login program to learn about logic tag library. We will modify this proram to list all the users in the database. Add more users to the database though your SQL client.

Create a new bean “UsersVO”, also called the value object by some programmers which will be a place holder for the names retrieved from the database.

package com.salsa;

import java.io.Serializable;
public class UsersVO implements Serializable{
	
	private String userName;
		
	public String getuserName() {
		return userName;
	}
	public void setuserName(String user) {
		userName = user;
	}	
}

Add the code to get user names from the data base upon successful login..
Statement statement=conn.createStatement();
rs = statement.executeQuery("SELECT USER_NAME FROM CMS_USER");

Add these resultset values an ArrayList

List userList = new ArrayList(20);
while(rs.next()) {
	String userName = rs.getString(1);
	System.out.println("user:"+userName);
	UsersVO users = new UsersVO();
	users.setuserName(userName);
	userList.add(users);
}
Set the ArrayList as request attribute so that it can be accessed from the JSP page.

request.setAttribute("users", userList);

Complete Code listing of the Action class

public class UserLoginAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
		System.out.println("UserLoginAction::execute()");
		Connection conn=null;
		ResultSet rs = null;
		boolean loginFlag = false;
		if (isCancelled(request)){
			return mapping.findForward("logincancel");
		}
		else {
			try{
				Context context = new InitialContext();
				Context envContext  = (Context)context.lookup("java:/comp/env");
				DataSource dataSource =(DataSource)envContext.lookup("jdbc/docmanager");
				conn = dataSource.getConnection();
				LoginForm loginForm = (LoginForm) form;
				PreparedStatement statement = conn.prepareStatement(
				"SELECT USER_NAME, PASSWORD FROM CMS_USER WHERE USER_NAME=? AND PASSWORD=?");
				statement.setString(1,loginForm.getuserName());
				statement.setString(2,loginForm.getPassword());
				rs = statement.executeQuery();
				
				while(rs.next()) {
					loginFlag=true;
				}
			}
			catch(Exception e) {
				System.out.println("db message: " + e.getMessage());
			}
			if (loginFlag){
				try{
					Statement statement=conn.createStatement();
					rs = statement.executeQuery("SELECT USER_NAME FROM CMS_USER");
					List userList = new ArrayList(20);
					while(rs.next()) {
						String userName = rs.getString(1);
						System.out.println("user:"+userName);
						UsersVO users = new UsersVO();
						users.setuserName(userName);
						userList.add(users);
					}
					request.setAttribute("users", userList);
				}
				catch(Exception e) {
					System.out.println("db message: " + e.getMessage());
				}
				finally{
				conn.close();
				}
				return mapping.findForward("loginsuccess");
			}	
			else {
				conn.close();
				return mapping.findForward("loginfailure");
				
			}	
			
		}	
	}
}

Modify loginStatus.jsp to access the ArrayList of user names.

Import the logic tag library

<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>

Check if the ArrayList “users” is in scope using <logic:present> tag

Iterate through the ArrayList of beans using <logic:iterate> tag

Display the values in the beans using <bean:write>

The partial listing of above three points
<logic:present name="users">
<logic:iterate id="username" name="users">
<bean:write name="username" property="userName"/>
</logic:iterate>
</logic:present>

Complete listing of loginStatus.jsp

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<html>
<head>
<title>
Login Status
</title>
</head>
<body>
<bean:message key="login.status" />
<br>You are logged in as <b><bean:write name="loginBean" property="userName" /></b>
<logic:present name="users">
<table border="1">
<tr><th>User Names</th></tr>
<logic:iterate id="username" name="users">
<tr><td><bean:write name="username" property="userName"/></td></tr>
</logic:iterate>
</table>
</logic:present>
</body>
</html>

Back | Tutorial Home

site comments powered by Disqus
Download our free toolbar

toolbar powered by Conduit

| Copyright © 2009. All rights reserved | Terms and Conditions | About | Contact | Feed Back |