DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Domain Object as JavaBeans Property Tutorial

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 15, 12 · · Tutorial
Like (1)
Save
Tweet
54.54K Views

Join the DZone community and get the full member experience.

Join For Free

In Struts 2 transferring the data to the domain object is automatically done by the params interceptor.

You just need to create a domain object as a JavaBeans property and the corresponding getter and setter methods.

The framework will automatically initializes the domain object and transfers the form data. The UserAction class contains the following code.

public class UserAction extends ActionSupport{

    private User user;

    public UserAction() {
    }

    public String execute() {
        return SUCCESS;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

To refer the user attributes like name, age etc. we need to first get the user object and then access its properties. For example to access the user's age in the Action you need to use the following syntax.

getUser().getAge();

The User class contains the following attributes and the corresponding getter and setter methods.

public class User {

    private String name;
    private int age;
    private String sex;
    private String[] hobby;
    private String country;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String[] getHobby() {
        return hobby;
    }

    public void setHobby(String[] hobby) {
        this.hobby = hobby;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
}

In the jsp page the user attributes cannot be directly referenced. Since the attributes we refer in the jsp page belongs to the User object we need to go one level deeper to reference the attributes. To refer the user's age, the value of the name attribute should be

name="user.age"

The index.jsp page contains the following code.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>User Details</title>
</head>
<body>
    <s:form action="UserAction" >
        <s:textfield name="user.name" label="User Name" />
        <s:textfield name="user.age" label="Age" />
        <s:radio name="user.sex" label="Sex" list="{'M','F'}" />
        <s:checkboxlist name="user.hobby" label="Hobby" list="{'Music','Art','Dance'}" />
        <s:select name="user.country" label="Country" list="{'Select','India','USA','France', 'Spain'}"  />
        <s:submit />
    </s:form>
</body>
</html>

The result.jsp page contains the follwing code.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>User Details</title>
</head>
<body>
    <h2>User Details</h2>
    <hr>
    User Name :<s:property value="user.name" /><br>
    Age :<s:property value="user.age" /><br>
    Hobbies :<s:property value="user.hobby" /><br>
    Country :<s:property value="user.country" /><br>
</body>
</html>

On executing the example the following page will be displayed to the user.

On entering the user details and clicking the Submit button the following page will be dispalyed.

You can download this example by clicking the download link.

Source :Download
Source + Lib :Download
Object (computer science) Property (programming)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Use Cluster Mesh for Multi-Region Kubernetes Pod Communication
  • An Overview of 3 Java Embedded Databases
  • Why to Implement GitOps into Your Kubernetes CI/CD Pipelines
  • Testing Under the Hood Or Behind the Wheel

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo