Magazine
 
Struts2 Data Tags

success.jsp

<%@ taglib prefix=”s” uri=”/struts-tags” %>
<html>
<head>
<title>Action Tag Example</title>
</head>
<body>
<h2>Action Tag Example</h2>
<s:action name=”success”>
<b><i>
The action tag will execute the result and
include it in this page.</i></b></div>
</s:action>
</body>
</html>

Output of the success.jsp


2. Bean Tag (Data Tag) Example

The Bean tag is a generic tag that is used to instantiate a class that confirms to the JavaBeans specification. This tag has a body, which can contain a number of Param elements to set any mutator methods on that class. If
the id attribute is set on the BeanTag, it will place the instantiated bean into the stack’s Context.

Add the following code snippet into the struts.xml file.

struts.xml

<action name=”beanTag”
class=”net.javajazzup.beanTag”>
<result name=”success”>/pages/dataTags/
beanTag.jsp</result>
</action>

 

 

Create an action class as shown below:

beanTag.java
package net.javajazzup;
import
com.opensymphony.xwork2.ActionSupport;
public class beanTag extends ActionSupport {
public String execute() throws Exception{
return SUCCESS;
}
}

Create a simple java bean as shown:

companyName.java

package net.javajazzup;
public class companyName {
private String name;
public void setName(String name){
this.name =name ;
}
public String getName(){
return name;
}
}

Now create a jsp page using <s:bean> and <s:param> tags as shown in the beanTag.jsp page. The bean tag instantiates the “net.roseindia.companyName” class, it confirms to the JavaBeans specification. The id attribute
is set on the BeanTag, it places the instantiated bean into the stack’s Context. The body of <s:bean> tag contains a param element <s:param name=”name”>RoseIndia</
s:param> which is used to set the value for the setName() method of the “companyName” class and <s:property value=”%{name}” /> retrieves that value by calling the getName() method.

 

Jan 2008 | Java Jazz Up | 43
 
previous
index
next
 
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 , 74 , 75 , 76 , 77 , 78 , 79 , 80 , 81 , 82 ,

83, 84 , 85 , 86, 87 , 88, 89 , 90 , 91 , 92 , 93 , 94 , 95 , 96 , 97 , 98 , 99 , 100 , 101 , 102 , 103, 104 , 105 ,

106, 107,

Download PDF