I am having a struts app with a jsp page that populates check boxes from collection as below
UserDetails.java
public class UserDetails extends ActionForm
{
private Collection userSkills;
.
//Getters and Setters for userSkills
.
.
.
public void reset(ActionMapping mapping, HttpServletRequest request)
{
userSkills = new ArrayList();
userSkills.add(new LabelValueBean("java", "Java"));
userSkills.add(new LabelValueBean("mysql", "MySQL"));
userSkills.add(new LabelValueBean("php", "PHP"));
userSkills.add(new LabelValueBean("css", "CSS"));
userSkills.add(new LabelValueBean("html", "Html"));
}
}
I am Populating the JSP page with the values from collection as Below
RegisterUser.jsp
<logic:iterate property="userSkills" id="userDet" name="User">
<html:checkbox property="label" name="userDet" indexed="true">
<bean:write property="label" name="userDet"></bean:write>
</html:checkbox>
</logic:iterate>
The Output is as below
Now while submitting the form I want the values from the Checked Check box alone.I don't want to write 5 separate lines for getting values from each check box. How to do this
Thanks for the help.
Struts has a tag that fit your needs : multibox, you can see these links :
http://struts.apache.org/release/1.2.x/userGuide/struts-html.html#multibox
http://www.jguru.com/faq/view.jsp?EID=925277
And convert your collection to an array
In Userdetails.java
public class UserDetails extends ActionForm {
private String[] userSkills;
//setter and getter for userSkills
}
In RegisterUser.jsp
<html:multibox property="userSkills" value="java"/> java
<html:multibox property="userSkills" value="mysql"/> mysql
<html:multibox property="userSkills" value="php"/> php
Try to use this
Related
Heyy,
I want to take a list of data in my request param,here "personIdCollection" is a set of list but when i am hitting through postman i am getting a bad request.
Here is my code.
controller
#PostMapping("/face-tag-data")
public String getFaceTaggedData(#RequestParam String projectId,#RequestParam List<String> personIdCollection) {
return null;
}
and here is my ajax
var data = {};
data.personIdCollection = personIdCollection;
data.projectId = $("#projectId").val();
$.ajax({
type:'POST',
url:contextPath+'/face-tag-data',
data:data,
success:function(resp){
console.log(resp);
},
failure:function(resp){
console.log(resp);
}
});
This is working for me. I do not use an ajax-request but instead submit my form directly but it should work either way.
My controller looks like:
#RequestMapping(value="addSingleArticles", method=RequestMethod.POST)
public #ResponseBody String addSingleArticles(
ArticleSubmitData pupilPackageData,
HttpServletRequest request) {
... // do something with the data
}
As you can see I have defined my own composite type which consists of three lists. So you obviously can use it with only one list directly.
public class ArticleSubmitData {
private List<Article> singleArticles;
private List<Article> packageArticle;
private List<Article> popupArticles;
... // getter & setter, inner classes etc.
}
In my server page oder faclet I use the following html-code to make this work
...
<input id="" class="" type="text" name="singleArticles[${line.index}].engraving" />
...
So the trick is to define the variables on your webpage as an array and use this in your controller as a list. As you can see in my example I also use an inner class in my composite class which has extra attributes.
I have an issue using an attribute of an arraylist in my jsp.
The arraylist in my ActionForm :
private ArrayList<Account> accounts = new ArrayList<Account>();
The class declaration of the Account object in the Arraylist :
public class Account implements Serializable, Cloneable {
private String bic;
public String getBic() {
return bic;
}
public void setBic(final String newBic) {
bic = newBic;
}
}
The call in my jsp :
<bean:write name="BankAccountsActionForm"
property="accounts.get(0).bic" />
The console error :
javax.servlet.jsp.JspException: No getter method for property accounts.get(0).bic of bean BankAccountsActionForm
Do you have a solution or another way to do this?
I have a terrible alternative using a property accountbic1 directly in the form. But it induces lots of work behind to re affect all the temporary attributes to the real ArrayList.
If you have a collection of items in Struts 1.x, then use the <logic:iterate> tag.
Add the struts-logic.tld taglig on top of your JSP as follows:
<%# taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
Then, using <logic:present> and <logic:iterate> you can iterate your ArrayList as follows:
<logic:present name="accounts">
<logic:iterate id="account" name="accounts">
<bean:write name="account.bic" />
</logic:iterate>
</logic:present>
If you want to iterate a collection and access a particular index, use the indexId on <logic:iterate> like so:
<logic:present name="accounts">
<logic:iterate id="account" name="accounts" indexId="index">
<logic:equal name="index" value="0">
<bean:write name="account.bic" />
</logic:equal>
</logic:iterate>
</logic:present>
The same can be done using JSTL:
<logic:present name="accounts">
<logic:iterate id="account" name="accounts" indexId="index">
<c:if test="${index == 0}">
<bean:write name="account.bic" />
</c:if>
</logic:iterate>
</logic:present>
Make sure that Account class has a getter/setter method for attribute bic.
This is simply the error of getter and setter method. recode your getter and setter according to POJO standard as shown below:
Remove final from setter method and change your setter and getter method name as per POJO standard as shown below :
public String getBic() {
return bic;
}
public void setBic(String bic) {
this.bic = bic;
}
try some thing like:
<bean:write name="BankAccountsActionForm" property="accounts.get(1).bic" />
As it is a ArrayList not Array.
Make sure you have getter setter for accounts in in action class ** BankAccountsActionForm **
public List getAccounts ();
public void setAccounts(List acc);
Change your setter and getter method name as per POJO standard as shown below :
public String getBic() {
return bic;
}
public void setBic(final String newBic)
{ bic = newBic; }
It will work fine
remove final from setter method and try again and write it as follows
public void setBic(String bic )
{ this.bic = bic ; }
I am new to Struts2. I have a JSP page which shows a list of Rooms (where Room is a user-defined class). I need to send this entire list to the action class as a hidden field. The JSP code is as follows:
<form method="GET" action="reporting.action" >
<s:hidden name="roomsReport" value="%{allRooms}"/>
<s:textfield name="roomsR" value="%{allRooms}"/>
<s:submit name="action" style="width:220px;" value="Generate Report for Rooms" />
</form>
The textfield (used for testing) shows the address for the list (implying that its not null in the jsp page)
I am still unable to access it in my ReportingAction class using the following code:
System.out.println("xxx"+this.roomsReport.size());
System.out.println(this.roomsReport);
Both above print statements give 0 and [] respectively. I have the getters and setters for roomsReport as follows:
private List<Room> roomsReport;
public List<Room> getRoomsReport() {
return roomsReport;
}
public void setRoomsReport(List<Room> roomsReport) {
this.roomsReport = roomsReport;
}
Can anyone help ?
Finally I could pass the list to the jsp using session
The list is set in a session variable in jsp as follows:
<s:set name="roomsReport" value="%{allRooms}" scope="session" />
In the action class I can access it using the following code:
List<Room> roomsReport = (List<Room>)ActionContext.getContext().getSession().get("roomsReport");
I am new to the jsp and interested in display tag for its export functionality. Say, I have this simple structure:
public class MyActionBean implements ActionBean{
List<Country> countries;
// getters and setters and some other un-related logic
}
public class Country {
List<String> countryName;
List<Accounts> accounts;
// getters and setters and some other un-related logic
}
public class Accounts {
private FinancialEntity entity;
// getters and setters and some other un-related logic
}
public class FinancialEntity {
String entityName;
// getters and setters and some other un-related logic
}
Now, I want to make a table, which will have two columns - country name and entityName(FinancialEntity)
<display:table id="row" name="${myActionBean.countries}" class="dataTable" pagesize="30" sort="list" defaultsort="8" export="true" requestURI="">
<display:column title="Country" sortable="true" group="1" property="countryName" />
<display:column title="Financial Entity"> somehow get all of the entity names associated with the country? </display:column>
</display:table>
So, essentially I want to iterate over the accounts and get all the financial entities. I do not know how to do that in JSP with displaytag. I tried to use c:forEach and display:setProperty tag, but it look like this tag is not for these purposes. I am deadly stuck :(
Thank you in advance :)
You don't have to do the work in the jsp. You could do that work in a model object and the controller.
public class CountryFinancialEntity {
private Country country;
public CountryFinancialEntity(Country country) {
this.country = country;
}
public String getCountryName() {
return this.country.getName();
}
public List<String> getFinancialEntityNames() {
List<String> financialEntityNames = new ArrayList<String>
for (Account account : this.country.getAccounts() {
financialEntityNames.add(account.getFinancialEntity().getName();
}
}
}
Then make a list of these objects for all your countries and pass this object to your view (jsp).
Hopefully this will simplify the use of the display tags and allow you to use a c:forEach tag.
EDIT
If you MUST do this work in the jsp.
I would recommend just passing the list of countries. The MyActionBean really doesn't help and could cause confusion.
Your jsp will look something like the following:
<display:table id="country" name="countries">
<display:column title="Country Name" property="name" />
<display:column title="Financial Name" >
<ul>
<c:forEach var="account" items="${country.accounts}">
<li>${account.financialEntity.name}</>
<c:forEach>
</ul>
</display:column>
</display:table>
BTW, this is most likely how the CountryFinancialEntity would look as well come to think of it, but if you're going to have other columns then using something like the CountryFinancialEntity object, but calling it a TableRowModel instead.
I have a command object associated with a spring form controller:
public class PluginInstance {
private Set<PluginParameter> pluginParameters = new HashSet<PluginParameter>();
... some other string/long properties and getter setters...
}
the PluginParameter also have a Set in it which contain the values
public class PluginParameter {
private String parmName;
private Set<PluginParmvalue> pluginParmvalues = new HashSet<PluginParmvalue>();
...some other string/long properties and getter setters...
}
(Normally the pluginParmvalues will contain only one value, a list have been used for future expandability)
In the spring form I binding the values as
<form:input path="pluginParameters[${itemsRow.index}].pluginParmvalues[0].parmValue" />
but the thing is that there can be a form:select(to present multiple predefined options to the user) or form:input (user can input any value). This has to be decided from another object
public class PluginConfigParm {
private String parmName;
private ArrayList<String> choices;
...getter setters and other properties
}
where I have to compare the name of PluginConfigParm.paramName with PluginParameter.paramName when they match and PluginConfigParm.choices.size() > 0 then form:select will be shown populated with the values from PluginConfigParm.choices otherwise form:input will be shown.
The question is simple: How can I do that.
Any help will be highly appreciated.
By using List<> instead of Set<> in controller. Problem solved. May be Set<> has no getter/setter that can be bind with spring form.
So <form:input path="pluginParameters[${itemsRow.index}].pluginParmvalues[0].parmValue" />
and List<> in controller makes my life easier.
The Set is not an indexed collection so I could not work by using this syntax
pluginParameters[${itemsRow.index}].pluginParmvalues[0].parmValue
eg:
class person{
set name<string> = new HashSet<String>()
}
<input type="hidden" path="person.name" name="person.name" value="<%=valueStr%>"/>
take valueStr = "hello, world"
by giving it as comma seperated values.. set works
Its working for me