Posts

Showing posts from May, 2017

Operators of the OGNL

Operators of the OGNL sum (+) : 2 + 4 , 'hello' + 'world' minus (-) : 5 – 3 multiplication(*) : 9*9 division(/) : 99/3 modulo (%) :  99% 3 increment (++) : ++pre , post++ decrement (--) : post-- , --pre equality (==)  : hello == world greater than (>) :  19 > 10 less than  (<) :  10 < 20

Literal of the OGNL

Literal of the OGNL Literal : Example Char : 'a' String : 'hello' or  "hello"  Boolean : True , False int : 178 double : 199.5 BigDecimal : 777b BigInteger : 777h

Filter and Projection on Collections using OGNL

Filter and Projection on Collections using OGNL Filter Syntax for the filter collectionObjectName.{? expression} Filtering filter the objects from the collection based on the expression provided. For example students.{?#this.percentage>70} Here it will return  new students collection having percentage greater than 70. Filtering always gives less than or equal to the number of elements available in the collection based on the provided expression. Projection Syntax for projection Projecting always gives exactly the same number elements available in the original main collection. students.{name}; Here this projection will gives you a new collection contains only the name of all the students elements available in the original collection. students.{name+''+percentage}; Here this projection will gives you a new collection contains  the name + percentage combination of all the students elements available in the original collection. students.{?#this.percentage...

Create Map dynamically using OGNL

Create Map dynamically using OGNL Java Code Map<Integer,String> myMap =  new HashMap<Integer,String>(); myMap.put(1,"one"); myMap.put(2,"two"); myMap.put(3,"three"); myMap.put(4,"four"); myMap.isEmpty() : myMap.isEmpty OGNL Expression #{1:"one", 2:"two", 3:"three", 4:"four"} Example: 1) <s:iterator value="#{1:'one', 2:'two', 3:'three', 4: 'four'}" status="counter"> <s:property value="#counter.index"></s:property> <s:property value="key"></s:property> <s:property value="value"></s:property> </s:iterator> Here we are creating the map dynamically in the value attribute. In this case # sign is used to creat the map object. 2) <s:iterator value="#request.studentsMap" status="counter"> <s:property value="#cou...

Explore Maps using OGNL

Explore Maps using OGNL Java Code : OGNL Expression myMap.get("id") : myMap.id myMap.get("id") : myMap['id'] myMap.size : myMap.size User user = (User)myMap.get("user") : myMap.user.name user.getName() (configuration of the OGNL type conversion required : see here for more in

Explore List and Array using OGNL

Explore List and Array using OGNL Java Code : OGNL Expression dataList.get(0) : dataList[0] dataArray[0] : dataArray[0] ((Student)dataList.get(0)).getName() : dataList[0].name dataArray.length: dataArray.length dataList.size() : dataList.size dataList.isEmpty() : dataList.isEmpty

i18n and text tags (Miscellaneous Tag)

18 tag i18n tag has  'name'  attribute. It is mandatory. Its type is string. In this name attribute you can specify the name of the resouce bungle from where you want to read the messages for keys. text tag text tag has  'name ' and  'var'  attributes. inside the  'name'  attribute you specify the key name. That key value will be taken from the resource bundle. var  attribute is used to create a reference in a ActionContext and to store the value obtained from the 'name' attribute. Example <s:i18n name="name_of_resource_bundle"> Language: <s:text name="language" /> <s:text name="movie" var="movieName"/> </s:i18n> MovieName: <s:property value="#movieName" /> Here the first <s:text> tag will take the language value from the resource bundle file and directly display it. The second <s:text> will take the value of the movie from the resource bundle file and...

if and else tags (Control Tag)

The if else tag has a one mandatory attribute 'test' test:  Its type is boolean. It acceptes the multiple conmbinations of the conditions which must be evalue to true or false. Example <s:if test="student.marks > 80"> A grade</s:if>  <s:elseif test="student.marks > 70"> B grade</s:elseif>  <s:elseif test="student.marks > 50"> C grade</s:elseif>  <s:elseif test="student.marks > 35"> D grade</s:elseif>  <s:else> Failed </s:else> Here student object will be available into the valuestack. 

include tag (Miscellaneous Tag)

Include tag has one attribute value: It's type is string and It is mandatory parameter. It is used to include jsp or page or action or servlet or any other web resource where the app is deployed. You can dynamically include the value by taking the value from the ValueStack using OGNL expression  i.e%{}.  Here you need to use %{nameOfVar} because the value attribute type is string and it will take the whatever value you provide as a string only. So using %{nameOfVar}  will get the actual value of the variable and will put that value as a string. Also you can include value using the <s:param/> tag. When you write <s:include value="../Hello.jsp"/>  then struts will rewrite ../Hello.jsp as absolute URL based on the current URL of the JSP.

URL tag (Miscellaneous Tag)

URL tag (Miscellaneous Tag) The URL tag has following attributes. value: It contains the base URL. You can provide the complete URL in this value attributes and can use the URL tag inside the href attribute of the anchor tag. var: This attributes is used to store the generated URL in the ActionContext which can be later used at any place in the JSP. action: It is the action name to be targted with the URL to be generate. (Here no need to provide the .action at the end of the provided value) includeParams: This parameter is used to include the get or post parameters into the generated URL. Possible values are "post or get or none or all"If you provide post then it will take all the request parameter in the generated URL. Default is get. If you provide get then it will take request query parameters into the URL being generated. includeConetxt:  This attribute is used to prepend the generated URL w...

Struts2 iterator tag (Control tag)

Usage: Iterator tag is used to iterate over the collection of values. It has two attributes  value:  It is mandatory. It has object type. In this attribute you can specify the object to be loop over. status:  It is not mandatory. It's type is String. If this is specified then it will exponse the IteratorStatus object in the actionContext under the value given to this attribute. Example: <s:iterator value="birds" status="counter"> <li> <s:property value="#counter.count" /> <s:property value="birdName"/> </li> </s:iterator> The iteratorStatus has following method getCount(), getIndex(), isEven(), isFirst(), isLast(), isOdd(), modulus(int operand)

Struts2 action Tag (data tag)

Usage:  This tag allow us to invoke another action from our view layey. It allow us to include the content of the seconds actions view in the first parent actions view layer using the 'executeResult' attribute. It allow us to use the values set by the second action in the frist action after the execution of the second action. Using this tag the you can handle the secondary action can receive, or not receive, the request parameters from the primary request, according to the 'ignoreContextParams' attribute. Attributes: name:  It is required and it is the action name. namespace:  It is not mandatory. Its type is String. It specified the action namespace; defaults to the cur-rent page namespace var:  It is not mandatory. Its type is String. It specifies the reference name of the action bean for use later in the page. executeResult:   It is not mandatory. Its type is Boolean.  When it is set to true, It executes the result of the action (default...

Struts2 bean Tag (data tag)

Usage: To create a new object on the value stack or ActionContext bean tag is used. By default it created the object on the ValueStack but you can also create the object on the ActionContext which you can access using #objectName Attribtues: name: It is not optional. full name of the class including the package hierarchy. var: It is optional. Name of the object which you will use for the reference of the object. When you specify the value for the var attribute then your object will be placed on the ActionContext and it will available after the closing tag also, but when you do not provide the value for the var attribute then the object will placed on the ValueStack. Without the var the scope of the created object will remain uptil the closing of the respective tag where it is used. Here the class of the created object must have the no-argument default constructor.  Example1: <s:bean name="com.data.UserData" > <s:param name="name">hrithik...

Struts2 push Tag (data tag)

Usage: push tag is used to push value or object or property on top of the ValueStack. When you have to do lot of work around single object properties. Then you can simply push that object on the top of ValueStack and use the properties of the object directly using the property name itself. Attribute: value: It is mandatory. Its type if Object. It is used to push object on the ValueStack. Example: <s:push value="userData"> <s:property value="id"/> <s:property value="name"/> <s:property value="address"/> <s:property value="emailAddress"/> <s:property value="mobileNumber"/> </s:push> Here we have pushed the userData object on the top of ValueStack. As it is on the top of ValueStack we can easily access the properties of the userData using OGNL. when we close the push tag then the userData object removed from the value stack.

Struts2 set Tag (data tag)

Usage: set tag allow user to store value at any specific scope or at ActionContext. Its proper use case is, if you need a deep OGNL expression for locating any value in the framework. Then you can store that particular value in your desired scope with a meaningful short name. Attributes: name: It is required. Its type is String. It is a reference name for the variable to be stored into any specified scope. scope: Its type is String. It is not mandatory. It should be any valid scope name. value: Its type is object. It will be expression of the value you want to store. Example: <s:set name="username" value="user.username"/> UserName: <s:property value="#username"/> Here we have used #username in the property tag because by default the username will be stored into the ActionContext at the level of  ValueStack, session map etc <s:set name="username" scope="application" value="user.username"/>...

Struts2 property Tag (data tag)

Usage: property tag provides a convenient and easy way for displaying data into the rendering HTML page. Attributes:  value: Its type is object. It is not mandatory. It is used to display value of any object. default: Its type if String. It is not mandatory. It is used to display any default value if the value is null. escape: Its type is boolean. It is not mandatory. It is used to escape HTML. Syntax: <s:property value="user.name" default="milky_way" escape="true" /> It will display the name property of the user object available on the ValueStack. If the name is null for the user object then it will display milky_way instead of null value. if name contains the html specific value then it will escape the html.

Different OGNL syntax

<s:property value="user.name"/> Here user.name is an OGNL expression which first locates user object on the values stack object in the ActionContext. then it will fetch the name property of the user object. <s:property value="array[0]"/> It will display oth element of array <s:property value="array.length"/> It will display length of array <s:property value="arrayList[0]"/> It will display oth element of arrayList <s:property value="arrayList.size"/> It will display size of arrayList <s:property value="#session['bird.name']"/> Here OGNL will first find the session object into the ActionContext then it will locate the bird object into session map and then it will fetch name property of that bird object. # is used to locate particular object on the ActionContext. without # it wil by default try to find data into ValueStack. <s:property value="${expression}...

What ActionContext contains ?

Action context contains followings. 1) Map of the current request parameters 2) Map of the current request scoped attribtues. 3) Map of the current session scoped attributes. 4) Map of the application scoped attribtues. 5) ValueStack : contains all the applcation domain specific data for the current request. 6) ActionContext returns first occurence of the attribtue from page, request, session and application scope order.

