I'm a jstl newbie so probably this question will sound to you funny.
Anyway, I have a model with a List as property and I'd like to fill this with a list of values (chosen from a list of checkboxes). I'm using the useBean tag in the form-processing jstl page, but doing this:
<jsp:useBean id='subscription' class='Subscription'>
<c:set target='${subscription}' property='priviledge' value='${param.priviledge}'/>
Where the property priviledge is a List and $param.priviledge the values of a series of checkboxes, I get a
javax.servlet.jsp.el.ELException: Attempt to convert String "ads" to type "[Ljava.lang.String;", but there is no PropertyEditor for that type
"ads" is one of the values I have selected. I thought the values of the priviledge field was already a List but it seems it works in a different way. I tried to iterate through the $param.priviledge object and I get all the values with no problem.
How can I use this list to fill a List?
Thanks for any help.
Roberto
Attempt to convert String "ads" to type "[Ljava.lang.String;"
This error suggests that the setter is setPriviledge(String[] arr), not a java.util.List.
The values in the param map are Strings; to get all the values as an array, use the paramValues map.
${paramValues.priviledge}
The property on the Subscription bean would need to be a String array:
private String[] priviledge;
public String[] getPriviledge() {
return priviledge;
}
public void setPriviledge(String[] priviledge) {
this.priviledge = priviledge;
}
(I don't know if you've simplified the code to post here, but you should not use the default package - many web servers won't like it and won't be able to instantiate your Subscription class.)
Related
I'm playing around with the ZK 8 MVVM form validation system and generally it seems to do what I want, but I wonder what the definition of the dependent property index is...
Let's take a simple validator...
public class FormValidator extends AbstractValidator {
#Override
public void validate(final ValidationContext ctx) {
Property[] properties = ctx.getProperties("firstName");
Object value0 = properties[0].getValue();
Object value1 = properties[1].getValue();
}
}
So, when this is called before the save command, for every property, I get a Property[] array of length 2. But somehow, I have yet to find out what is stored in [0] and what is stored in [1]. Sometimes it seems that [0] stores the current value (which may or may not be valid according the field validator there) and [1] the last valid entry... But sometimes it seems to be the other way round...
The examples in the documentation always seem to simply take the first element ([0]) for validation, but I would like the understand what both parts of this pair actually mean...
Anyone got an idea for that?
I might be off the mark with my answer, but if you are using ZK8, you should look into using Form binding
That way you do not have to handle Properties in your validator and can retrieve a proxy object matching the bean you use for your form.
If you are using a User POJO with a firstName and lastName attribut.
User myProxy= (User ) ctx.getProperty().getValue();
And then you can validate both fields by simply doing getFirstName and getLastName on myProxy.
Hope it helps.
In login Action I am checking user authentication, and if it is validated, I am putting the user bean into sessionMap:
public String execute()
{
if(userValid)
sessionMap.put("userBean", userBean); //userBean retrieved from DB
}
Now on the landing jsp, when trying to retrieve the session items:
<s:property value="#session.userBean.name" />
Obviously this would return an Object type, as I am storing it that way, so how can I type caste this to UserBean class.
I was expecting to get a solution for this on Google, but found it nowhere since this seems to be a basic implementation. So please let me know if there is any other way to implement this functionality using Struts2.
This works fine for me...
<sp:property value="#session.usertype"/>
<sp:property value="#session.bean.loginID"/>
This both worked fine for me...
sessionMap.put("bean", loginBean);
sessionMap.put("usertype", loginBean.getUserType());
I declared something like this....
Just make sure that in property tag you you same name you used while setting the bean in sessionMap ....
This should probably work....
Obviously you can't cast it to UserBean class if the object is not the instance of that class. In the value attribute you have put a string "#session.userBean.name". Struts parse this string for OGNL expression and if it's a valid expression that returns a value, it will replace it with that value. The returned type is Object, but this type is determined by ValueStack implementation.
Then property tag writes this object to the out. It uses toString() to convert the object to string. And if your object implements this method, then this value would be written.
Looks like your expression returns an Object, which has instance type String, so it's already implemented this method.
I'm filtering my dataTable and want to filter it with custom function. The problem is, that I want to take as input two columns of datatable. Default signature for implementing filterFunction is public boolean filter(Object value, Object filter, Locale locale) and in value parameter it holds contents of whatever is passed by "filterBy" attribute. What's making it's more tricky - that the field I filter with is not the key - it can have same values. Is it possible to pass more than one value to "filterBy" or somehow get the whole dataTable line in filter function in backing bean?
I found a way to solve it: whatever is passed to "filterBy" attribute is in EL(Expression Language), so I can write my filter like this:
in xhtml:
... filterBy="#{item.property1};#{item.property2}" ...
and in filter function:
public boolean filter(Object value, Object filter, Locale locale){
...
String[] properties = value.toString().split(";");
...
I'm having trouble getting this to work. I'll keep my code and idea generic since I've seen a few examples of people with similar problems:
The basic idea is that the view has a list of order items with properties that the user can modify inside a form. When the user submits the form, I want the order items populated with the data that the user submitted.
How do I create the HTML form that populates this #ModelAttribute("orderItems") which is an ArrayList of OrderItemBeans
Controller Code:
#RequestMapping("/order/{orderId}/save")
public String saveOrder(Map<String, Object> map, #ModelAttribute("orderItems") ArrayList<OrderItemBean> orderItems) throws Exception
{
...
}
Java Bean Code: (getters and setters implied)
public class OrderItemBean
{
String orderItemId;
String itemName;
}
I'm not sure where I'm going wrong as I am still learning about Spring.
I always use declaration params via interface.
try change ArrayList -> List
also be sure that your OrderItemBean declarate with #XmlRootElement annotation and under context.
I have a Struts 2 (JDK 1.7, Struts 2.2.1) application that contains a list of filtering criteria, stored as strings in a map.
Map< String, String > m_filters = new HashMap< String, String >();
public Map< String, String > getFilters() {
return m_filters;
}
I pass a URL formatted like this:
http://myserver.com/myapp/GenerateReport.action?reportID=Whatever&filters.fromDate=0&filters.toDate=2000000000&filters.FcsType=piv_cardholder_3kp&detailed=true
Even though the Map has both key & value types specified as String, attempting to get a value from this
Map< String, String > filters = getFilters();
String value = filters.get( "fromDate" );
causes this exception to be thrown:
java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String
I reproduced this in a unit test, and confirmed in a debugger that Struts 2 seems to be creating a String[1] instead of a String for each of the parameters. i.e. it is a length-1 string array with the only string being the expected value ("0" in this case).
My question is: Is this a bug in Struts2, or am I doing something incorrectly?
If it is a bug, is there a known workaround?
There are no issues if you follow Java bean conventions.
Here are some guidelines to resolve the issue:
This issue does not happen if you name the private member "filter" and provide only a getter for filter.
This issue does not happen if you provide a public setter (even if you use a different name for the private member such as m_filter) in addition to the getter
this issue only happens if you do not provide a setter and the property does not have the same name as the getter.
Long story short follow conventions. Above behaviour tested with Struts 2.3.4
What I'm guessing is happening: The getter allows for setting too (If there was only a setter you could only set one item in the map, well with the current parameter interceptor this is the case). The bean is inspected for the property to see how it should do type conversion probably first it looks for a setter failing that it looks at the action for a propterty of that name to determine the type failing that it will need to use the default. The default parameter type is a mapping of String to String[] and this is what you are seeing.
You're using the wrong notation. filters.fromDate would be the equivalent of getFilters().setFromDate(), which is not actually what you want. Dot notation is for JavaBeans.
Try using brackets, such as filters['fromDate'].
Refer to: http://struts.apache.org/2.2.1/docs/type-conversion.html#TypeConversion-RelationshiptoParameterNames.
Try this:
Object[] obj = (Object[]) filters.get( "fromDate" );
String value = (String)obj[0]);
It's not a bug you can consider it's a feature. Access RequestParameters
ParameterAware Interceptor
Let us suppose /http://localhost:8080/myApp/login.action?name=struts2&name=rocks
if you try to access the name parameter string[0] = struts2, string1=rocks