3

I'm working on a JSF (v1.2) application. In my application I need a generic servlet which could serve any resource (PDF, Images, Excel, etc). My idea is to ask the caller to send the required information so that I can find out the correct delegator class using some configurations.

This delegator class will take care of serving the correct resource.

For example this is the request url

http://example.com/servlet?delegatorid=abcd

My Servlet code is something like this.

protected void doGet(HttpServletRequest request, HttpServletResponse response){
 String delegatorID=request.getParameter("delegatorid");
//Get the configuration from Configuration table
configuration=getConfiguration(delegatorID);
//invoke the method of the delegator class based on this configuration
Object result=invokeMethod(configuration);
//write the response to the stream
}

My question is what is the best way to do this in a JSF project?

  1. Should I completely avoid JSF dependency in this operation? I can find the delegator method and class and invoke it using reflection. Will there be any potential restrictions in future if I avoid JSF dependency. [One problem which I can think about is, in one of the code, I need to get the user information from session. I'm doing this through FacesContext. Since FacesContext is not available, it will fail, I should have another option to get the session.
  2. If I have to introduce JSF dependency, how do I get the FacesContext here? As far as I know, only the beans that are stored in application scope can be accessed here. I don't want to do that. Is there any other way of getting it?
  3. Instead of using a servlet, can I do this by invoking a ManagedBean method directly using the URL? This will give me FacesContext. I think I need to have a dummy JSP page for the managed bean method to get invoked.

Could you please let me your thoughts on this?

4

1 回答 1

7

( FacesContextand ExternalContext) 只是 , , , , etcetara 的一个外观,以及一些在普通的 vanilla servlet 中根本不需要的 JSF 细节。无非是 的抽象映射。HttpServletRequestHttpServletResponseHttpSessionServletContextExternalContext#getSessionMap()HttpSession#get/setAttribute()

在一个普通的 servlet 中,会话只能由request.getSession()应用程序以getServletContext()通常的方式使用。另请参阅此相关问题:Get JSF managed bean by name in any Servlet related class

您也可以将需要由 JSF 和 Servlet 共享的代码重构为一个实用方法,该方法不依赖于javax.faces.*norjavax.servlet.*类(或最多仅依赖于类javax.servlet.*),最后让调用者各自传递必要的信息。

于 2012-12-24T11:27:30.093 回答