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: Forms & Action Forms

Back | Tutorial Home | Next

Lets now explore the forms. Forms are the HTML elements where the user enters some values and submits the form and gets the results page. We will now create a login page to see how it works in struts.

Struts comes with to tag libraries. You need to import these libraries into a JSP page.

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

This works in Struts 1.3 version but for struts 1.2 and below

The following code has to be inserted in web.xml

<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld
</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld
</taglib-location>
</taglib>

Then import the tag libraries in your code by inserting the following code into JSP page

<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>

HTML tag library (struts-html.tld/ tags-html.tld) are used to create HTML pages and forms.

Sample Form

<html:form action="loginAction">
<html:text property="userName" />
<html:password property="password" />
<html:submit />
<html:cancel />
</html:form>

action property in <html:form> element the and is mapped in the struts-config.xml to repective action. The other elements like <html:text> and <html:password> are mapped to ActionForm Bean. These mappings have to be inserted into struts-config.xml. We will study these mappings as we create the requisite classes

struts-config.xml

<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>


Complete code of LoginForm.jsp

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html>
<head>
<title>Login Form</title>
</head>
<body>
<h1>Login Form</h1>
<html:errors/>
<table>
<html:form action="loginAction">
<tr>
<td>
User ID*
</td>
<td>
<html:text property="userName" />
</td>
</tr>
<tr>
<td>
Password*
</td>
<td>
<html:password property="password" />
</td>
</tr>
<tr>
<td>
<html:submit />
</td>
<td>
<html:cancel />
</td>
</tr>
</html:form>
</table>
</body>
</html>

ActionForm

Action Form is the bean where all the parameters sent from the from are stored. To create an this bean your class should extend from ActionForm

public class LoginForm extends ActionForm {

Create variables, setter and getter methods for form submission parameters

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;
Use reset() method to reset bean values.

	public void reset(ActionMapping mapping,
		HttpServletRequest request) {
		System.out.println("LoginForm::reset()");
		userName=null;
		password=null;
	}

Now that the parameters are received, Next validations have to be done for the request parameters. Struts provides a method validate(). All the requisite validations are done in this method and the errors are logged in an ActionErrors object. If there are no errors then the controller forwards the request to Action class defined in the struts-config. And if error is detected the loginForm.jsp is invoked with error messages displayed in place of <html:errors/> tag in the login form.

In the below code the error handling is done using ActionErrors class

ActionErrors errors = new ActionErrors();

When the error is detected ActionMessage object with error massage is added to ActionErrors object

errors.add("userName", new ActionMessage("login.userName.error"));

In case of Struts1.2 and below ActionError object is used instead of ActionMessage. ActionError class is deprecated in Struts 1.3

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;
	}

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 |