scriplet tag in <s:if condition -Struts 2 [duplicate] - struts2

I want to check value of variable bool_val using Struts 2 tag <s:if> but it's not working.
<%# taglib prefix="s" uri="/struts-tags" %>
<%boolean bool_val=true;%>
real value : <%=bool_val%></br>
expression evaluated value :
<s:if test="%{bool_val==true}">
TRUE
</s:if><s:else>
FLASE
</s:else>
I also tried following test expressions too, but still not working.
<!--
bool_val
bool_val==true
%{bool_val}
%{bool_val==true}
%{bool_val=="true"}
-->

Use struts tag to create a variable like this
<s:set var="bool_val" value="true" />
expression evaluated value :
<s:if test="%{#bool_val == true}">
TRUE
</s:if><s:else>
FALSE
</s:else>
Here is a sample tutorial.

There is a shorter version to the one suggested by Visruth CV :
<s:set var="foo" value="true" />
expression evaluated value :
<s:if test="foo">
TRUE
</s:if><s:else>
FALSE
</s:else>
In case you want to check the boolean value against an Action attribute, here is the way to go :
class FooAction extends ActionSupport {
private Boolean _bar = true;
public Boolean isBar() { return _bar; }
}
And in the jsp file :
expression evaluated value :
<s:if test="isBar()">
TRUE
</s:if>
<s:else>
FALSE
</s:else>

You can't use a scriptlet variable in Struts tags unless you put this variable to the value stack. But you'd better not use a scriptlet variable, but the variable value.
<%# taglib prefix="s" uri="/struts-tags" %>
<%boolean bool_val=true;%>
real value : <%=bool_val%><br/>
expression evaluated value :
<s:set var="bool_val"><%=bool_val%></s:set>
<s:if test="#bool_val == 'true'">
TRUE
</s:if><s:else>
FALSE
</s:else>

If getter method for your boolean variable in Action class is isBool()
then use <s:if test="bool">.
The key is to remove is from the method name and use.
For example, if method is isApple() use <s:if test="apple">.

Related

Set color for even rows using <s:set> - Struts2 [duplicate]

This question already has an answer here:
Using <s:iterator> to populate a dynamic arraylist over multiple columns
(1 answer)
Closed 4 years ago.
I want to give dynamic color/style for even rows of the table. Below is my code but it is not giving me required output.
<table>
<s:iterator value="allMonths" status="incr">
<style>
.evenRow{background-color: yellow;}
.oddRow{background-color: orange;}
</style>
<tr>
<s:if test="#incr.count%2 ==0">
<s:set var="clr" value="evenRow"></s:set>
</s:if>
<s:else>
<s:set var="clr" value="oddRow"></s:set>
</s:else>
<td class="%{#clr}">Month:</font> <s:property/></td>
<%-- <td class="%{clr}">Month:</font> <s:property/></td> --%>
<%-- <td class="clr">Month:</font> <s:property/></td> --%>
</tr>
</s:iterator>
</table>
index.jsp
<%# taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
<s:form action="clickAction">
<s:textfield name="beanobject.uname" label="Enter Username" /><br>
<s:textfield name="beanobject.age" label="Enter Age" /><br>
<s:textfield name="beanobject.sex" label="sex" /><br>
<s:submit value="Submit" align="center" />
<s:submit value="Clear" align="center" action="clearAction"/>
</s:form>
</body>
</html>
MyAction.java
---imports---
public class MyAction extends ActionSupport{
private MyBean beanobject;
private List<String> allMonths;
//Getters & Setters
public String execute(){
System.out.println("execute");
return SUCCESS;
}
public String click()
{
allMonths = new ArrayList<String>();
allMonths.add("Jan");
allMonths.add("Feb");
allMonths.add("Mar");
return SUCCESS;
}
public String clear(){
return SUCCESS;
}
}
I also tried using "bgcolor" attribute but with no success. What am I missing?
Provided required code. I am just hardcoding values in list in ActionClass
You're assuming JSP understands OGNL; it does not. JSP understands JSP EL.
OGNL is strictly within S2 custom tags, not generic JSP.

Struts 2 Populate select drop list

