Menu
Struts 2 > Struts 2 Property Tag Example

Struts 2 Property Tag Example

Our Artist data class contains the following code.

package vaannila;

public class Artist {

    private String name;
    private String bio;

    Artist(String name, String bio)
    {
        this.name = name;
        this.bio = bio;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getBio() {
        return bio;
    }
    public void setBio(String bio) {
        this.bio = bio;
    }

}

Let's see how we can access the action class attributes using the property tag in the jsp page. The albumDetails.jsp page contains the following code.


<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<s:head />
<style type="text/css">
@import url(style.css);
</style>
<title>Album Details</title>
</head>
<body>
<div class="content">
    <b>Album Title :</b>
    <s:property value="title" /> <br>
    <b>Artist Name :</b>
    <s:property value="artist.name" />
    <br>
    <b>Artist Bio :</b>
    <s:property value="artist.bio" />
	<br>
</div>
</body>
</html>

As you can see title is the property of the AlbumInfoAction so we can access it directly. But name and bio are properties of the Artist class so to access them we need to go one step deeper. We need to use a second-level OGNL expression to access them.

Subscribe
Contact Us  |  Copyright © 2009 vaannila.com