Manually creating a Faces context

I have two systems that I'm trying to integrate. One built on raw servlets, the new built on JSF with IceFaces. I am trying to lighten the cross system sign. The idea is that I have a button in the old system that sends the relevant information to the new site and registers them.

Well, ideally, I would like to use only the regular old servlet to facilitate this on the new site. Go to the new Servlet website, do what it needs and go to the toolbar.

Our security is handled through a managed bean. However, by the time you get to the servlet, there is no context for the faces. So how do I create a new faces interface?

I have a backup plan in which I can always link to the .iface dummy page, which will create a FacesContext for me, and then create a backup bean that will do something when it gets instanciated, and then go to the main page. But this is very similar to a hack.

Any help would be appreciated!

EDIT: I went back. Basically, I am sending a message to this page:

<f:view>
   <ice:outputText value="#{EntryPoint}"/>
</f:view

Bean support looks like this ...

public EntryPoint() {
      try {
         HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
         HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse(); 
         String loginID = request.getParameter("loginID");
         //Do some code to load the user/permissions
         response.sendRedirect(
            //The appropriate page
         );
      } catch (IOException ex) {
         logger.error(null, ex);
      } catch (SQLException ex) {
         logger.error(null, ex);
      }
   }

It still looks like a hack, but I'm not sure how to get around this. Ideally, I would POST to the servlet, get the loginID, create a user and put it directly in a managed bean. But in this case, FacesContext does not exist.

Any other ideas?

+5
source share
1

, "" .

:


, :

FacesServlet (mapping: /faces/*)
 -> /faces/jsfPage.jsp (a JSP with JSF controls)
    -> DispatchBean (calls ExternalContext.dispatch("/AnotherServlet")
       -> AnotherServlet

jsfPage.jsp :

<f:view>
    <h:outputText value="#{dispatchBean.dispatch}" />
</f:view>

"" bean "getDispatch":

public String getDispatch() {
    FacesContext context = FacesContext.getCurrentInstance();
    try {
        context.getExternalContext().dispatch("/FacesClientServlet");
    } catch (IOException e) {
        throw new FacesException(e);
    }
    return null;
}

:

public class FacesClientServlet extends javax.servlet.http.HttpServlet
        implements javax.servlet.Servlet {

    static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        FacesContext context = FacesContext.getCurrentInstance();
        ELContext elContext = context.getELContext();
        ExpressionFactory expressionFactory = context.getApplication()
                .getExpressionFactory();
        ValueExpression expression = expressionFactory.createValueExpression(
                elContext, "#{myBean.text}", Object.class);
        Object value = expression.getValue(elContext);

        ResponseWriter writer = context.getResponseWriter();
        writer.write("" + value);

    }

}

bean "myBean":

public class MyBean {

    private final String text = "Hello, World!";

    public String getText() {
        return text;
    }

}

, .


, , :

public class ContextServlet extends javax.servlet.http.HttpServlet implements
        javax.servlet.Servlet {
    static final long serialVersionUID = 1L;

    private FacesContextFactory facesContextFactory;
    private Lifecycle lifecycle;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);

        LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder
                .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
        facesContextFactory = (FacesContextFactory) FactoryFinder
                .getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
        lifecycle = lifecycleFactory
                .getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
    }

    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        FacesContext context = facesContextFactory.getFacesContext(
                getServletContext(), request, response, lifecycle);
        try {
            ELContext elContext = context.getELContext();
            ExpressionFactory expressionFactory = context.getApplication()
                    .getExpressionFactory();
            ValueExpression expression = expressionFactory
                    .createValueExpression(elContext, "#{myBean.text}",
                            Object.class);
            Object value = expression.getValue(elContext);

            PrintWriter pw = response.getWriter();
            try {
                pw.write("" + value);
            } finally {
                pw.close();
            }
        } finally {
            context.release();
        }
    }

}

, , .

+4

All Articles