Hi i want to populate the select drop down list with some value. I use Struts 2, Tiles and JSP. I initialize my list in the Action class but i am still getting the following error :
Caused by: tag 'select', field 'list', name 'anneeResultat': The requested list key 'anneesResultatsList' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]
Here is my code in Action class:
private AnneeResultat anneeResultat;
private Map<String, String> anneesResultatsList = new HashMap<String, String>();
public Map<String,String> getAnneesResultatsList() {
this.anneesResultatsList.put("2005","2005");
this.anneesResultatsList.put("2006","2006");
this.anneesResultatsList.put("2007","2007");
this.anneesResultatsList.put("2008","2008");
this.anneesResultatsList.put("2009","2009");
this.anneesResultatsList.put("2010","2010");
this.anneesResultatsList.put("2011","2011");
return this.anneesResultatsList;
}
public void setAnneesResultatsList(Map<String,String> anneesResultatsList) {
this.anneesResultatsList = anneesResultatsList;
}
return SUCCESS;
}
My struts.xml file contains :
<action name="ChoixAxes" class="fr.si2m.occ.web.actions.ChoixAxesAction"
method="execute">
<result type="tiles">choixAxes.tiles</result>
</action>
My jsp is here:
<s:set name="theme" value="'xhtml'" scope="page" />
<s:form action="ChoisirAxes" name="choices" id="choices">
<s:select name="anneeResultat" label="Année de résultats" list="anneesResultatsList"></s:select>
<s:radio label="Listes nominatives" name="listesNominatives" list="#{'1':'Oui','2':'Non'}" value="2" />
<s:submit value="Calculer provisions" name="calculerProvisions"/>
<s:reset value="Annuler" />
<input type="button" value="Critères sauvegardés" id="criteresSauvegardes"/>
</s:form>
Some one could help me please ?
I have this problem since yersterday.
Prepare interceptor calls prepare() on actions which implement
Preparable. This interceptor is very useful for any situation where
you need to ensure some logic runs before the actual execute method
runs.
Your action should extend Preparable interceptor and override prepare() method, give the pre populated data.
Struts2 Prepare Interceptor
Put AnneesResultatsList in session
Map session=ActionContext.getScession();session.put("list",AnneesResultatsList );
<pre>
s:select name="anneeResultat" label="Année de résultats" list="%{#session.list}""></s:select>
</pre>
actually it is,
Map session = ActionContext.getcontext().getsession();
session.put("key",list);
<s:select list="%{#session.key}">

Struts2 Iterator property value (not working)

I am trying to use the struts iterator to write some data from a List. Student is a class I have defined.
The s:property when used with the value attribute does not seem to work at all.
I have the following code in the JSP:
<s:iterator value="students" var="student">
<h2><s:property /></h2>
<h2><s:property value="firstName" /></h2>
<h2><s:property value="student.firstName" /></h2>
<h2><s:property value="top.firstName" /></h2>
<h2><s:property value="[0].firstName" /></h2>
<h2><s:property value="#firstName" /></h2>
</s:iterator>
On the first property tag works. It prints the toString() of the Student object. All other property tags do not print anything at all.
The toString() method of Student class is as follows:
public String toString() {
return firstName + " " + lastName + " " + Integer.valueOf(age);
}
The Student class has the getFirstName() method as follows:
String getFirstName() {
return firstName;
}
I am using Struts 2.1.6
There are no exceptions in the log files.
When viewing source of html, I just see the empty h2 tags.
I wonder what is the issue here. Any help is appreciated.
Your getFirstName() method only has package-level access. It should be public to be accessible from the JSP.
change it to
public String getFirstName()
and you're set.
As you are using "var" attribute in iterator tag you have to refer the value from the object like
<s:iterator value="students" var="student">
<h2><s:property value="#student.firstName" /></h2>
</s:iterator>
You can write the same code without using var attribute like below
<s:iterator value="students">
<h2><s:property value="firstName" /></h2>
</s:iterator>
Both will works same.

access data of struts2 nested iterator in action

<s:iterator var="parent" value="studentList">
<s:iterator var="child1" value="#parent.subjectList">
<s:property value="%{subjectName}" />:
<s:textfield id="subject" name="%parent.subject.id}" theme="simple" />
</s:iterator>
</s:iterator>
I have a jsp page with the above code. I have two lists i) studentList, ii) subjectList.
For each student there is a subjectList. Now I have to save the marks. How can I get these values in my action ? I am using Struts2.
Thanx in advance.
This is how the JSP code will look like:
<s:form action="saveaction" >
<s:iterator value="lstBean" id="lstBean" status="outerStat">
<s:textfield value="%{name}" name="lstBean[%{#outerStat.index}].name"/>
<s:textfield value="%{amt}" name="lstBean[%{#outerStat.index}].amt"/>
<s:textfield value="%{id}" name="lstBean[%{#outerStat.index}].id"/>
<s:iterator value="%{lstString}" status="myStat">
<s:textfield name="lstBean[%{#outerStat.index}].lstString[%{#myStat.index}]"/>
</s:iterator>
</s:iterator>
<s:submit value="Click me to submit lstBean"/>
</s:form>
Following is the bean(XBean) whose List is used in the JSP:
public class XBean
{
private ArrayList<String> lstString=new ArrayList<String>();
private String name;
private Double amt;
private Integer id;
//Getters and setters of fields
}
Now you can simply have a field lstBean with setters in your submitting action (saveaction) and hey you are done.

how to iterate Set in s:iterator in Struts 2

I have a Set with list of objects, i want to iterate this set in s:iterator tag in struts 2 and access one of the property of Object.
How can i achieve this?
Sample code:
class Employee{
String name;
String age;
...getters and setters...
}
...
Set<Employee> empSet = new HashSet<Employee>;
empSet.add( ...some objects)
In Jsp: I want to access employee name
<s:iterator value = "empSet">
<property value=???(how to get employee name) >
</s:iterator>
Thanks
Iterator docs: http://struts.apache.org/2.1.6/docs/iterator.html
You want to do something like this:
<s:iterator value="empSet">
<p>Name is: <s:property value="name"/></p>
</s:iterator>
Note that you'll need a getter for empSet on your Action class.

Resources