difference between request.getParameter() and form.get() - java

I have a basic doubt in struts 1.x
Is there any difference in getting the value from jsp using request.getParameter('name') and form.get('name'), in terms of efficiency.
I know that form.get() returns an object and the former a string.
I want to know in the action class is it worth to get the dynaActionForm from the form argument of execute method and use it to get the user entered values in jsp. Or request.getParameter is enough ? Is there any other use of form object, if I typecast and create one ?

It's better to use request.getParameter().

Related

How to check which of the Object parameters got changed

I have a situation where i am changing few parameters values of an Object.
UserDetails has around 14 parameters.I am changing the values of few parameters and submitting them from a Form .These values should get updated on the database back-end.
Are there any inbuilt functions to check if any of the values got changed?
Are there any inbuilt functions to say which of the values got changed?
No.
Are there any inbuilt functions to check if any of the values got changed?
No.
However, you can implement your own methods to test these things. An equals method is easy to implement, and indeed many IDEs have "wizards" to generate them. A "what has changed" method is more complicated. The complexity comes in how the method tells the caller what fields have changed, and how the caller can make use of this information.
Alternatively, Apache Commons provides a class called EqualsBuilder that uses reflection, etcetera to compare objects based on their fields.
I also agree with JB Nizet. If you are doing this in an attempt to optimize database updates, you are probably wasting your time. You are probably better off just saving the all of the fields.
Consider this. Unless your front-end caches the old values of the fields read from the database while the user is updating the form (or not), your front end is going to have to re-query the database to find the old value. You would be better off just issuing the UPDATE to update all of the fields than doing a SELECT followed by a conditional UPDATE is something has changed.
Probably you can check this link.. I am not sure this can be done in java. But, you can try with javascript. Please check this link. You can do with EXT.js
handler: function(btn, evt) {
var f = btn.up('form').getForm();
f.submit({
url: '/some-path-on-my-server/save/,
getParams: function(useModelValues) {
var falseVal = false;
var fieldParams = this.form.getValues(falseVal, true, this.submitEmptyText !== falseVal, useModelValues, true);
return Ext.apply({}, fieldParams);
}
});
}
https://www.sencha.com/forum/showthread.php?173867-I-want-to-submit-only-dirty-field-values.

SOAP UI passing in a form a list of long

I have to check a GET method (REST) which takes a list of longs as a parameter. What should I insert in the corresponding SOAP-UI form in order to run the method?
For example if the parameter was a String, all I had to do was inserting a the string in the form.
Thanks in advance.
In the Servlet API, all request parameters are Strings, so you don't have many choices. The conversion of those Strings to the type you need for your business logic methods, depends on the MVC framework you're using (or your own code in case you don't use any framework).
In short: The GET method will only send Strings as parameters. It's up to your application to treat those Strings as longs.

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"

Accessing Request object from custom JSP tags

I'm trying to make a set of custom tags that encapsulate form elements (markup and validation).
There's a method given to retrieve the "Out" object easily:
JspWriter out = getJspContext().getOut();
However I can't figure out how to get the request object. I want to be able to directly access the submitted form values from within the Tag class so that I can validate each field.
The documentation is quite sparse, so I thought maybe I could use the JspContext object to somehow get the request attributes. But I don't understand the different scopes.
System.out.println(getJspContext().findAttribute("field1"));
always prints "null".
Enumeration e = getJspContext().getAttributeNamesInScope(1);
Looping through and printing out the enumeration just gives me a list of classes that don't exist:
javax.servlet.jsp.jspOut
javax.servlet.jsp.jspPage
javax.servlet.jsp.jspSession
javax.servlet.jsp.jspApplication
javax.servlet.jsp.jspPageContext
javax.servlet.jsp.jspConfig
javax.servlet.jsp.jspResponse
javax.servlet.jsp.jspRequest
So is this even possible?
If not, could anyone point me to a tag library that deals with form display and validation? I searched the internet for a couple hours and it seemed every single one was discontinued and I couldn't download them. Either that or suggest a better alternative for handling forms.
Edit: The tags extend the SimpleTagSupport class.
If your class is extending TagSupport, you can access the protected pageContext variable. From that you're able to retrieve the request object.
http://java.sun.com/webservices/docs/1.5/api/javax/servlet/jsp/tagext/TagSupport.html#pageContext

How do I call java methods on an object from a FreeMarker template?

Is it possible to call a method that takes parameters from a Freemarker template?
I have an object model that I'm trying to render with Freemarker into a web page. One of the objects has a method to get a sublist of it's contents - taking a parameter that is used to filter the list:
public List getunits(final String type);
I know in JSP you can't do this directly, but you can write custom functions that will allow you to achieve the result you want. How do you solve this in Freemarker? Is it the same with writing custom functions? Or is there some way of actually calling this kind of function?
FreeMarker allows invoking methods that were made available through the model from within expressions.
Assuming your object has been exposed as myBean you can invoke the method as follows:
<#list myBean.getunits("myType") as unit>
do stuff with ${unit}
</#list>
You don't have to use <list>, of course, it's just there as an example since your method returns a list.
As ChssPly76 said, you can just peform the method call from within a Freemarker template, as long as you expose the object in the model.
But it's important to keep in mind that if your method returns NULL (for whatever reason), you are going to get a confusing
Expression myBean.getunits() is undefined on line ....
To avoid this, you should better use myBean.getunits(...)! (notice the exclamation point).
Learn more about how Freemarker handles nulls here: http://freemarker.org/docs/dgui_template_exp.html#dgui_template_exp_missing

Categories