0% found this document useful (0 votes)
47 views

Struts 2 Tags Code

The document defines a Java class called RegisterAction that handles a user registration form. It initializes fields for the form including colors, hobbies, and products. It also defines getter and setter methods for the form fields. The struts.xml file configures the registration action and results. The register.jsp file defines the registration form using Struts tags to display the fields and retrieve data from the RegisterAction class.

Uploaded by

Julio Cesar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Struts 2 Tags Code

The document defines a Java class called RegisterAction that handles a user registration form. It initializes fields for the form including colors, hobbies, and products. It also defines getter and setter methods for the form fields. The struts.xml file configures the registration action and results. The register.jsp file defines the registration form using Struts tags to display the fields and retrieve data from the RegisterAction class.

Uploaded by

Julio Cesar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Product.

java

public class Product {

Integer productId;
String productName;
Integer productPrice;

public Product(Integer productId, String productName, Integer productPrice) {


super();
this.productId = productId;
this.productName = productName;
this.productPrice = productPrice;
}

public Integer getProductId() {


return productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public Integer getProductPrice() {
return productPrice;
}
public void setProductPrice(Integer productPrice) {
this.productPrice = productPrice;
}
}

RegisterAction.java

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

public class RegisterAction extends ActionSupport {

String firstName;
String lastName;
String gender;
Integer age;
String email;
String address;
String selectedColor;
List<String> colors;
Boolean subscription;
List<String> hobbies;
String selectedHobbies;
List<Product> products;

public String initializeFormFields()


{
initializeColors();
initializeHobbies();
initializeProducts();
return "input";
}

public void initializeColors()


{
colors = new ArrayList<String>();
colors.add("Red");
colors.add("Blue");
colors.add("Green");
colors.add("White");
}

public void initializeHobbies()


{
hobbies = new ArrayList<String>();
hobbies.add("Drawing");
hobbies.add("Teaching");
hobbies.add("Singing");
hobbies.add("Programming");
}

public void initializeProducts()


{
products = new ArrayList<Product>();
products.add(new Product(111,"Mobile Phone",10000));
products.add(new Product(222,"Camera",7000));
products.add(new Product(333,"TV",20000));
products.add(new Product(444,"Laptop",30000));
}

public String execute()


{
System.out.println("execute() method called");
if(subscription == true)
{
System.out.println("You are a subscriber");
}
else
{
System.out.println("You are NOT a subscriber");
}
return "success";
}

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 String getGender() {


return gender;
}

public void setGender(String gender) {


this.gender = gender;
}

public Integer getAge() {


return age;
}

public void setAge(Integer age) {


this.age = age;
}

public String getEmail() {


return email;
}
public void setEmail(String email) {
this.email = email;
}

public String getAddress() {


return address;
}

public void setAddress(String address) {


this.address = address;
}

public String getSelectedColor() {


return selectedColor;
}

public void setSelectedColor(String selectedColor) {


this.selectedColor = selectedColor;
}

public List<String> getColors() {


return colors;
}

public void setColors(List<String> colors) {


this.colors = colors;
}

public Boolean getSubscription() {


return subscription;
}

public void setSubscription(Boolean subscription) {


this.subscription = subscription;
}

public List<String> getHobbies() {


return hobbies;
}

public void setHobbies(List<String> hobbies) {


this.hobbies = hobbies;
}

public String getSelectedHobbies() {


return selectedHobbies;
}
public void setSelectedHobbies(String selectedHobbies) {
this.selectedHobbies = selectedHobbies;
}

public List<Product> getProducts() {


return products;
}

public void setProducts(List<Product> products) {


this.products = products;
}

/*public void validate()


{
if (firstName.equals("")) {
addFieldError("firstName", "First name is required.");
}

if (lastName.equals("")) {
addFieldError("lastName", "Last name is required.");
}

if (gender == null) {
addFieldError("gender", "Gender is required.");
}

if (age == null) {
addFieldError("age", "Age is required.");
}
else if(age <= 18)
{
addFieldError("age", "Age should be above 18.");
}

if (email.equals("")) {
addFieldError("email", "Email is required.");
}
}*/
}

struts.xml

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
<package name="register" extends="struts-default">
<action name="registerAction" class="RegisterAction">
<result name="success">/welcome.jsp</result>
<result name="input">/register.jsp</result>
</action>
<action name="formLoadAction" method="initializeFormFields" class="RegisterAction">
<result name="input">/register.jsp</result>
</action>
</package>
</struts>

register.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"


pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration Form</title>
<s:head/>
</head>
<body>
<h2>Registration Form</h2>

<s:form action="registerAction">
<s:textfield name="firstName" label="First Name" />
<s:textfield name="lastName" label="Last Name" />
<s:radio name="gender" list="{'Male','Female'}" label="Gender" />
<s:textfield name="age" label="Age" />
<s:textfield name="email" label="Email" />
<s:textarea name="address" cols="30" rows="7" label="Address"/>
<s:select list="colors" name="selectedColor" headerKey="None" headerValue="Select a color"
label="Favourite Color" />
<s:checkbox name="subscription" value="true" label="Subscribe to our newsletter"/>
<s:checkboxlist list="hobbies" name="selectedHobbies" label="Hobbies" />
<s:reset value="Reset" />
<s:submit value="Register" />
</s:form>
<table border="1" width="300">
<tr>
<th>Product ID</th>
<th>Product Name</th>
<th>Product Price</th>
</tr>
<s:iterator value="products" var="product">
<tr>
<td>
<s:property value="#product.productId"/>
</td>
<td>
<s:property value="#product.productName"/>
</td>
<td>
<s:property value="#product.productPrice"/>
</td>
</tr>
</s:iterator>
</table>
</body>
</html>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"


pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome</title>
</head>
<body>
<h2>Welcome</h2>
<s:label value="First Name:" />
<s:property value="firstName"/><br/>

<s:label value="Last Name:"/>


<s:property value="lastName"/><br/>

<s:label value="Gender:"/>
<s:property value="gender"/><br/>

<s:label value="Age:"/>
<s:property value="age"/><br/>

<s:label value="Email:"/>
<s:property value="email"/><br/>

<s:label value="Address:"/>
<s:property value="address"/><br/>
<s:label value="Favourite Color:"/>
<s:property value="selectedColor"/><br/>

<s:label value="Hobbies:"/>
<s:property value="selectedHobbies"/>

<s:if test="%{subscription == true}">


<div>You are a subscriber</div>
</s:if>
<s:else>
<div>You are NOT a subscriber</div>
</s:else>
</body>
</html>

You might also like