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.
Related
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.
This is the scenario
This is the JSP from where I get to the servlet. There is an entity called CurrentUser wich holds multiple fields of information about the user. I want to pass the servlet all the information about the user in a form of entity.
Information about the current user, like: name, id, profile picture etc...
Inside the servlet:
if(request.getParameter("action").equals("profile")){
CurrentUser _currentUser= (CurrentUser) request.getParameter("entity");
}
If I do a system.out to see whats inside "entity" parameter i get something like: Package.CurrentUser#abc1235 ... and I cannot cast that, to receive it as an Entity.
incompatible types: String cannot be converted to CurrentUser
Is there any way to get that refference and the info inside those fields?
Inside the JSP, store your _currentUser entity to the Session as
<% session.setAttribute("entity", _currentUser); %>
Information ...
Now, inside the Controller Servlet you can retrieve this entity as
if(request.getParameter("action").equals("profile")){
CurrentUser _currentUser= (CurrentUser) session.getAttribute("entity");
}
Note, that storing the current user object into Session should ideally be done by the Servlet that forwarded the request to the JSP first. Use of Java code scriptlets <% %> inside a JSP is deprecated as JSPs should mostly be used for presentation purposes only.
No, you shouldn't do it. Using the reference string representation you can't retrieve object instance. But you can use String object's ID parameter in the query string. Then retrieve the object by ID.
Information about the current user, like: name, id, profile picture etc...
String id = request.getParameter("entity");
I always confuse / forget how to use Expression Language in a JSP file.
Can you give some examples?
What implicit object does EL come with?
What implicit object does EL come with?
all attributes set in request, page context, session, servlet context are impliticly available
plus request parameter map available via ${param.paramName}
I will edit my answer and add some better explanations:
There are 11 implicit objects. 10 of them are simply Maps and of those 10, 4 of them are sessions:
pageScope
requestScope
sessionScope
applicationScope
Then there comes parameters:
param
paramValues
These provide access to the request parameters. The param variable is a Map and contains only the first value from any parameter with multiple values.(This is similar to getParameter from ServletRequest). Map paramValues contains all the values of every parameter.(This is similar to getParameterValues from ServletRequest).
Ok then comes:
header
headerValues
2 more maps are:
initParams
cookie
initParam is a Map containing all the context init paramters from the ServletContext instance for this application.
cookie on the other hand is a Map containing all the cookies that the user's browser sent along with the request.
So there is 1 more implicit object left, which is not a Map:
pageContext
There you go..
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);
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).