In Struts2, dynamically generate form element name using a member value - java

My requirement: In Struts 2, dynamically generate form element name by using member value.
I had tried with: <s:textarea name="employee_<s:property value='employeeNumber'/>"/>
Resulting code in web browser: <textarea name="employee_<s:property value='employeeNumber'/>"></textarea>
My expectation in web browser code: <textarea name="employee_101"></textarea>. 101 as an employeeNumber is only used for demonstration only.
Please help, thanks!

In Struts2 you can always use OGNL expression in the Struts tag's attribute. Just provide the value from the value stack. You should know that the action bean is on top of it, and having a getter for employeeNumber should return desired value 101.
<s:textarea name="employee_%{employeeNumber}"/>
Also be aware of Struts doesn't allow nested tags in the attributes in favor of OGNL.

try this,it working fine.
MyAction.class
private int property_value;
public int getProperty_value() {
return 6;
}
public void setProperty_value(int property_value) {
this.property_value = property_value;
}
result.jsp
<s:set var="xyz" value="property_value" />
<s:textarea name="emap_%{#xyz}" />

Related

Parameter passed to JSP via struts to JSP variable

Im a Java newbie, I have this code working which obtains the brandCode parameter and places in a hidden input named "brandCode"
<html:form method="get" action="/catalogindexsearch.do" styleId="sortAndNavigationForm">
<input type="hidden" name="formAction" value="searchDisplay" />
<html:hidden property="brandCode" />
</html:form>
But I wish to use the parameter elsewhere in my JSP i.e.
<h1>Brand Code: ${brandCode}</h1>
So my question is how can I create a Java variable from the html:form URL parameter?
In JSTL you can access a form property by form name
<h1>Brand Code: ${formName.brandCode}</h1>
Give it an ID like this:
<html:hidden property="brandCode" id="brandcode" />
then access the value by:
var brandCode = $("#brandcode").val();
or if you're not using jQuery:
var brandCode = document.getElementById("brandcode").value;
If you're trying to use the value of a property from your action class, you had it right - It's
${brandcode}
where brandcode is a property in your action class with getters and setters:
private String brandcode;
public String getBrandCode() {
}
you get the point.
What I was after in the end was this ${param.brandCode} or this ${param["brandCode"]}.

struts2 get property with dynamic value in jsp

I have a problem with struts property, pl check my sample code.
in struts action class i have a property that is name2,
SampleAction.java
public class SampleAction extends ActionSupport
{
private String name2;// setter and getter is also there
}
if i use like below i ll get the value
<s:property value="name2"/>
I wan to get the name2 value in jsp in dynamic way,
but here i need pass the value 2 in dynamic way some thing like below..
<s:set name="nameNumber" value="2" />
<s:property value="name%{#nameNumber}"/>
How can i achieve that.
Thanks in Advance,
Prabhakar Manthena

Reading contents from application map dynamically IN JSP page. - struts 2.0

