It is posible for a portlet to read a request parameter of its surrounding page?
E.g. the URL of the page the portlet resides in is http://example.com/mypage?foo=bar
Is it possible to read the "foo" parameter from a portlet that is on that page?
Portlet Container is Liferay 5.2.5.
Yes this can be achieved with something like this -
HttpServletRequest convertReq = PortalUtil.getHttpServletRequest(request);
HttpServletRequest originalReq = PortalUtil.getOriginalServletRequest(convertReq);
String productId = originalReq.getParameter("foo");
Where request is RenderRequest.
PortletRequest class has method getAttribute()
You can treat it like HttpServletRequest.
I haven't yet found a way besides using platform specific class com.liferay.portal.util.PortalUtil.
Related
I have a portlet developed in Liferay, in which I want to get query parameter value from URL.
I tried this way but get "null" value from Query parameter:
HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(request);
HttpServletRequest httpOrigReq = PortalUtil.getOriginalServletRequest(httpReq);
String myValue = httpOrigReq.getParameter("idProcessOrigin");
Any advice would be greatly appreciated!
The code you mention in your question should work, however, it's ignoring the peculiarities of a portlet environment. Typically, in a portlet, you'd rather "decorate" the names of parameters with <portlet:namespace/> (or whatever the equivalent in your UI library of choice is to this JSP tag). Instead of submitting a parameter "idProcessOrigin", you'd submit "<portlet:namespace/>idProcessOrigin" (of course, with properly replaced namespace, e.g. rather SOME_RANDOM_STUFF_idProcessOrigin)
If you don't want this, you can also declare the property com.liferay.portlet.requires-namespaced-parameters=<boolean> in your portlet-#Component's property list (as carried over from liferay-portlet.xml)
For the standard way of obtaining the parameters from a portlet request, you don't need to go through the HttpServletRequest at all - just use the PortletRequest's getParameter method. The result of this method depends, however, on properly decorated parameter names (or the deactivated option mentioned above). Note: When you call request.getParameter("idProcessOrigin"), you don't need the decoration any more, provided that request is a PortletRequest, not an HttpServletRequest.
The goal is to create a URL to the Portlet with this code:
ExternalContext ctx = FacesContext.getCurrentInstance().getExternalContext();
RenderResponse response = (RenderResponse)ctx.getResponse();
PortletURL portletUrl = response.createRenderURL();
String url = portletUrl .toString();
But if I call this in a backing bean's JSF-actionListener method, I get a ClassCastException because ctx.getResponse() gives me an javax.portlet.ActionResponse instead.
I know that a RenderResponse is accessible from the doView method in the Portlet class. But how can I access it in my backing bean?
I'd like to put link into an email that leads the user to the portlet. I'm using WebSphere Portal 6.1.
The render URL is not normally available at that point in the portlet lifecycle.
Consider using the URL mapping features
Consider using a servlet with the URL engine service to perform a redirect
In both these cases, you don't need the huge encoded URL usually generated by Portal; you use something like http://host/foo/bar as your entry point.
I've used the second approach in production. Unique names are added to the target pages and portlet instances for easy lookup. These are added to the page configuration via XMLAccess scripts - they are not available via the admin user interface in version 6.1.
I use the following approach now, which causes some (very little) workload overhead, but works well:
Grab the RenderResponse in the doView method
Use PortletURL portletUrl = response.createRenderURL(); and store this object in the portlet session (with every request, I know).
In your action listener method, retrieve the PortletURL object, append neccessary parameters and render the URL for whatever.
Is there any way to add parameters in a servlet or jsp? Because as far as I have searched, parameters can only be set in forms.
Why is there no command in java to add parameters manually?
Another doubt I have is that, when forwarding from one jsp page to other, is the input request(with the parameters, attributes etc) forwarded to the next jsp page?
What if I don't want certain parameters or attributes to be forwarded?
Is there any way to add parameters in a servlet or jsp?
-- Append it to the URL. If you are thinking of some hypothetical function such asrequest.setParameter, then there is no such method provided. Don't you think it would be a security breach then?
Request parameters can be passed by using <jsp: param>
This tag contains two attributes:
name
value.
eg:
<jsp: param name="myParam" value="Amar Patel"/>
This tag is used as a nested tag within <jsp:forward> or <jsp:include> blocks.
For example:
<jsp: forward page="ssParameters.jsp">
<jsp: param name="myParam" value="Amar Patel"/>
<jsp: param name="Age" value="15"/>
</jsp: forward>
Is there any way to add parameters in a servlet or jsp?
Yes. If it's possible to use the RequestDispatcher.forward(ServletRequest, ServletResponse) or RequestDispatcher.include(ServletRequest, ServletResponse) you can use the the HttpServletRequestWrapper to add or filter Request Parameters
// assuming this code is part of a Servlet or JSP
HttpServletRequest request = ...;
final Map<String,String> additionalParameters = ...;
RequestDispatcher dispatcher = this.getServletConfig().getServletContext().getRequestDispatcher("/");
dispatcher.forward(new HttpServletRequestWrapper(request){
public String getRequestParameter(String parameterName) {
if (additionalParameters.contains(parameterName)) {
return additionalParameters.get(parameterName);
} else {
if (!"filteredParameter".equals(parameterName)) {
return super.getParameterMap().get(parameterName());
} else {
return null;
}
}
}
}
, response);
If you only want to pass additional "parameters" to the forwarded/included page / servlet
it's recomended to use the ServletRequest.setAttribute(String, Object). ServletRequest Attributes may be added / removed during processing the request and allow to add complete java Objects rather than Strings when using Request Parameters.
Why is there no command in java to add parameters manually?
The ServletRequest Request Parameters should usually be treated as unmodifiable as it is Warpper of the Request sent from the Client to server. If you want to add Parameters use the Request Attributes.
Another doubt I have is that, when forwarding from one jsp page to
other, is the input request(with the parameters, attributes etc)
forwarded along to the next jsp page?
Most of the time the original HttpServletRequest is passed to the forwarded page / servlet. But as shown in my snipplet it's also possible to pass a different ServletRequest to the forwarded / included servlet / jsp.
What if I don't want certain parameters or attributes to be forwarded?
See code snippet above. You can filter the forwarded parameters or attributes using your own HttpServletRequestWrapper
You can pass your parameters by using it in your url. Regarding your questions about request. If you do forward to another jsp/servlet, all your parameters and attributes will be in the request. If you don't want to see it there, then you should use redirect instead (response.sendRedirect(url))
is their a neat way to pass a model to jsp, render the jsp and return the html as string using Spring. The html is then used in an e-mail that is fired off programmitcally, I do not want to use freemarker, but maybe I should ?
The url being requested is part of the same app.
I want one of my service layer classes to be able to call a view and use the html as a String.
You can call requestDispatcher.include(request, response) method.
You will need to implement the request and response objects. The request object will provide all information to the dispatcher which page should be rendered, the response object you will pass to the call will then capture the result to a string (using e.g. a StringBuilder).
See e.g. this tutorial for more info.
I'm guessing a servlet filter will do the trick? Not really a Spring solution, but easy enough to do.
Also this answer seems relevant, although it is DWR that you may not necessarily want to use in this instance.
You can use Velocity to create an email template:
String text = VelocityEngineUtils.mergeTemplateIntoString(
velocityEngine, "emailTemplate.vm", model);
There is a complete chapter in the Spring reference docs of how Spring can be used to send emails of various types.
Is posible for a portlet to read a request parameter of its surrounding page?
E.g. the URL of the page the portlet resides in is http://example.com/mygroup/mypage?foo=bar Is it possible to read the "foo" parameter from a portlet that is on that page?
Portlet Container is Liferay 6.0.5.
P.S.
I have already tried:
com.liferay.portal.util.PortalUtil.getOriginalServletRequest(com.liferay.portal.util.PortalUtil.getHttpServletRequest((javax.portlet.PortletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest())).getParameter("foo")
but I always get null for productId
Thanks!
Have you tried
ExternalContext.getRequestParameterMap()
The following code will do the trick:
javax.portlet.PortletRequest pr = (javax.portlet.PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("javax.portlet.request");
java.lang.reflect.Method method = pr.getClass().getMethod("getOriginalHttpServletRequest");
HttpServletRequest httpServletRequest = (HttpServletRequest)method.invoke(pr, new Object[] {});
return httpServletRequest.getParameter(YOUR_PARAM_KEY);
In a partial submit, the icefaces ajax bridge (which replaces a usual jsf portlet bridge) is avoiding the normal portal action/render request, contacting directly the blocking servlet (in order to avoid invalidating other request-scoped portlets and in general, to be faster). Because of this, all those params/attributes which are set in a normal portal request are not set in ajax. They are set only in the initial GET type request for that page. So, actually what you should do is saving those params in the #PostConstruct or some other method of your controlling bean, and then reuse them later. (They wouldn't change in a partial submit anyway, right?).
Keep in mind though, that this will not work if you use IceFaces in conjuction with Spring (and their EL Resolver, since that eliminates your extended request scope).
if you are in JSF environment then try this:
String param = LiferayFacesContext.getInstance().getRequestQueryStringParameter("foo");