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

