
Using Form Beans
• When using JSP it is common to create a form and associate that form with a single JavaBean.
- The JSP user can type very few lines of code and access a bean.
- This pattern of one bean to one form is commonly called a FormBean.
- Struts support form beans.
• The struts version of the Form-Bean can be easier for a user to use than the JSP version.
- The usual JSP version of this would look something like the below.
<input type=”text” name=”username” value=”<%=bean.getUsername()%>” >
- This can be replaced in Struts with the easier to work with code that follows.
<html:text property=”username”/>
- In addition to being easier to type, this is of value to non-Java coders since it removes the need to
provide the name of the bean being used.
- Of coarse to get at this kind of JSP ease you will need to spend some time configuring your application.
• Beans require configuration of your struts-config.xml file.
- You will need to list the Bean in the xml.
- You will need to change the action to refer to the bean.
• Starting with the bean configuration: add a <form-bean> to the struts-config.xml.
- Whether you are using a GUI or a text editor a <form-beans> tag should be provided in the
configuration for you to add <form-bean> tags.
- The <form-bean> is a simple listing of a name and a type that name is associated with.
<form-beans>
<form-bean name="user" type="com.intertech.User">
</form-bean>
</form-beans>
• After adding the form bean you will need to create an ActionForm.
- The API and the configuration file don’t 100% agree on what to call this “form bean”, the config calls
the <form-bean> the API makes them all extensions of “ActionForm”.
- The form bean is similar to a JavaBean, but it will be a descendant of the org.apache.struts.action.
ActionForm class
package com.intertech;
import org.apache.struts.action.ActionForm;
public class User extends ActionForm{
private String userName;
private String password;
public String getPassword() {
return password;
}
public String getUserName() {
return userName;
}
public void setPassword(String password) {
this.password = password;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
• To use the form bean in a JSP you will need to link the form bean to the action in the struts-config file.
- This would be most likely done when you add the <form-bean>, we use this order hoping to make it
easier to understand and remember.
- You need to add an attribute and scope to your action mapping.
- The attribute needs to match the name of the <form-bean> name attribute. This means the attribute=”
user” points back to the name=”user” in the <form-bean> tag.
<action path="/viewHelloWorld"
type="com.intertech.actions.ViewHelloWorld"
name="user"
scope="session"
attribute="user"
validate="false">
<forward name="success" path="/helloWorld.jsp">
</forward>
</action>
• With this configuration correct you can now refer to a bean in the JSP using one of a number of bean
and html tags.
- You can access a property of the bean using the <bean:write> tag.
<bean:write name="user" property="userName" scope="session" filter="true"/>
- You can create a text input that is tied to a field in a bean using an <html:text> tag.
<html:text property="userName" size="16" maxlength="16"/>
• Using the <html:text> tag struts takes care of getting and setting values out of the bean.
- Similar to the <jsp:setProperty> action, the value entered in the text field is used to populate the bean
when the form is submitted.
- The html tags include tags to work with other input types as well.
- <html:text>: a text field
- <html:checkbox>: a checkbox
- <html:submit>: a submit button
- <html:clear>: a clear button
- <html:textarea>: a textarea
- <html:hidden>: a hidden field
• Looking at a simple example will help to understand how it all links together.
• Using the Form-Bean com.intertech.User we have previously created you can create a login form for
this like the following JSP.
- When the submit button is pressed the username and password fields will arrive in your action as a
User.
- This removes the need to read much of the request parameters.
- The hard coded text like “Username:” and “Submit” were left in this code snippet to make it easier to
read.
<html:form action="/logon" >
Username:<html:text property="userName" size="16" maxlength="16"/>
<br>
Password:<html:password property="password" size="16" maxlength="16"/>
<br>
<html:submit>Submit</html:submit>
<html:reset>Reset</html:reset>
</html:form>
• These tags make working with beans on both the view side JSP and the model side action easier to use.
- The interpretation from string has been done.
• The execute method is passes an ActionForm. This ActionForm is your bean.
- Recall we extended the ActionForm when creating the bean.
- Looking at an example action we can see how the bean arrives as a parameter.
- The ActionForm passed into the execute() method is the bean declared created and populated in the JSP.
- A portion of this code was removed make it easier to read.
- This Action could only run if the proper configuration of forwards is done.
public class VerifyLogon extends Action{
private int numTries;
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
//Get the javabean out of the action mapping to work with
User user = (User)form;
if (/*???*/){ //Success condition
return (mapping.findForward("success"));
}else if(numTries < 3){
//Non-Teminal Failure Condition
numTries++;
return (mapping.findForward("failure"));
}else{
//Terminal Failure Condition
return (mapping.findForward("kickout"));
}
}
}
Form Beans
Table of Contents
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials