Struts 2 Registration Form Example
In this example, we are going to create a registration form using struts UI tags and store these information into the oracle database. You may use other database also such as mysql, DB2 etc. according to your requirement.
Let's see the table first that we need to create in the oracle database.
- CREATE TABLE "STRUTSUSER"
- ( "NAME" VARCHAR2(4000),
- "PASSWORD" VARCHAR2(4000),
- "EMAIL" VARCHAR2(4000),
- "GENDER" VARCHAR2(4000),
- "COUNTRY" VARCHAR2(4000)
- )
- /
It will be better for you to create an id for each user. To simply the example, we have not alloted any id with primary key enabled. But you can do it.
The steps to create the registration application in struts2 are as follows:
- Create input page (index.jsp)
- Create the action class (RegisterAction.java)
- Create the class to store data (RegisterDao.java)
- Map the request in (struts.xml) file and define the view components
- Create view components
1) Create input page (index.jsp)
It is the simple jsp page that uses struts 2 UI tags to create a form to get input from the user.
index.jsp
- <%@ taglib uri="/struts-tags" prefix="s" %>
-
- <s:form action="register">
- <s:textfield name="name" label="UserName"></s:textfield>
- <s:password name="password" label="Password"></s:password>
- <s:textfield name="email" label="Email"></s:textfield>
- <s:radio list="{'male','female'}" name="gender"></s:radio>
- <s:select cssStyle="width:155px;"list="{'india','pakistan','other',}"
- name="country" label="Country"></s:select>
-
- <s:submit value="register"></s:submit>
-
- </s:form>
2) Create the action class (RegisterAction.java)
This Action class has five fields and one execute method. As we know, struts framework creates instance of the action class per request, we are passing this object in the save method of RegisterDao class.
RegisterAction.java
- package com.javatpoint;
-
- public class RegisterAction {
- private String name,password,email,gender,country;
-
-
- public String execute(){
- int i=RegisterDao.save(this);
- if(i>0){
- return "success";
- }
- return "error";
- }
- }
3) Create the class to store data (RegisterDao.java)
This class gets information from the object of RegisterAction class and stores these information in the strutsuser table.
RegisterDao.java
- package com.javatpoint;
- import java.sql.*;
- public class RegisterDao {
-
- public static int save(RegisterAction r){
- int status=0;
- try{
- Class.forName("oracle.jdbc.driver.OracleDriver");
- Connection con=DriverManager.getConnection(
- "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
-
- PreparedStatement ps=con.prepareStatement("insert into strutsuser values(?,?,?,?,?)");
- ps.setString(1,r.getName());
- ps.setString(2,r.getPassword());
- ps.setString(3,r.getEmail());
- ps.setString(4,r.getGender());
- ps.setString(5,r.getCountry());
-
- status=ps.executeUpdate();
-
- }catch(Exception e){e.printStackTrace();}
- return status;
- }
- }
4) Map the request in (struts.xml) file and define the view components
This xml file contains information about the package, action class and view components.
struts.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC "-
- Struts Configuration 2.1
- <struts>
-
- <package name="default" extends="struts-default">
-
- <action name="register" class="com.javatpoint.RegisterAction">
- <result name="success">register-success.jsp</result>
- <result name="error">register-error.jsp</result>
- </action>
-
- </package>
- </struts>
5) Create view components
Here, we are creating two view components register-success.jsp and register-error.jsp.
register-success.jsp
- <%@ taglib uri="/struts-tags" prefix="s" %>
- Welcome, <s:property value="name"></s:property>
register-error.jsp
- <%@ taglib uri="/struts-tags" prefix="s" %>
- Sorry, some error occured!
- <s:include value="index.jsp"></s:include>
|