This tag can be used to parameterize other tags.The parameters can be added with or without a name as key.
when we declare the param tag, the value can be defined in either a value attribute or as text between the start and end tag. Struts behaves a bit different according to these two situations.
This tag has the following two paramters.
name (String) - the name of the parameter
value (Object) - the value of the parameter
The following Example will shows how to implement the ParamTag in the Struts2.2.1 --
First we create a JSP file named ParamTag.jsp as follows.ADS_TO_REPLACE_1
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">ADS_TO_REPLACE_2 <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head>ADS_TO_REPLACE_3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Example of Param Tag</title> </head>ADS_TO_REPLACE_4 <body> <s:bean name="roseindia.ParamTag" var="paramTag"> <s:param name="name">Gyan Singh</s:param>ADS_TO_REPLACE_5 <s:param name="age">25</s:param> <s:param name="status">Single</s:param> <s:param name="birthday">1st March</s:param>ADS_TO_REPLACE_6 </s:bean> <ol> <li>Name : <s:property value="#paramTag.name" /></li>ADS_TO_REPLACE_7 <li>Age : <s:property value="#paramTag.age" /></li> <li>Status : <s:property value="#paramTag.status" /></li> <li>Birthday : <s:property value="#paramTag.birthday" /></li>ADS_TO_REPLACE_8 </ol> </body> </html> |
The index.jsp file is as follows- This file only contains the hiperlink only.ADS_TO_REPLACE_9
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01
Transitional//EN" <%@taglib prefix="s" uri="/struts-tags"%> <html> <head>ADS_TO_REPLACE_11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Example of Param Tag</title> </head>ADS_TO_REPLACE_12 <body> <a href="ParamTag.action">Example of param Tag</a> </body>ADS_TO_REPLACE_13 </html> |
The Struts mapping file Struts.xml is as follows-
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLICADS_TO_REPLACE_14 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>ADS_TO_REPLACE_15 <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" />ADS_TO_REPLACE_16 <constant name="struts.custom.i18n.resources" value="ApplicationResources" /> <package name="default" namespace="/" extends="struts-default">ADS_TO_REPLACE_17 <action name="ParamTag" class="roseindia.ParamTag"> <result name="success">/ParamTag.jsp</result> </action>ADS_TO_REPLACE_18 </package> </struts> |
The action class ParamTag.java is as follows.
package roseindia;ADS_TO_REPLACE_19 import com.opensymphony.xwork2.ActionSupport; public class ParamTag extends ActionSupport { private String name;ADS_TO_REPLACE_20 private String age; private String status; private String birthday;ADS_TO_REPLACE_21 public String execute() throws Exception { return SUCCESS; }ADS_TO_REPLACE_22 public String getName() { return name; }ADS_TO_REPLACE_23 public void setName(String name) { this.name = name; }ADS_TO_REPLACE_24 public String getAge() { return age; }ADS_TO_REPLACE_25 public void setAge(String age) { this.age = age; }ADS_TO_REPLACE_26 public String getStatus() { return status; }ADS_TO_REPLACE_27 public void setStatus(String status) { this.status = status; }ADS_TO_REPLACE_28 public String getBirthday() { return birthday; }ADS_TO_REPLACE_29 public void setBirthday(String birthday) { this.birthday = birthday; }ADS_TO_REPLACE_30 } |
This Program produces output on the basis of the Param tag evaluation, This give the output as-
Output:-
ADS_TO_REPLACE_31
Advertisements
Ads
Ads