1) Create a Register.jsp page as shown in the code below,
2) Create the Register.java action class that contains the JavaBean object into the properties of which the incoming request data is to be transferred.
3) Create the PersonBean.java class that is the JavaBean class in question,
4) Create the RegisterDone.jsp page that displays the incoming request data,
5) Configure the struts2 actions in the struts.xml file,
6) Test the code by typing the following URL in the browser,
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Register Page</title>
</head>
<body>
<h4>Enter Registration Information!</h4>
<s:form action="doRegister">
<s:textfield name="person.firstname" label="First Name"/>
<s:textfield name="person.lastname" label="Last Name"/>
<s:textfield name="person.age" label="Age"/>
<s:submit/>
</s:form>
</body>
</html>
2) Create the Register.java action class that contains the JavaBean object into the properties of which the incoming request data is to be transferred.
package info.icontraining.struts2;
import com.opensymphony.xwork2.*;
public class Register extends ActionSupport {
public String execute() {
return Action.SUCCESS;
}
private PersonBean person;
public PersonBean getPerson() {
return person;
}
public void setPerson(PersonBean person) {
this.person = person;
}
public void validate() {
if ( person.getFirstname().length() == 0 ){
addFieldError( "person.firstname", "First name is required." );
}
if ( person.getLastname().length() == 0 ){
addFieldError( "person.lastname", "Last name is required." );
}
if ( person.getAge() < 18 ){
addFieldError( "person.age", "Age required & must be > 18" );
}
}
}
3) Create the PersonBean.java class that is the JavaBean class in question,
package info.icontraining.struts2;
public class PersonBean {
private String firstname;
private String lastname;
private int age;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
4) Create the RegisterDone.jsp page that displays the incoming request data,
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Register Done</title>
</head>
<body>
<h1>Thank You</h1>
<h3>for Registering!</h3>
<s:property value="person.firstname"/>
<s:property value="person.lastname"/>
<s:property value="person.age"/>
</body>
</html>
5) Configure the struts2 actions in the struts.xml file,
<action name="Register">
<result>/Register.jsp</result>
</action>
<action name="doRegister" class="info.icontraining.struts2.Register">
<result>/RegisterDone.jsp</result>
<result name="input">/Register.jsp</result>
</action>
http://localhost:8080/WebAppName/Register.action
No comments:
Post a Comment