Lohit
Lohit

Reputation: 891

Variable declaration in Struts2

How to declare a variable and assign value to that variable in Struts2?

Upvotes: 4

Views: 20721

Answers (3)

robsf
robsf

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

Umesh Awasthi
Umesh Awasthi

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

ThiefMaster
ThiefMaster

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

Related Questions