How to make Custom converter in Struts2 ?

To make custom converter for any specific user definied type it need to make a java class which extends the framework's provided class  "org.apache.struts2.util.StrutsTypeConverter" this class has following two abstract methods which need to implement with the required code difinition as per the requirement of conversion. public abstract Object convertFromString(Map context, String[] values, Class toClass); public abstract String convertToString(Map context, Object o ); For any conversion to specific type it need to convert from string to specific type when submitting data using Form to the action class. and from specific user definied type to the String when displaying on the view (JSP). Example public class CircleTypeConverter extends StrutsTypeConverter { public Object convertFromString(Map context, String[] values, Class toClass) { String userString = values[0]; Circle newCircle = parseCircle ( userString ); return newCircle; } public S...

How struts2 store the form elements into Map ? (Prior Java5)

Lets assume that you are submitting following form data <s:textfield name="myUsers['1'].age" label="age"/> <s:textfield name="myUsers['2'].age" label="age"/> <s:textfield name="myUsers['3'].age" label="age"/> and below is the map available on your action class where the form data will be stored. private Map myUsers; public Map getMyUsers() { return myUsers; } public void setMyUsers ( Map myUsers ) { this.myUsers= myUsers; } here myUsers map has Integer value as a Key and User object as a Value for integer key. So to inform the OGNL about this key and value object's type we need to configure this key and value type into the ActionClassName-conversion.properties file. Key_myUsers = java.lang.Integer  /* Here we are telling the framework that the key into myUsers map must be of type Integer*/ Element_myOrderedUsers=com.data.User /* Here we are telling the...

