I have a authorization managed bean to fetch restriction rules to be applied to the tags within every jsf of the application. The managed bean requires to know the name of the requested jsf on initiation so it can fetch restrictions specific to the tags within that jsf. What is the best way this can be achieved ?
Declare it in web.xml as follows:
<context-param>
<param-name>paramName</param-name>
<param-value>PARAM_VALUE</param-value>
</context-param>
Access it in ManagedBean as follows:
FacesContext.getCurrentInstance()
.getExternalContext().getInitParameter("paramName")
Hope this helps to solve your problem.
You can get init-params defined in web.xml by:
FacesContext.getCurrentInstance().getExternalContext()
.getInitParameter("paramName");
Related
I want to add an URL mapping to the filter that I'm adding to the servlet context. It looks like this
servletContext.addFilter("filterName", Filter.class).addMappingForUrlPatterns();
addMappingForUrlPatterns(); requires DispatcherType as a parameter. When we use web.xml type of configuration, we can just add the <url-pattern> tag with the value. And if I understand this configuration correctly, it would require the DispatcherType anyway. What would be the default value in that case?
Is it possible to make auto url mapping in Spring MVC? For example, url http://localhost/eshop/products invokes ProductsController in eshop module without explicit #RequestMapping. System should know that the first param is a module and the second param is a controller. I don't want to do any changes in web.xml or in #RequestMapping each time I create a new controller.
Please check this link
http://static.springsource.org/spring/docs/3.0.0.M3/reference/html/ch16s10.html
Convention over configuration with Spring MVC
I upgraded my liferay to 6.0 and JSF from 1.2 to 2.1. My existing code
((ActionResponse)context.getExternalContext().getResponse()).sendRedirect( redirect );
context.responseComplete();
Here redirect is defined as follows:
String redirect = "/namingportal/group/customercenter/accountSearch";
The above URL is the portlet page to which the request must be redirected.
Started breaking with the error:
java.lang.IllegalStateException: Set render parameter has already been called
at com.liferay.portlet.ActionResponseImpl.sendRedirect(ActionResponseImpl.java:48)
After doing some google, I figured that the above code should be replaced with the following:
Solution 1:
context.getExternalContext().redirect(redirect);
When I tried with Solution 1, its giving me the error FacesFileNotFound /namingportal/group/customercenter/accountSearch.xhtml, its actually looking for the xhtml and not for the portlet.
I also have this in my web.xml:
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
Please let me know if there is any other alternative way to redirect to portlet from JSF managed bean method using Liferay 6.0 and JSF 2.1.
Thanks
The bridge adheres to the JSR 329 Spec requirements for ExternalContext.redirect(String), which provides a standard way to achieve what you want to do.
In most cases, ExternalContext.redirect(String) is assumed by the bridge to be part of a JSF navigation-rule firing. However, there are two ways to make it work in your case:
1) Add the Bridge.DIRECT_LINK parameter to the URL with a value of "true":
String redirect = "/namingportal/group/customercenter/accountSearch?javax.portlet.faces.DirectLink=true";
externalContext.redirect(redirect);
2) Make the URL to be absolute, like this:
PortletRequest portletRequest = externalContext.getRequest();
ThemeDisplay themeDisplay = (ThemeDisplay) portletRequest.getAttribute("THEME_DISPLAY");
String portalURL = themeDisplay.getPortalURL();
String redirect = portalURL + "/namingportal/group/customercenter/accountSearch";
externalContext.redirect(redirect);
HI,
We are declaring the FacesServlet and its URL mapping in the Web.xml. From my understanding,
FacesServlet loaded only once at the server startup.
URL mapping is used only when first time JSP application accessed from the external context.
One of the new learner for JSF has asked me the questions, these two things are used only once by the application. Is it true? Also is there any other way by not including in the web.xml?
What I should answer?
Updated
For example, I am accessing the application using the URL http://localhost:8080/webapp/index.jsf. When we are accessing this URL, FacesServlet invoked and view is rendered. The following is my question:
In JSF, we never seen changing the URL in the address bar. In that case, how it is handling the new request with the same URL?
In faces-config.xml we are giving the navigation cases as follows:
to-view-id>failure.jsp /to-view-id>
Why we need not give the view name as failure.jsf? We are just giving the *.jsp in the faces-config.xml. How it is handled internally?
FacesServlet loaded only once at the server startup.
Correct.
URL mapping is used only when first time JSP application accessed from the external context.
Incorrect. It's been tested on every incoming HttpServletRequest. How else should the container know which servlet to invoke?
Also is there any other way by not including in the web.xml?
If you're using a servletcontainer which supports Servlet 3.0, you can also do this by #WebServlet annotation. JSF 2.0, however, is designed to be backwards compatible with Servlet 2.5, so it doesn't ship with that annotation and you need to explicitly declare it in web.xml.
See also:
Servlets tag info page
Lifecycle of a JSP/Servlet webapplication
How servlets are initialized and used
Update as per the new series of questions (which should each belong in its own question, but ala)
In JSF, we never seen changing the URL in the address bar. In that case, how it is handling the new request with the same URL?
This happens only if under the covers a forward by RequestDispatcher#forward() takes place. In a forward, the servletcontainer basically reuses the same HTTP request/response for a view (JSP/XHTML page). It does not force/instruct the webbrowser to send a brand new request. On the other hand, the HttpServletResponse#sendRedirect() will instruct the client (the webbrowser) to fire a new GET request and thus the URL will change. You can force this in the JSF sice by adding <redirect/> to the <navigation-case>. Note that since this causes a new request, all request scoped beans of the initial request will be lost.
Why we need not give the view name as failure.jsf? We are just giving the *.jsp in the faces-config.xml. How it is handled internally?
The FacesServlet knows its own url-pattern.
Yes. while loading your application container will load web.xml and will extract the data for
particular URL pattern to servlet . when request comes it checks from memory that for this pattern which servlet to invoke and then if servlet is already loaded it will take it from memory otherwise it will create an instance of servlet and it will invoke doGet() or doPost() depending on the request type.
and there is another way to delcare URL mapping as from JAVAEE-6 by annotation
something like
import javax.servlet.annotation.InitParam;
import javax.servlet.annotation.WebServlet;
#WebServlet(
name = "SimpleServlet",
urlPatterns = {"/login"},
initParams = {
#InitParam(name = "param1", value = "value1"),
#InitParam(name = "param2", value = "value2")}
)
public class SimpleServlet {
}
In faces-config.xml we are giving the navigation cases as follows:
<to-view-id>failure.jsp </to-view-id>
Why we need not give the view name as failure.jsf? We are just giving the *.jsp in the faces-config.xml. How it is handled internally?
it is view identifier not the URL FacesServlet will load that view upon invocation of that navigation case.
Is there a way for me to instantiate the Spring MVC DispatcherServlet in code rather put it in the web.xml and have it be instantiated by the web server?
The reason for this is that I want to check a memCache to see if I have already recently rendered the page that is being requested and if so just return from the memCache, rather than going through Spring MVC and the controllers.
The ~2 second instantiation of the DispatcherServlet is significant because I am using Google App Engine and that might end up being an additional 2 seconds the user has to wait for their page.
I have tried
dispatcherServlet = new DispatcherServlet();
dispatcherServlet.init();
dispatcherServlet.service(request, response);
but I get this exception on the init call:
[java] java.lang.NullPointerException
[java] at org.springframework.web.servlet.HttpServletBean$ServletConfigPropertyValues.<init>(HttpServletBean.java:196)
[java] at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:114)
Basically, what I'm looking for is a way to instantiate a servlet in code without having to specify it in web.xml and not have to call
getServletConfig().getServletContext().getServlet("dispatcherServlet");
DispatcherServlet is a servlet, so you should call init(ServletConfig) instead of init() to initialize it.
Unless Google App Engine does something really weird, the DispatcherServlet is only instantiated once, on application startup.
If you want to cache page response as you mention, I would suggest implementing this as a HandlerInterceptor (which you can apply to any URL pattern you like), which gives you hooks to plug in logic in either pre- or post-invocation of your controller.