Here, you will see how to access value of array in struts2 using OGNL.
1-index.jsp
<%@taglib uri="/struts-tags" prefix="s"%><html>ADS_TO_REPLACE_1 <head> <title>OGNL_Example</title> </head>ADS_TO_REPLACE_2 <body><h1>OGNL_Example</h1><hr> <a href="ognlAction.action">OGNL_Example</a> </body>ADS_TO_REPLACE_3 </html> |
3_ OGNLAction .java (Action class)
package roseindia.action; import java.util.ArrayList; import java.util.List;ADS_TO_REPLACE_4 import com.opensymphony.xwork2.ActionSupport; ublic class OGNLAction extends ActionSupport{ private List<String> name=new ArrayList<String>();ADS_TO_REPLACE_5 { name.add("Bharat"); name.add("Avnish");ADS_TO_REPLACE_6 name.add("Gyan"); name.add("Ankit"); }ADS_TO_REPLACE_7 public String execute() throws Exception { return SUCCESS; }ADS_TO_REPLACE_8 public List<String> getName() { return name; }ADS_TO_REPLACE_9 public void setName(List<String> name) { this.name = name; }ADS_TO_REPLACE_10 } |
5_ struts.xml
<struts> <constant name="struts.devMode" value="false" /> <package name="roseindia" extends="struts-default">ADS_TO_REPLACE_11 <action name="ognlJsp"> <result>OGNLJsp.jsp</result> </action>ADS_TO_REPLACE_12 <action name="ognlAction" class="roseindia.action.OGNLAction"> <result name="success">display.jsp</result> </action>ADS_TO_REPLACE_13 </package> </struts> |
5_ struts.xml
<%@taglib uri="/struts-tags" prefix="s" %> <html>ADS_TO_REPLACE_14 <head> <title>OGNL_Example</title> </head>ADS_TO_REPLACE_15 <body><h1>OGNL_Example</h1><hr> Size of list : <s:property value="name.size"/><br/> <p> access data of list.</p><br/>ADS_TO_REPLACE_16 name[3] : <s:property value="name[3]"/><br/> name[2] : <s:property value="name[2]"/><br/> name[1] : <s:property value="name[1]"/><br/>ADS_TO_REPLACE_17 name[0] : <s:property value="name[0]"/><br/> Value array list : <s:property value="name"/> </body>ADS_TO_REPLACE_18 </html |
Output
ADS_TO_REPLACE_19
Advertisements
Ads
Ads