How struts2 store forms elements in List on the Action ? (Prior Java5)

Here we have two choices when we work with list, either we go with the default type for the list elements or we specify the elements type explicitly in the .properties file for the respective action class. The default elements type for the list element is String. here the list works the same way as the Array works but the difference here is, for the name[index] elements you do not need to initialized the list. Means you do not need to initialize the any list on the Action class. But assume here you want the weights list to store Integer values (Remember: before java 5 there was not generics). So for that you can specify the type of the values to be stored into the "List weight;". You can specify that type using below syntax. Make a file named "ActionClassName-conversion.properties" and placed it at the location of the ActionClass file. inside this properties file you have specify "Element-weights=java.lang.Integer" for storing Integer values into w...

How struts2 store forms elements in Array on the Action ? (Prior Java5)

If you have an array of Integer[] weights and String[] names on the actions class then  here is an example to store data on those arrays in action class. For example consider below form with the textfields of 'weights' and names[index] ============= <s:form action="MyArraysAction"> <s:textfield name="weights" label="Weight"/> <s:textfield name="weights" label="Weight"/> <s:textfield name="weights" label="Weight"/> <s:textfield name="names[0]" label="names"/> <s:textfield name="names[1]" label="names"/> <s:textfield name="names[2]" label="names"/> <s:submit/> </s:form> ============= private Integer[] weights ; public Double[] getWeight() { return weights; } public void setWeight(Double[] weights) { this.weights = weights; } private S...