I am using ApplicationAware in my struts 2.0 action class. So, i placed an entry in then application map as
application.put("animalDTO", animalDTO);
animalDTO:
public class AnimalDTO {
private String name_N1;
private String name_N2;
private String name_N3;
private String name_N4;
// getters and setters
}
Now before going to my jsp page i am populating the values in each of the variables.
In JSP page: i am using iterator to get N1, N2 till N4.
Now i need to create s:textfield and populate these variables content DYNAMICALLY.
ie.
<s:iterator value="application.nameList" var="nameList">
<s:textfield theme="simple" value="%{application.animalDTO.name_#nameList}" />
</s:iterator>
Here, i am not getting any values populated.
If i hard code as -> value="%{application.animalDTO.**name_N1**}"
Then all the text-fields will be populated with value of name_N1, but that not what i need. I need for fetch the values dynamically.
Can anyone help me with this OGNL requirement?
Thanks in advance
=====================
I tried this also, i created string variable which is of the required ognl format, but i dont know how to make the string as a lookup to valuestack. This is what i did:
<s:iterator value="application.nameList" var="nameList">
<s:set var="varUrl" value="%{'application.animalDTO.name_' + #nameList}" />
<s:property value="varUrl" />
<s:textfield name="NAME__%{#nameList}" theme="simple" id="NAME-%{#nameList}" value="%{varUrl}" />
</s:iterator>
but instead the text box is getting values like:
application.animalDTO.name_1
application.animalDTO.name_2
application.animalDTO.name_3
application.animalDTO.name_4
what is need is the values corresponding to these variables.
Any idea how to convert this string as an object/key to lookup into valuestack.
Finally i got the solution. I tried many trail and error methods. And finally hit the answers. We can dynamically read the property values by doing:
<s:iterator value="application.nameList" var="nameList">
<s:textfield theme="simple" value="%{application.animalDTO['name_' + #nameList]}" />
</s:iterator>
I tried %{application.animalDTO['name_' + #nameList]} and it worked. :-)

Struts 2 s:submit button syntax to set values in map

I am browsing some Struts 2 code and I see this syntax for submit button that I haven't seen before..
<s:submit key="map.keyName$Value" />
It's not working (it was working with Struts 2.0.x now we have moved to Struts 2.2.3) any more. I mean its not setting the appropriate value based on the mentioned key in the map.
Has anyone used this syntax before?
Any other alternative syntax suggestions that will let me SET values in a map (using struts tag only) will be most welcome.
The jsp page containing this code is designed to be a decoupled component that can be included by any page at runtime that's why this page CANNOT call any java code to set these values in java map - which is why i am looking for tag solution that can set values in the map.
thanks in advance
Set value in a map by :
In JSP only
OGNL assignment statement :
<s:set var="" value="map[key] = keyValue" />
Java
<s:set var="" value="map.put(key, keyValue)" />
EDIT
You could set value in map (to action class) with
<s:hidden name="map[key]" value="keyValue" />
by submit button with onclick attribute, for example (answer - assume multiple submit button) :
<script type="text/javascript">
function setMap(key, keyValue) {
document.getElementById("mapToSet").name="map['" + key + "']";
document.getElementById("mapToSet").value=keyValue;
}
</script>
<s:hidden name="test" id="mapToSet" />
<s:submit value="Search" onclick="setMap(key, keyValue)" />
I found this page when searching for "how to set values in a map in Struts2" and it led me to the following answer (which I understand is a little OT):
As an HTML input element:
<input type="hidden" name="myField[105]" value="myValue" />
This would populate an action variable declared as:
Map<Integer, String> myField;
such that:
myField.get(105).equals("myValue"); // == true

getting hidden fields value

I am working on a Struts2 application. I am setting the value of a hidden field in JSP with the purpose to access it by JavaScript.
My JSP code:
<s:iterator value="collegelist">
<tr>
<td align="center"><s:property value="collegename"/></td>
<s:hidden name="hiddenname" key="collegename" />
</tr>
</s:iterator>
My JS code:
var myForm = document.frmAction;
var text = myForm.hiddenname.value;
alert("hidden field text is:" + text);
The alerts shows a blank value.
What is the cause and how can I solve this?
Try
element = document.getElementsByName("hiddenname");
alert(element[0].value);
You generate multiple fields having the same name, since your code is inside a s:iterator tag. You should obviously have such a loop in your Javascript as well :
var hiddenFields = document.getElementsByName("hiddenname");
for (var i = 0; i < hiddenFields.length; i++) {
alert("hidden field text is::" + hiddenFields[i].value);
}
Also, verify the the value is not blank in the generated HTML, and that the hidden fields'a name is hiddenname.
I tried your code and it surely works.. problem is somewhere in your server code itself..
Look here: http://jsbin.com/ajajo4/2/edit
Make sure you have only one form with the name "frmAction" and only one hidden field with the name "hiddenname". If you have multiple, you'll get an array instead of a single value.
The root of the problem is that you are inside of an iterator. Struts updates the name for you in order to correctly hook everything up. If you pull up your page and view source, your hidden field will probably look something like this:
<input type="hidden" name="collegelist[0].hiddenname" value="thename"/>
Regardless, if you want the retrieval by name to work, do not trust the name that you supply to a struts tag. Always pull up the generated source and look at what name the field actually has.

Categories

Resources