我有两个系统,我正在尝试整合.一个是基于原始servlet构建的,新的一个是使用IceFaces构建在JSF上的.我正在尝试促进跨系统登录.我的想法是,我在旧系统中有一个按钮,它将适当的信息发布到新站点并将其记录下来.
好吧,理想情况下,我只想使用一个普通的旧servlet在新网站上使用它.转到新站点的Servlet,执行它需要做的事情并转发到仪表板上.
我们的安全性通过托管bean处理.但是,当你到达Servlet时,没有面部上下文.那么,我将如何创建一个新的面部上下文?
我有一个备份计划,我可以随时链接到一个虚拟的.iface页面,它将为我创建FacesContext,然后创建一个支持bean,当它获得实例化然后转发到主页面时将执行操作.但这感觉非常像黑客.
任何帮助,将不胜感激!
编辑:我采取了备份的方式.基本上,我发布到这样的页面:
支持豆看起来像……
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);
}
}
这仍然感觉像是一个黑客,但我不知道如何解决这个问题.理想情况下,我将POST发送到servlet,获取loginID,构建用户并将其直接放入托管bean.但是,FacesContext在那时并不存在.
还有其他想法吗?
几个笔记:
>托管bean永远不会在定义它们的Web应用程序(WAR)之外可用.
> FacesContext对象实例最终由FacesServlet.service创建和处理,因此请求应该通过此servlet.尝试在其他情况下创建上下文可能会导致未定义的行为.
记住这一点,你可以创建一个这样的请求序列:
FacesServlet (mapping: /faces/*)
-> /faces/jsfPage.jsp (a JSP with JSF controls)
-> DispatchBean (calls ExternalContext.dispatch("/AnotherServlet")
-> AnotherServlet
jsfPage.jsp包含:
“dispatch”属性解析为bean方法“getDispatch”:
public String getDispatch() {
FacesContext context = FacesContext.getCurrentInstance();
try {
context.getExternalContext().dispatch("/FacesClientServlet");
} catch (IOException e) {
throw new FacesException(e);
}
return null;
}
哪个调度到这个servlet:
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,IOException {
FacesContext context = facesContextFactory.getFacesContext(
getServletContext(),request,response,lifecycle);
try {
ELContext elContext = context.getELContext();
ExpressionFactory expressionFactory = context.getApplication()
.getExpressionFactory();
ValueExpression expression = expressionFactory
.createValueExpression(elContext,Object.class);
Object value = expression.getValue(elContext);
PrintWriter pw = response.getWriter();
try {
pw.write("" + value);
} finally {
pw.close();
}
} finally {
context.release();
}
}
}
如果可能的话,我会再次避免使用这种方法.