Struts Java Framework3 Registeration Form

Create:RegForm

then add Jars struts1.3.10 then create one input file index.jsp



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

<h1>Reg Form</h1>

<html:form  action="reg" method="post">
<table>

<tr>
<td>ID:</td>
<td><html:text property="id"/></td>
<td><html:errors property="id_e"/></td></tr>

<tr>
<td>Name:</td>
<td><html:text property="name"/></td>
<td><html:errors property="name_e"/></td></tr>

<tr>
<td>EMAIL:</td>
<td><html:text property="email"/></td>
<td><html:errors property="email_e"/></td></tr>

<tr>
<td>ADDRESS:</td>
<td><html:textarea property="address"/></td>
<td><html:errors property="address_e"/></td></tr>

<tr>
<td>GENDER:MALE</td>
<td><html:radio property="gender" value="MALE"/></td>
<td><html:errors property="gender_e"/></td></tr>

<tr>
<td>GENDER:FEMALE</td>
<td><html:radio property="gender" value="FEMALE"/></td>
<td></td></tr>


<tr><td>HOBBIES</td>
<td><html:checkbox property="HOBBIES" value="HOBBIE1"/>DANCING</td>
<td><html:errors property="HOBBIES_e"/></td></tr>

<tr><td>HOBBIES</td>
<td><html:checkbox property="HOBBIES" value="HOBBIE2"/>Swimming</td>
<td></td></tr>

<tr>
<td></td>
<td html:submit value="Register"></td>
<td></td></tr>
</table>


Now Create a Package "FormBeans"  then class Employee extends ActionForm

Employee.java


package formbeans;
import org.apache.struts.action.ActionForm;
public class Employee extends ActionForm

{
private int id;
private string name,email,address,gender;
private String[] hobbies


}

Right Click on Form and Select Source then Click on Generate Getters and Setters

@Override
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request){


ActionErrors ae=new ActionErrors();
if(id==0)

 ae.add("id_e" new ActionMessage("msg1"))
if(name.equals(""))
ae.add("name" new ActionMessage("msg2"))
if(email.equals(""))
ae.add("email" new ActionMessage("msg3"))
if(address.equals(""))
ae.add("address" new ActionMessage("msg4"))
if(gender.equals(""))
ae.add("gender" new ActionMessage("msg5"))
if(hobbies.length<1)
ae.add("hobbies" new ActionMessage("msg6"))
return ae;

}

Create Properties File "Messages.properties"


msg1=<font color='red'> id should not zero</font>

msg2=<font color='red'> name should not zero</font>

msg3=<font color='red'> email should not zero</font>

msg4=<font color='red'> address should not zero</font>

msg5=<font color='red'> gender should not zero</font>

msg6=<font color='red'> hobbies should not zero</font>


11.Part8



struts-config.xml


<struts-config>
<form-beans>
<form-bean name="EMP" type="formbeans.Employee"/>
</form-beans>
<action-mappings>
<action path="/reg" name="EMP"  validate="true" input="">
</action-mappings>

<message-resources parameter="resources/Messages"/>
</struts-config>


File Web.xml 

<display-name>RegForm</display-name>
<welcome-file-list>
<welcome-file>
</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/struts-config.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>