In servlets - getParameterNames() returns the name passed in the request.
I want to ask that if we have 3 form tags into one html file and we want to get the names from form1 and form3 then how we can pass the reference of the form with getParameterNames.
Enumeration enum = req.getParameterNames();
Thanks..
You will only get the parameters for the form that is submitted. I'm not sure what you are trying to do, but you could have hidden form inputs that contain the values of the non-submitted forms. You can set those hidden values using javascript.
Related
What's the best way for passing values between web forms in ASP.NET. I have several web forms linked to a site master and I want that when certain button is pressed in one form , certain information will be passed to another form and the user will be redirected to that form displaying the information retrieved from the previous form.
Ex: Form1 --> click --> go to Form2 --> Display in Form2 the data received from Form1.
For testing it I tried using Session variables but it hasn't worked until now. I declare the variable in the Page_Load of Form1 as following:
Session["diseno"] = "nombre";
but when I go to Form2 I do the following on the Page_Load:
Response.Write( (string) Session["diseno"] )
however nothing is printed. I would appreciate your help :)
Not sure if its the best way for your scenario as its easy to break and not good for secure data you don't want the user to see but I have used URL parameters, They are simple and you can pass multiple parameters.
Source
You could do something like this on your second page and set the variable to a string etc... and use the variable in a label control etc...
string _name = Session["diseno"].ToString();
To just print out your session variable do this:
Response.Write(Session["diseno"].ToString());
I am using spring mvc form to submit request. I have a hidden variable which i want to pass to the controller. The hidden variable path is using a property which present in the model attribute object. Now I want to pass this hidden variable to the controller. For this one approach is to have the this variable in the model which curremtly is present. But I dont want to put this hidden variable in the model vo object. I just want to pass this hidden field as a request parameter to the controller. Is there any way to do that? If I use html input field type=hidden, will work ?
Please let me know. Below is the code for this. The value of the hi9dden field i am setting from javascript and doing the form submit.
JSP file
<form:form id="form" modelAttribute="customerRelationshipBean">
<form:hidden path="customerSearchBean.action" /> </form>
JS file
document.getElementById("customerSearchBean.action").value='addCustomer';
document.getElementById("form").action = '/gcldw-web/customerSearch' ;
document.getElementById("form").method='POST';
document.getElementById("form").submit();
As soon as you use's spring tags hidden element with a path attribute you establish an associatation with a model bean, and the value ends up in the model
Instead, you can simply add a plain input hidden element, e.g.
<input type="hidden" id="secretValue" name="secretValue" value="" />
place the value to that element (via your js code) and add a suitable #RequestParam argument to your handle method e.g.
public String processSubmit(#ModelAttribute("customerSearchBean") CustomerSearchBean customerSearchBean, BindingResult result,
#RequestParam String secretValue) {
Is there any way to override method in dynamic form?
I've created a form from code (create Form, adding DataSource, etc. and then FormRun). The problem is with the datasource validation. In normal form (in the AOT) I'd use return true in validateWrite to prevent normal validation on table.
How I can achieve this only from code? (or more precisely: when I've only class to play with)
I think the FormBuild.addMethod is what you are looking for. Provide the FormBuildDatasource object as the third argument to the addMethod method.
In my Spring MVC project I have an update page for Class1 that must display a list of form:checkbox tags that is bound to a collection of entities on Class1.
Class1.java:
class Class1 {
private Set<Class2> set;
//... other fields
}
In updateclass1.jspx:
<c:forEach items="${allClass2Instances}" var="class2">
<form:checkbox label="${class2.name}" path="set" value="${class2}"/><br/>
</c:forEach>
With the checkbox tag as above, when I display the page, the checkbox is ticked if the Class2 instance is part of the Set on class1, and unticked if it isn't. But when I hit submit, I get the following error:
Failed to convert property value of type 'java.lang.String[]' to required type 'java.util.Set' for property 'set'; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "Name 1" from type 'java.lang.String' to type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "Name1"
As far as I can tell when the page is populated, the form:checkbox tag needs an instance to set the correct checked/unchecked value, but on submit the JSP is sending an array of class2.toString() values to a converter that expects the IDs. Conversely when I change the tag to the following:
<form:checkbox label="${class2.name}" path="set" value="${class2.id}"/><br/>
The binding works fine, but when I view the update page the checkboxes are not ticked / unticked correctly because the tag does not know that value being passed in is the object id.
How do I make the binding after submit consistent with what the checkbox tag expects?
In case it matters - this is all inside a scaffolding page generated by Roo.
Figured out how to make this work for now. If anyone comes up with a neater solution please add it and I'll mark it correct instead.
The problem above was being caused by needing ${InstanceOfClass2} to evaluate to different things in different places:
At the time of evaluation of the tag, <form:checkbox> needed an expression that evaluated to an actual instance of Class2
After the tag completed, the value attribute of the generated <input type="checkbox"> tag needs to be equal to a numerical ID field of an instance of Class2
The solution was to add a converter to my Class1Controller, eg:
Converter<Class2, String> getClass2Converter() {
return new Converter<Class2, String>() {
public String convert(Class2 instance) {
return "" + instance.getId();
}
};
}
Thus the expression ${InstanceOfClass2} evaluates to a Class2 instance for the checkbox tag, but when it comes to writing the actual HTML is converted to a numerical ID.
This approach is very messy when working with Roo. All of the other scaffolding relating to Class1 then wants to use this same Converter, so I started seeing a whole bunch of IDs everywhere that you would want to see Class2.name or other such fields. I solved this by modifying the Spring Roo <field:display> custom tag - added an attribute fmtCollectionToString that if present forces the tag to evaluate collections by iterating them and calling toString on each element, instead of calling spring:eval on the whole collection, which also seems to end up with the Converter being invoked.
Like I say, neater solutions greatly appreciated! If there's a way of making converters behave differently in different circumstances, for instance - still want to hear it.
asp.net mvc model object is being fetched by ajax call - $.ajax(....
form has fields with IDs exactly to matching properties on returned json object (created by Html.TextBox("NAME", Model.Order.NAME) )
How to automatically populate fields(inputs) with corresponding json object properties ?
Manually would be like $("#NAME).val(json.NAME) so how to make this dynamic?
Is there some kind of reflections (like System.Reflection in c#) for javascript/jquery ?
Maybe something like this:
$("#formId input").each(function(){
$(this).val(json[$(this).attr("id")]);
});
... which iterates over all the form inputs, and looks for a JSON entry with the inputs ID.
The thing to note here is that you can retrieve json.NAME via json["NAME"].