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).
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.
I've a problem with a GET operation in a REST WS. We have a Front-end panel with several filters for searching customers. The panel contains these filters:
Customer ID (Customer property)
Customer Name (Customer property)
Account number (Account property)
License plate (Vehicle property)
...
In the domain model we have 3 entities:
Customer
Account (A customer could have 1 or more accounts)
Vehicle (An account could have 1 or more vehicles)
How can I implement REST GET operation for this seach?
GET ..../customers/?name={name}&accountNum={accountNumber}&licensePlate={licensePlate} ?????
I think it is wrong because accountNumber and licensePlate don´t belong to customer resource. I don´t need these properties in the result expected.
I think about create new resource like customerFilter but It is no sense if I have to return a customer resource.
Any idea?
Thank you!
It will not pretend to be a specific answer for your question. But I think it will clarify something related to GET method.
According with URI Specification - RFC 3986 and Http Specification - RFC 7230, there are 3 kinds of ways to send data from client to server: via query, via path or via message-body.
When you are using GET method, it is not recommended to use message-body, because GET method can be cached for improving performance stuffs and these caches could ignore the message-body or reject the request:
A payload within a GET request message has no defined semantics;
sending a payload body on a GET request might cause some existing
implementations to reject the request.
So, you can choose now query or path. Both are in URL in this format:
http://example.com/{path1}/{path2}?query1=value1&query2=value2
What are the differences between these? according with RFC 3986 - Path and RFC 3986 - Query:
The path component contains data, usually organized in hierarchical
form, that, along with data in the non-hierarchical query component
(Section 3.4), serves to identify a resource within the scope of the
URI's scheme and naming authority (if any).
The query component contains non-hierarchical data that, along with
data in the path component (Section 3.3), serves to identify a
resource within the scope of the URI's scheme and naming authority
(if any).
As conclusion, you can design whatever you want. You can use for example:
GET .../customers?name={name}&accountNum={accountNumber}&licensePlate={licensePlate}
GET .../customers/{customerId}/
GET .../customers?customerId=12345
I don't see any issue with the url you have but your concern is also valid.
There is something that we need to take into consideration in this scenario. With this search what you expect to get as the response of the API call?
If you want response should contain information about customer only, when the search condition gets satisfied then /customer is right.
If you want some generic response consisting of Customer, Account and Vehicle info, you can have some generic terminology instead of customer in the url.
I hope this will help you.
Thanks :)
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..
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.
I am working on a project which made use of an old (but nice) framework Struts 1.3, and I am trying to accomplish a simple task.
I have a bean containing a property, which is a key corresponding to a property file. I would like to use it for recall the appropriate translation.
Variable in property file
props.myprop.sample=This is my sample property
The property is in a bean passed to the jsp called for convenience AllProps which has a getter for the property, and this should be a pseudo code:
<bean:define id="sample" name="AllProps" property="sample" type="java.lang.String"/> // should result in sample = props.myprop.sample
<div><bean:message key="sample"/></div>
Which should output:
<div>This is my sample property</div>
But obviously result in a property not found, can you give me help on how to deal with this ?
I would like to stick as much as possible to Struts tag, then Jsp tag, and scriptlet as last resource.
Thanks
Straight from the documentation:
<bean:message>
Render an internationalized message string to the response.
Retrieves an internationalized message for the specified locale, using
the specified message key, and write it to the output stream. Up to
five parametric replacements (such as "{0}") may be specified.
The message key may be specified directly, using the key attribute, or
indirectly, using the name and property attributes to obtain it from a
bean.
(emphasis mine)
So, all you need is
<bean:message name="AllProps" property="sample"/>