Reputation: 891
How to declare a variable and assign value to that variable in Struts2?
Upvotes: 4
Views: 20721
Reputation: 1733
Use the set tag:
<s:set var="myVar">hello</s:set>
read the var with:
<s:property value="#myVar"/>
Another example:
<s:set name="personName" value="person.name"/>
Hello, <s:property value="#personName"/>. How are you?
where person is a bean in your value stack
Upvotes: 12
Reputation: 23587
Well in struts2 we have concept of value stack and during request processing Struts2 framework will push the action on the top of value stack and its properties (Variable) will work as they are on the top of it.
all you need to have getter and setter for your variable and you can than access values both (in/out) in struts2 with OGNL.OGNL is an expression language integrated with Struts2 which is capable of refereeing values from the value stack and also will do the data conversion (except custom type) for you
Upvotes: 0
Reputation: 318568
You shouldn't have to declare variables in JSPs. Do it in your Action and create a getter so you can access it from the JSP showing the action's output.
Upvotes: 0