1. Normal Method with simple getters() and setters()
2. Object Backed Method
3. Model Driven Method
We will see the third method now.
Another way of transfering the form data directly inside the object is Model Driven approach. This approach is the best among all the three because for this approach no need to specify the field names deeply. We can define the fields as usual.
Create a simple jsp file index.jsp with struts2 form tags as below. Note that we never named the fields deeply like what we did earlier for object backed approach.
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Struts Data Transfer - Normal Transfer Method</title> </head> <body> <h3>Struts Data Transfer - Normal Transfer Method</h3> <s:form action="register"> <s:textfield name="fname" label="First name" /> <s:textfield name="lname" label="Last name" /> <s:textfield name="email" label="Email" /> <s:textfield name="age" label="Age" /> <s:submit /> </s:form> </body> </html>
So As per the jsp page when the form is getting submitted it will look for an action with name register. See out struts.xml below.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <constant name="struts.custom.i18n.resources" value="ApplicationResources" /> <package name="default" extends="struts-default" namespace="/"> <action name="register" class="com.rajesh.dataTransfer.Register" method="register"> <result name="success">success.jsp</result> </action> </package> </struts>
In our Struts.xml we pointed the register action to register() method in the Register class. So when the form is getting submitted the control will go the register() method. Now see the Register action class below.
package com.rajesh.dataTransfer; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; @SuppressWarnings("rawtypes") public class Register extends ActionSupport implements ModelDriven { private static final long serialVersionUID = -367986889632883043L; private User user = new User(); public User getModel() { return user; } public String register() { System.out.println("Inside Action Method"); System.out.println(user.getFname()); System.out.println(user.getLname()); System.out.println(user.getAge()); System.out.println(user.getEmail()); return SUCCESS; } }
In our register action class we made two changes from Object backed method. One is the class implements ModelDriven interface and it’s method getModel(). So while the control look for setter methods it will run the getModel() method. Then it will take care of the data movement from form to object. Finally it will simply return the user object.
How this is achieved is by the framework interface ModelDriven. One important thing here is we have to create the User Object explicitly outside the method. And as usual the User class should have all the setters() and getters() for the required variables. See our User class below.
package com.rajesh.dataTransfer; public class User { private String fname; private String lname; private int age; private String email; public String getFname() { return fname; } public void setFname(String fname) { this.fname = fname; } public String getLname() { return lname; } public void setLname(String lname) { this.lname = lname; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
So the Struts2 framework will execute the getModel() method before executing the actual action method, in our case register(). So while executing the getModel() method it will look for all the submitted data and if it matches with the method return type then it will call the corresponding setter methods and return as object. Thus all the variables will be assigned to the user object and then returned.
In our action method we just displayed the values in the console, then returning success message as result name. So as we mentioned the result page is success.jsp for string “SUCCESS” struts2 will load the page success.jsp to the user. See our success.jsp below.
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Struts Data Transfer - Normal Transfer Method</title> </head> <body> <h3>Struts Data Transfer - Normal Transfer Method</h3> <h4>User Data saved successfully</h4> First Name:<s:property value="fname" /> <br>Last Name:<s:property value="lname" /> <br>Email:<s:property value="email" /> <br>Age:<s:property value="age" /> <br> </body> </html>
What will happen for those tags like <s:property value =”fname”/> . Struts2 will look for getter() methods in the corresponding action class. As we discussed earlier due to ModelDriven interface implementation both the getter and setter process will be take care by the framework.
So it will run the getModel method and runs the getter methods inside the User object then it will return the object with all values. Then all the fields will be filled then returned to the user as a rendered page.
For the above method I have created a complete .war file with source code. Download and just place under Apache web apps folder and run.
If you have any issues while execution please post in comments or send mail to rajeshmepco@gmail.com.
]]>1. Normal Method with simple getters() and setters()
2. Object Backed Method
3. Model Driven Method
We will see the second method now.
Create a simple jsp file index.jsp with struts2 form tags as below. The only difference from normal method and Object backed method is while naming the fields the name should be defined deeply. See below.
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Struts Data Transfer - Normal Transfer Method</title> </head> <body> <h3>Struts Data Transfer - Normal Transfer Method</h3> <s:form action="register"> <s:textfield name="user.fname" label="First name" /> <s:textfield name="user.lname" label="Last name" /> <s:textfield name="user.email" label="Email" /> <s:textfield name="user.age" label="Age" /> <s:submit /> </s:form> </body> </html>
The meaning of the above code is all the values will go inside the user object. So As per the above page when the form is getting submitted it will look for an action with name register.
Now See our struts.xml below.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <constant name="struts.custom.i18n.resources" value="ApplicationResources" /> <package name="default" extends="struts-default" namespace="/"> <action name="register" class="com.rajesh.dataTransfer.Register" method="register"> <result name="success">success.jsp</result> </action> </package> </struts>
In our Struts.xml we pointed the register action to register() method in the Register class. So when the form is getting submitted the control will execute the register() method. As per struts2 design the control will search the methods for all available data fields. In our case we named all the fields deeply like user.fname,user.lname.
So the control will look for method setUser() in the action class that takes an object as parameter which have variables matching to the data fields. (i.e) An object contains lname, fname, age and email as members will be the parameter for setUser() method.
Also the parameter object class should contains setters() and getters() method for required variables. So while executing the setUser() method the control will call the setters inside the User object for corresponding data fields.
Now see our Register class and User Class below.
package com.rajesh.dataTransfer; import com.opensymphony.xwork2.ActionSupport; public class Register extends ActionSupport { private static final long serialVersionUID = -367986889632883043L; private User user; public void setUser(User user) { this.user = user; } public User getUser() { return user; } public String register() { System.out.println("Inside Action Method"); System.out.println(user.getFname()); System.out.println(user.getLname()); System.out.println(user.getAge()); System.out.println(user.getEmail()); return SUCCESS; } }
package com.rajesh.dataTransfer; public class User { private String fname; private String lname; private int age; private String email; public String getFname() { return fname; } public void setFname(String fname) { this.fname = fname; } public String getLname() { return lname; } public void setLname(String lname) { this.lname = lname; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
As we know the Struts2 framework will execute the setUser() method before executing the actual action method register(). So while executing the setUser() method it will call the setter methods inside the User Object. Here user.setLname(), user.setFname(), user.setAge() and user.setEmail(). So all the variables will be assigned to the user object.
In our action method we just displayed the values in the console, then returning success message as result name.
So as we mentioned the result page is success.jsp for string “SUCCESS” struts2 will load the page success.jsp to the user. See our success.jsp below.
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Struts Data Transfer - Normal Transfer Method</title> </head> <body> <h3>Struts Data Transfer - Normal Transfer Method</h3> <h4>User Data saved successfully</h4> First Name:<s:property value="user.fname" /> <br>Last Name:<s:property value="user.lname" /> <br>Email:<s:property value="user.email" /> <br>Age:<s:property value="user.age" /> <br> </body> </html>
What will happen for those tags like <s:property value =”user.fname”/> . Struts2 will look for get methods in the corresponding action class. As we discussed earlier due to the name of the field it will look for method getUser() ninside the action class. As we have getUser() method inside the class it will return the object with all values. Then all the fields will be filled then returned to the user.
For the above method I have created a complete .war file with source code. Download and just place under Apache web apps folder and run.
If you have any issues while execution please post in comments or send mail to rajeshmepco@gmail.com.
]]>1. Normal Method with simple getters() and setters()
2. Object Backed Method
3. Model Driven Method
We will see the first method now.
Create a simple jsp file index.jsp with form (struts2 tags) as below
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Struts Data Transfer - Normal Transfer Method</title> </head> <body> <h3>Struts Data Transfer - Normal Transfer Method</h3> <s:form action="register"> <s:textfield name="fname" label="First name" /> <s:textfield name="lname" label="Last name" /> <s:textfield name="email" label="Email" /> <s:textfield name="age" label="Age" /> <s:submit /> </s:form> </body> </html>
So As per the jsp page when the form is getting submitted it will look for an action with name register. See out struts.xml below.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <constant name="struts.custom.i18n.resources" value="ApplicationResources" /> <package name="default" extends="struts-default" namespace="/"> <action name="register" class="com.rajesh.dataTransfer.Register" method="register"> <result name="success">success.jsp</result> </action> </package> </struts>
In our Struts.xml we pointed the register action to register() method in the Register class. So when the form is getting submitted the control will go the register() method. As per struts2 design the control will look methods for all available values. In out case for fname,lname,email and age. It will search for methods setFname(),setLname(),setEmail() and setAge() respectively. Now see our Register class in below.
package com.rajesh.dataTransfer; import com.opensymphony.xwork2.ActionSupport; public class Register extends ActionSupport { private static final long serialVersionUID = -367986889632883043L; private String fname; private String lname; private int age; private String email; private User user;; public String getFname() { return fname; } public void setFname(String fname) { this.fname = fname; } public String getLname() { return lname; } public void setLname(String lname) { this.lname = lname; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String register() { System.out.println("Inside Action Method"); user = new User(); user.setAge(getAge()); user.setEmail(getEmail()); user.setLname(getLname()); user.setFname(getFname()); System.out.println(user.getFname()); System.out.println(user.getLname()); System.out.println(user.getAge()); System.out.println(user.getEmail()); return SUCCESS; } }
As we have all the required methods, the Struts2 framework will execute all those methods before executing the actual action method, in our case register(). So after executing all the methods the control will go to register method. So all the variables are available now for operation.
In our action method we just displayed the values in the console, then returning success message as result name.
So as we mentioned the result page is success.jsp for string “SUCCESS” struts2 will load the page success.jsp to the user. See our success.jsp below.
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Struts Data Transfer - Normal Transfer Method</title> </head> <body> <h3>Struts Data Transfer - Normal Transfer Method</h3> <h4>User Data saved successfully</h4> First Name:<s:property value="fname" /> <br>Last Name:<s:property value="lname" /> <br>Email:<s:property value="email" /> <br>Age:<s:property value="age" /> <br> </body> </html>
What will happen for those tags like <s:property value =”fname”/> . Struts2 will look for get methods in the corresponding action class. As we have all get methods getEmail(), getLname(), getFname() and getAge() in the action class those will get executed and the value will be placed there.
For the above method I have created a complete .war file with source code. Download and just place under Apache web apps folder and run.
If you have any issues while execution please post in comments or send mail to rajeshmepco@gmail.com.
]]>