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

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

Related

Why does PropertyDescriptor return a property name with uppercase as first character?

I'm obtaining an information about a class via
Introspector.getBeanInfo(this.getClass()).getPropertyDescriptors()
then getting the property's name by invoking propery[i].getName().
Everything is fine if a property has no one-letter-part. For example, if a property has a name personAddress (meanwhile its getter/setter -> getPersonAddress(), setPersonAddress(String personAddress)), is's OK, getName() returns personAddress.
But if the property has a name rPersonId (getRPersonId(), setRPersonId(Long rPersonId)) then getName() returns "RPersonId", i.e. first letter has been capitalized! Why?
According to this: https://docs.oracle.com/javase/7/docs/api/java/beans/FeatureDescriptor.html:
public String getName()-> Gets the programmatic name of this feature.
So why does it return a name somehow related to its getter's or setter's name instead of the real name of the property?
This is actually the documented behaviour.
First of all, property names are entirely located by discovering their getter and setter, and not by looking at the fields of the class. This is specified in paragraph 8.3 of the Java Beans specification:
If we discover a matching pair of get<PropertyName> and set<PropertyName> methods that take and return the same type, then we regard these methods as defining a read-write property whose name will be <propertyName>.
So when you have the introspection of a class that contains Long getRPersonId() and setRPersonId(Long), a property can be extracted from it. The name of the property generally follows from lower-casing the first letter, and keeping the rest unchanged. But this is not always the case, the exact rule is in paragraph 8.8:
Thus when we extract a property or event name from the middle of an existing Java name, we normally convert the first character to lower case. However to support the occasional use of all upper-case names, we check if the first two characters of the name are both upper case and if so leave it alone. So for example:
FooBah becomes fooBah
Z becomes z
URL becomes URL
We provide a method Introspector.decapitalize which implements this conversion rule.
In the example above, the getter and setter would provide the String RPersonId to turn into a property name. Since the first two characters are upper-case, the first character will not be lower-cased. As such, the property name that will be derived is RPersonId, and this explains your output.
You can also call the method decapitalize to see which property name would be located from a pair of getter / setter:
System.out.println(Introspector.decapitalize("RPersonId")); // prints RPersonId
System.out.println(Introspector.decapitalize("PersonAddress")); // prints personAddress
Because this is part religion.
The people who did Java Beans felt that properties should ALWAYS be accessed by a pair of methods. So you are NOT getting the name of the data member. You are getting the property which can only be accessed by the methods.
This is from the documentation:
A PropertyDescriptor describes one property that a Java Bean exports
via a pair of accessor methods.
https://docs.oracle.com/javase/7/docs/api/java/beans/PropertyDescriptor.html
The theory was you should never use the data member name, and so through the Bean interface, they don't give you that.

Distinguish empty value from empty string ("") in Url query parameter

How to distinguish URL without value like this /url?var from /url?var="" in Spring MVC?
Method HttpServletRequest::getParameterMap() in controller returns "" in both ways.
I need this to separate commands from queries to specified resource.
One simple way of going about doing what you want to is use the getQueryString() of HttpServletRequest. You would just check and see if the returned String contains the pattern you are looking for.
If you need something like that often (as in many controller methods) you could also easily create a custom HandlerMethodArgumentResolver that will indicate the presence of a String in the URL.
Here is the relevant Javadoc and here is an example
/url?var is a valid URL which states that you have a parameter var which is not initialized.
So by default it is initialized to empty string. That's the framework behavior.
If you don't want to see that parameter coming in HttpServletRequest::getParameterMap(), just don't use it with URL (i.e. /url should be your call)

Spring #RequestMapping value explicit/implicit

In some cases I have seen two different ways to specify uri in #RequestMapping:
#RequestMapping(value="/somepath")
#RequestMapping("/somepath")
The latter is often used at class level. So, is it always legal omitting the value attribute?
The Java Language Spec states
It is legal to use single-element annotations for annotation types with multiple elements, so long as one element is named value, and all other elements have default values.
Since #RequestMapping has a defined value attribute and all other attributes have default values (empty arrays), it is always legal to omit spelling the name value if you don't specify any other attributes for the annotation:
#RequestMapping("/somepath") // valid
#RequestMapping("/somepath", method = RequestMethod.GET) // INVALID!
For #RequestMapping annotation value is the primary attribute, it is mandatory.
1)#RequestMapping(value="/somepath"):
Here you can mention remaining attributes, for ex: method,produces
2) #RequestMapping("/somepath"):
Here you can not mention rest of the attributes, as by default "/somepath" will be taken as value attribute.
Check this

Spring form saves two similar values for one field

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.

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)

Categories