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: Message Resources

Back | Tutorial Home | Next

Struts framework provides property files functionality with internationalization (I18N) features. These message resources can be configured in Struts configuration file by adding the below xml.

<message-resources parameter="MessageResources" />

This configuration in config file and the related property file(MessageResources.properties) is already there in blank Struts war file. The message resource is at location <Tomcat-Home>\webapps\salsa-tutorial\WEB-INF\classes\MessageResources.properties

Open MessageResources.properties and add the following lines to define the error messages used in validate() method.

login.userName.error=Invalid user name
login.password.error=Invalid password


Complete listing of LoginForm.java

package com.salsa;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;
import javax.servlet.http.HttpServletRequest;
public class LoginForm extends ActionForm {
	
	private String userName;
	private String password;
	
	public String getuserName() {
		System.out.println("LoginForm::getuserName()");
		return userName;
	}
	public void setuserName(String user) {
		userName = user;
		System.out.println("LoginForm::setuserName()");
	}
	
	public String getPassword() {
		return password;
	}
	public void setPassword(String passWord) {
		password = passWord;
	}
	
	public void reset(ActionMapping mapping,
		HttpServletRequest request) {
		System.out.println("LoginForm::reset()");
		userName=null;
		password=null;
	}
	
	public ActionErrors validate(
			ActionMapping mapping,
			HttpServletRequest request) {
			System.out.println("LoginForm::validate()");
			ActionErrors errors = new ActionErrors();
			if (userName==null || userName.trim().equals("")){
				errors.add("userName", new ActionMessage("login.userName.error"));
			}
			
			if (password==null || password.trim().equals("")){
				errors.add("password", new ActionMessage("login.password.error"));
			}
			
			return errors;
	}
}

Create an alias for this form bean in within <form-beans> tag of struts-config.xml

<form-beans>

<form-bean name="userLoginForm"
type="com.salsa.LoginForm" />

Remember? “userLoginForm” is used in action mapping for parameter name.

<action path="/loginAction"
type="com.salsa.UserLoginAction"
name="userLoginForm"
input="/loginForm.jsp"
cancellable="true">

Finally add logic for cancel button.

UserLoginAction.java

package com.salsa;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionForm;

public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
		System.out.println("UserLoginAction::execute()");
		if (isCancelled(request)){
			return mapping.findForward("logincancel");
		}
		else {
			return mapping.findForward("loginsuccess");
		}	
	}
}

Submit and Cancel Button configurations in the Struts Config file are highlighted below
<action path="/loginAction"
type="com.salsa.UserLoginAction"
name="userLoginForm"
input="/loginForm.jsp"
cancellable="true">
<forward name="loginsuccess" path="/loginStatus.jsp" />
<forward name="logincancel" path="/loginCancel.jsp" />
</action>

If you do not set cancellable="true" in action mapping, InvalidCancelException will be thrown
And finally create the JSP files to display successful logins and cancelled logins. This login is incomplete without validating the user with the database values for login and password. We will complete the database part in our database section.

loginStatus.jsp

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<html>
<head>
<title>
Login Status
</title>
</head>
<body>
<bean:message key="login.status" />
</body>
</html>

Login status uses a Struts tag library struts-bean(tags-bean) to display messages from MessageResources.properties. Add The key ” login.status” in the property file.

login.status=Login Successful.
loginCancel.jsp

<html>
<head>
<title>
Login Status
</title>
</head>
<body>
Login Cancelled
</body>
</html>

To check your login program fire URL http://localhost:8080/salsa-tutorial/loginForm.jsp in your browser.

Look at the SOP’s(Sys Outs) in Tomcat console to understand the work flow. Alternatively you can use Log4J for better understanding of the work flow.

  • userLogin.jsp
  • LoginForm::reset() - ActionForm
  • LoginForm::setuserName() - ActionForm
  • LoginForm::setPassword() - ActionForm
  • LoginForm::validate() - ActionForm
  • UserLoginAction::execute() - Action
  • loginStatus.jsp

Now let us see the workflow diagram to clearly understand the flow

Back | Tutorial Home | Next

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 |