JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

Struts 2 Property Tag Example

December 14, 2013 by Krishna Srinivasan Leave a Comment

Struts 2 Property Tag is most commonly used by the struts applications. It is simply taking the field or property values from the bean instances or from the context variables. This tag can be used either as sub element of another tag or outside any other tags. This tutorial shows how to use the s:property tag with example.

1. Action Class and Bean

Struts2HelloWorldAction.java

[code lang=”java”]
package javabeat.net.struts2;

public class Struts2HelloWorldAction {
private String userName = "Hello World Property Tag Example!!";

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}
public String execute(){
return "success";
}
}
[/code]

UserDetails.java

[code lang=”java”]
package javabeat.net.struts2;

public class UserDetails {
private String name;
private String city;
public UserDetails(){}
public UserDetails(String… args){
this.name = args[0];
this.city = args[1];
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}

}
[/code]

2. Property Tag Example

[code lang=”xml”]
<%@ 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>Struts 2 Property Tag Example </title>
</head>
<body>
<s:property value="userName" />
<br>
<s:bean name="javabeat.net.struts2.UserDetails" var="users">
<s:param name="name">Hello World</s:param>
<s:param name="city">London</s:param>
</s:bean>

<s:property value="#users.name" /> ,
<s:property value="#users.city" />

</body>
</html>
[/code]

3. Struts.xml

[code lang=”xml”]
<?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.devMode" value="true" />
<package name="tags" extends="struts-default">
<action name="propertytag" class="javabeat.net.struts2.Struts2HelloWorldAction"
method="execute">
<result name="success">/PropertyTag.jsp</result>
</action>
</package>
</struts>
[/code]

4. Property Tag Demo

If you access the application using the URL, you would see the output as below screen.

Struts 2 Property Tag Example Screen

Filed Under: Struts Tagged With: Struts 2 Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved