[英]Open JSF page from a Servlet with login information
根据BalusC指南,我尝试创建一个Login系统,以便通过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在他的示例中显示的相同。
先感谢您
问候
您不应将servlet视为JSF支持bean。 JSF是一个基于Servlet API的MVC框架。 如果出于某种原因,用完全有价值的JSF支持bean替换servlet是不可行的,则应该使用FacesContext
JSF所做的“纯” Servlet API完全相同。
例如,
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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.