1

按照 BalusC 指南,我尝试创建一个登录系统,以便通过 Servlet 访问我的 JSF 页面,但它不起作用:(

我的登录 Servlet 是:

public class DoLogin  extends HttpServlet {

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


    creaLogin(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) {
    creaLogin(request, response);
}

//This method call the FacesContext and pass login nformation
private void creaLogin(HttpServletRequest request, HttpServletResponse response){
    FacesContext facesContext = FaceUtil.getFacesContext(request, response);

    //Retrieve login information from get/post callback
    String userid=request.getParameter("userid");
    String password=request.getParameter("password");

    //I create an istance of LoginBean backingbean that manage login information for my JSF page
    LoginBean mioBean = (LoginBean) facesContext.getApplication().evaluateExpressionGet(facesContext, 
            "#{loginBean}", LoginBean.class);
    mioBean.setUsername(userid);
    mioBean.setPassword(password);
    mioBean.externalLogin();   //this method perform all needed operation and intialize all backing beans of my app

    //Call ma xhtml page 
    ConfigurableNavigationHandler nav 
       = (ConfigurableNavigationHandler) 
               facesContext.getApplication().getNavigationHandler();

    nav.performNavigation("pannello");

}

}

FaceUtil 与 BalusC 在他的示例中显示的相同。

先感谢您

问候

4

1 回答 1

0

您不应将 servlet 视为 JSF 支持 bean。JSF 是一个建立在 Servlet API 之上的 MVC 框架。如果由于某种原因不能用完全值得的 JSF 支持 bean 替换 servlet,那么您应该使用“普通”Servlet API 来做与 JSF 在FacesContext.

例如,

LoginBean loginBean = new LoginBean();
request.getSession().setAttribute("loginBean", loginBean); // Puts bean in session scope.
loginBean.setUsername(userid);
loginBean.setPassword(password);
loginBean.externalLogin(); // Note: if this has CDI/EJB dependencies, just refactor and inject them in servlet instead.

response.sendRedirect(request.getContextPath() + "/pannello.xhtml"); // Assuming FacesServlet is mapped on *.xhtml.
于 2013-05-27T13:50:24.333 回答