I would know how remove or update value from a property of FormBean(action form) in struts 1.
I have some properties which are (name, code, cellphone). I want remove after request the value from name because I need perform comparison again in that field.
The variables of my FormBean are fill through from <html:hidden> that I send to my form using a javascript function but I when performed the second request the values are all there again.
how to solve this ?
after all the work is done in your StrutsAction. Right before the forwardStatement you could do this:
formBean.setName("");
request.getSession().setAttribute("formBean", formBean);
Related
I have an impex file(example)
INSERT_UPDATE Subscriber;firstName[unique=false];lastName[unique=false];email[unique=true];bulk(code)[default=false]
;FirstName;lastName;email#gmail.com;true;
and an InitDefaultInterceptor
public class MyInterceptor implement InitDefaultInterceptor<SubscriberModel>{
onInitDefaults(SubscriberModel model, InterceptorContext ctx)
}
How can I get values from the impex in this interceptor? I try to use
model.getFirstName();
....
but all the methods returns "null". What can I do to get the values ? I need to implement a logic before I save them into db.
If you want to make use of the values that are being sent, you need to use a PrepareInterceptor instead of a InitDefaultInterceptor interceptor.
InitDefaultInterceptor : The Init Defaults Interceptor is called when a model is filled with its default values. In your case, this happens at creation of the new instance of the object you want to add to the database. This interceptor is used to add default values (next to the ones you already defined in your items.xml). Only the defaults, marked in your items.xml are inserted at this point. No data from your impex is loaded here, as this just handles the defaults for new objects, no matter the content that will be added at a later stage.
PrepareInterceptor : The Prepare Interceptor is called before a model is saved to the database. Use this to add values to the model or modify existing ones before they are saved. In this interceptor, the values of your impex will be filled in the model object. You can add or modify your data here depending on your usecase.
For more info on all type of interceptors, there is a help page from SAP that describes all of them.
Just curious to know in which scenario we should go for #RequestParam and #PathVariable. I know that:
#RequestParam takes parameter value whereas #PathVariable takes placeholder value
#RequestParam can be optional (required=false) while making request whereas #PathVariable value has to be provided.
When we want to use #RequestParam we have to know the property syntax but for #PathVariable not required
Is there any other reason to go for specific one?
Use #PathVariable if you want to adhere to 'statefull' URLs.
For Example:-
/customer/:id Customer view/edit page
/customer/ Customer Add page
/customer/list List Customer Page
/customer/:cid/order All order of a Customer
/customer/:cid/order/:oid Specific order of a partucular Customer.
Wisely using Path Variable will result in URL that gives you hint/clue about what the resulting view/page means.
This also lets you support refresh,back & forward operation with no
extra effort.
#RequestParams can be used to exatract data which is not passed as path params. Your MVC handler can have combination of two as required.
org.springframework.web.bind.annotation.RequestParam is used to bind Query String.
org.springframework.web.bind.annotation.PathVariable is used to bind URL path.
org.springframework.web.bind.annotation.RequestBody is used to bind HTTP Body.
org.springframework.http.RequestEntity will give you some added flexibility in defining arbitrary HTTP Entity headers along with HTTP Body.
Best Practice:
If you want to identify a resource, you should use Path Variable.
But if you want to sort or filter items, then you should use query parameter.
Example:
/users # Fetch a list of users
/users?occupation=programmer # Fetch a list of user with filter programmer
/users/123 # Fetch a user who has id 123
you can get side effects. You don’t have to define other URL and other query parameter to achieve basic CRUD functions.You change HTTP method depends on what you want to do.
/users [GET] # Fetch a list of users
/users [POST] # Create new user
/users/123 [PUT] # Update user
/users/123 [DELETE] # remove user
Putting optional parameters in the Template URL will end up getting really messy, So I would recommend to put optional parameter in Query String.
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.
Is it possible to change session scope properties using ognl?
For example, if I have on my session an attribute called PROCESS_CONFIG which is an object with an attribute name, how can one change this attribute name on a JSP?
I've tried the following but it doesn't work:
<s:textfield value="%{#session.PROCESS_CONFIG.name}" id="PROCESSNAME" name="#session.PROCESS_CONFIG.name"/>
When I submit the form and access the session object in my action, through ServletActionContext.getRequest().getSession().getAttribute("PROCESS_CONFIG"), the attribute name has not changed.
EDIT:
The object saved in session as PROCESS_CONFIG, is a very deep complex object (composed by numerous references to other objects, with lists of lists of objects) and on my view I just want to present a very tiny subset of its attributes (including attributes from its composed objects). So, polluting my JSP with all other fields as hidden is impractical! The view in question is a form where one can change the value of those fields and I would like to directly and automatically update the object saved on my struts 2 session, PROCESS_CONFIG, as if PROCESS_CONFIG object was a property of my action. For example, given the previous code snippet, PROCESSNAME is an attribute of PROCESS_CONFIG object and I would like to update it automatically in PROCESS_CONFIG object instead of having an PROCESSNAME property on my action and then having to explicitly do the setting of PROCESSNAME on my
PROCESS_CONFIG object.
The session in S2 is a map where you could put the attributes before you use it with OGNL in the JSP. To have this working around let your action implement the SessionAware and look at the official site for the description and usages, and read How do we access to the session from the FAQ.
To your question: why didn't you get the attribute in JSP. Because you are using S2 and OGNL to get it (via #session reference) and you didn't put the attribute to S2 session. S2 session implementation differs from the standard http session. However, if you set attribute to the standard http session you can still access it in JSP 2.0 manner. The opposite is also true.
Could anyone please clarify the defination of attribute?
for example, in the following code, what is an attribute:
request.setAttribute("ja",new foo.Employee());
Is the attribute in the above code an object of type foo.Employee(), or it is key/value pair, or it is actually "ja"?
Request attributes are values indexed by a key (in your case "ja") which are shared in the life of the request object. In Java filter, servlet, jsp, include and forward use same request object so for example you can push an object in a servlet and pull it in a JSP.
The same approach is for session and application scopes
Request attributes are (or at least act like) a map of objects, in this case the key is "ja" and the value is a new foo.Employee.
The session, page, and application have the same data structure.
From the servlet API specification:
Attributes are objects associated with a request. Attributes may be set by the
container to express information that otherwise could not be expressed via the API,
or may be set by a servlet to communicate information to another servlet (via the
RequestDispatcher). Only one attribute value may be associated with an attribute name.
Here an attribute is a custom piece of information (here a new foo.Employee) added to your request (in a Map,Object> . This information will last as long as this request is processed and it can be used later in the process, for example by a JSP.
It's a key value pair
From the docs:
setAttribute
public void
setAttribute(java.lang.String name,
java.lang.Object o)
Stores an attribute in this request. Attributes are reset between
requests. This method is most often
used in conjunction with
RequestDispatcher.
Attribute names should follow the same conventions as package names.
Names beginning with java., javax.,
and com.sun.*, are reserved for use by
Sun Microsystems.
If the value passed in is null, the effect is the same as calling
removeAttribute(java.lang.String).