Spring form saves two similar values for one field - java

I have a spring form bind to object. When form submitted I get Object's name field with additional name. E.g. field value is "test". When it is saved it becomes "test,test". If saved again then it becomes "test,test,test".
I have binded form field as
<form:input path="groupName" id="groupName" />

You might be sending two values for same parameter.
One will be going through for post parameter and one will be going in action URL as get parameter.

Related

How to pass multiple parameters using html link in Struts1.x

I am trying to pass multiple parameters to my Struts action class using <html:link> property.
I am having a link, it should take two parameters from the JSP page to my action class.
How to achieve this?
In Struts 1.3 parameters could be set to the action attribute like in this example
<html:link action="/path/to/action?param1=2&param2=${param2Value}">Some text</html:link>
Quote from the documentation:
If you prefer to specify a java.util.Map that contains all of the request parameters to be added to the hyperlink, use one of the following techniques:
Specify only the name attribute - The named JSP bean (optionally scoped by the value of the scope attribute) must identify a java.util.Map containing the parameters.
Specify both name and property attributes - The specified property getter method will be called on the bean identified by the name (and optional scope) attributes, in order to return the java.util.Map containing the parameters.
As the Map is processed, the keys are assumed to be the names of query parameters to be appended to the hyperlink. The value associated with each key must be either a String or a String array representing the parameter value(s), or an object whose toString() method will be called. If a String array is specified, more than one value for the same query parameter name will be created.
Supplmenting these two methods, you can nest one or more tags to dynamically add parameters in a logic-friendly way (such as executing a for loop that assigns the name/value pairs at runtime). This method does not compete with the aforementioned; it will adds its parameters in addition to whatever parameters are already specified.
You can also use a regular HTML <a> tag and create the URL using the standard <c:url> tag from the JSTL.
why don't you go with ajax calling ? by using ajax you can pass many parameter to action class by set the method K

JSP expression language and dynamic attribute names

I'm trying to do something that seems like it should be relatively straightforward and running into a bit of a wall.
Let's say I've got a list of products which I expose as a request attribute under the name products. Let's also say that each product has an id field, and that I also have a bunch of request attributes set in the form of selectedProduct_<product-id> to indicate which ones are selected.
I understand there are better ways to represent this information, such as placing all the selected ids into a Map and checking against that, but let's assume that I don't have access to that approach for whatever reason.
So what I'd like to do is iterate products and emit some markup only if there is a selectedProduct_... attribute set for the current product. Something like:
<c:forEach var="product" items="${products}">
<c:if test="${! empty selectedProduct_${product.id}}">
<div class="productId">${product.id}</div>
</c:if>
</c:forEach>
But of course that doesn't work, as it dies on ${! empty selectedProduct_${product.id}}.
What will work is if I hardcode the product-id into the expression, like:
${! empty selectedProduct_17}
...assuming that '17' is a valid product id. Obviously that's not practical, but hopefully it illustrates what I'm trying to accomplish. Basically I need to:
Determine the correct selectedProduct_... value to use for each iteration in the forEach loop. Something as simple as <c:set var="key" value="selectedProduct_${product.id}"/> would do this, except I'm not sure how to then take key and use it to get the value of the request attribute with that name (without cheating and running literal Java code inside a <% %> block).
Get the value of the request attribute whose name I determined in #1. This seems to be the tricky part.
Is this possible using pure JSP/JSTL? I know I could run some Java code inside of <% %> to solve this, but that seems like it would be in exceedingly bad form. Surely a more elegant solution exists?
You can by using implicit objects:
There are objects that allow access to the various scoped variables described in Using Scope Objects.
pageScope: Maps page-scoped variable names to their values
requestScope: Maps request-scoped variable names to their values
sessionScope: Maps session-scoped variable names to their values
applicationScope: Maps application-scoped variable names to their values
When an expression references one of these objects by name, the appropriate object is returned instead of the corresponding attribute. For example, ${pageContext} returns the PageContext object, even if there is an existing pageContext attribute containing some other value.
So, for example:
<c:set var="selectedProductAttrName" value="selectedProduct_${product.id}"/>
${requestScope[selectedProductAttrName]}

Jasper Reports, pass a list/array within a bean

I have to generate a report, that displays info about one object ( so the input is only one bean ).
The first problem is - this bean should contain lists of sub-beans( for example, comments, with comment type and comment date ). So I can pass them to a sub-report.
The second problem is - there is an array of 4 sub-beans, that contains few fields. I can create a separate field for each sub-bean's property ( firstSubBeanName, secondSubBeanName... ) , but it's ugly :(. Ideally, there should be a way to access these beans in a such way :
$F{test}[0].name
Please help.
If your list have a name (ie is a property of the object), you have just to pass as Data Source Expression for the subreport this property
$F{subBeansList}
The subreport should be ready to receive such kind of data. If you are using struts, it is possible you should use this class:
org.apache.struts2.views.jasperreports.ValueStackDataSource
as intermediary on your list field to be passed to the subreport.
The second question, the easy way is to use a list, so you can use this expression:
$F{test}.thelist.get(0)

In Springs Velocity binding macros (or JSP taglibrary) what is the reference to "command"?

In Spring 3, if I have a form object like so:
public Person {
String firstname = null;
String lastname = null;
// ... getter & setters ...
}
I now understand that I can receive this object as a parameter in my controller method and that Spring will bind the form parameters to the getters/setters of my Person object.
Now I want to use springs binding macros (equivalent to the JSP taglib's for binding to form objects).
All the docs I read say that there is a default "command" object (what this means I don't yet understand). It seems like I should be able to reference the Person object via a call such as:
#springBind("command.firstname")
But I don't understand where "command" comes from or what it means, and I don't understand what I need to do in my controller to make this "binding" possible.
Do I need to add something to the model?
Do I need to define for Spring that the Person object is the form object?
Should I add <"command", new Person()> to the model before rendering the page, or re-rendering the page after a form submission with errors?
What went into the controller to make "command" meaningful to the macro?
I'm confused here and just missing some really really really simple concept I think.
Yes, you need to add the command object in the Model. And you need to put it there already when rendering the empty form, so model.addAttribute("command", new Person()) is correct way. On validation errors you might want to use the submitted Person object instead, so that the user wouldn't lose all his submission data.

logic: equal with the contents of a hidden field

I have a hidden field:
<input type="hidden" name="champs" id="champs">
I want do a <logic:equal> with the content of this field hidden, I tried the solutions but not work
<logic:equal name="virement" property="statut" value='champs' >
just be there is a syntax very precise in the value property of the logic: equal that I can found.
I'm a little rusty with this, but here are some hints:
Your field "champs" is a browser field, not a Java variable. When your request arrives at your servlet, the contents of browser fields have been transferred to request parameters. So the value in your field will end up in a predefined object called request, and for convenience broken down some more in a bean called param. There's a syntax for accessing this stuff... see below.
A little more detail (though I've seen it explained better) can be found in this page and maybe this one. Better yet, here
You can Google for "JSP Expression Language" to get more information.
You can use the struts tag html:hidden instead of input type = "hidden"

Categories