Variable declaration in Struts2 - java

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

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

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.

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

Related

How to show the hidden field in struts2

Fetching some variables in hidden field using struts2
<s:hidden name="ABCFormBean.dependentLists[0].lastNameEng" id="h_dependantLastNameEng_id"/>
I need to display the same in the same page
<s:property value="ABCFormBean.dependentLists[0].lastNameEng"/>
doesnt work
how to display the content?
Nothing gets displayed because the property value doesn't map the value in the value stack. Bu default the action is on top of the value stack. If you map properties they should should be initialized and have getters and setters. To display
<s:property value="ABCFormBean.depLists[0].firstNameEng"/>
the following method calls are expected
getABCFormBean().getDepLists().get(0).getFirstNameEng()
If you can get this value in the action method, then you would have the values been displayed by the property tag.

How to pass Struts2 property tag as parameter in java function in the jsp?

I am having a problem in passing Struts2 property tag as parameter in java function in the jsp.
I am calling a java function like this from jsp,
<s:if test="%{DoSomething()}">
DoSomething() function is in the Action class, and is being called for each record in the iterator.
The problem is that I want to pass this property <s:property value="userId"/> in the function DoSomething as java string as follow.
<s:if test="%{DoSomething("<s:property value="userId">/)}">
Can anyone please guide me that how can I do that. I goggled it but did not find anything.
<s:if test="%{DoSomething(userId)}">
Most likely you wouldn't need to pass in a parameter like this. You are trying to call a method DoSomething from your action class. You want to pass in a parameter to this method in your action class. The userId must be existing already in you action class otherwise you couldn't be able to get it through the s:property tag. You can just use this userId variable in your DoSomething method of your action class.
you cannot used Struts2 tag to another Struts2 tag, you can use expression language,
first use <s:set name="userId" value="userId" /> then use ${userId}

How to access Struts 2 action class properties directly

I want to know if we populate action class properties, can we access them directly in the result JSP without assigning them to a form in the action class?
I think you need to have the bean getter method defined properly . Say , if a property is named id and you have a method called getId() in your action. Then it can be accessed directly in the JSP as :
OGNL expression:
<s:property value="id"/>
OR
JSTL expression:
<c:out value="${action.id}"/>
one thing u can do is that initialize the properties in the ModelDriven Class which u want to be intialized whenever your action is called.
http://struts.apache.org/release/2.1.x/docs/model-driven.html
Struts2/XWork1 and its taglib is oriented towards OGNL, which is using
a valueStack for all action properties. These values are not direct
available for the expression language of JSP2.0/JSTL1.1.
To access it via OGNL you can use OGNL only in Struts tag attributes. For example
public String MyAction extends ActionSupport {
private String myAttr;
//getter and setter here
...
<s:set var="myAttrz" value="myAttr" scope="request"/>
${myAttrz} or ${myAttr} will just print out the value.
<%=request.getAttribute("myAttrz")%>
In the first tag the action attribute is accessed as OGNL expression
In the second line as JSTL expression.
In the third line scriptlets.
References:
Using Struts and XWork with JSP 2.0 and JSTL 1.1
Application, Session, Request objects in JSP
Can we use JSTL with the framework

traversing arraylist using EL using foreach

I have a class language having features ID and Name.
I have another class Language_List that has ArrayList<language> lang as a member variable.
In my JSP page I want to access the Name variable of ArrayList<language> lang using EL and For each Loop.
<c:forEach var="language" items="${languages.lang}">
${language}<br>
</c:forEach>
However, it doesn't show ant result and intellisense doesn't work too. Anyone who can help me with this
PS: languages is a Bean variable contain list of languages from DB
I tried this and got this
<b>${languages.lang}</b>
HTML
[sakila.language#f1541c, sakila.language#63c8fabf, sakila.language#1fc644c7, sakila.language#11cd751d, sakila.language#47c3cc0c, sakila.language#7894ca3, sakila.language#47066532, sakila.language#74ddda0b, sakila.language#1116441e, sakila.language#4cd21655, sakila.language#74b84dd9, sakila.language#6fff1d6c, sakila.language#55e4d6e5, sakila.language#22d88071, sakila.language#33d88c96, sakila.language#4df5e671, sakila.language#4aec2cb3, sakila.language#576ac232, sakila.language#76a6dbd7, sakila.language#44ab3d1c, sakila.language#46391c7c, sakila.language#4f7d34e8, sakila.language#251c941d, sakila.language#77400ef3]
The EL doesn't access fields of your objects. It accesses bean properties of your objects. This means that ${languages.lang} is translated to a call to languages.getLang().
If you don't have such a getter, you'll get an exception though. If it just doesn't display anything, it's probably because languages is null, or because its lang list is null or empty. To confirm or infirm those guesses, we need to see the code where you create and populate the bean and its list of languages, and where you store it somewhere to make it accessible from the JSP.
Another possibility is that you forgot to declare the core taglib at the beginning of the JSP. To confirm or infirm that, paste the code of the JSP, and the HTML code generated by the JSP (using View page source in the browser)

Accessing the value of the variable defined locally in the method in action class in jsp using Struts2

I am trying to access the value of the variable defined in the method in the action class in jsp using Struts2.
I have a variable defined desc_map in the method of the action class.I want to access the value of this in jsp.
How can I do it?
Thank you.
Create a public getter for the property, preferably named correctly as per the JavaBean spec:
public TheType getDescMap() {
return desc_map;
}
Access it from the JSP:
${descMap}
Or if you need escaping, can't use JSP 2.0+, etc:
<s:property value="descMap"/>
All of this is covered in essentially every Struts 2 tutorial.

Categories