从数据库服务JSF

时间:2010-08-20 07:24:44

标签: database jsf jsf-2

是否可以从数据库加载jsf 2页面,而不是从xhtml文件加载? 例如,请求来自/faces/foo.xhtml,FacesServet拦截请求,VieHanlder通过从数据库加载foo.xhtml而不是从服务器创建视图foo.xhtml?

由于

1 个答案:

答案 0 :(得分:0)

理论上,如果你把它从数据库放到公共webcontent中,那就是FacesServlet期望它出现的地方。Filter适合这项工作。public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; String rootPath = req.getSession().getServletContext().getRealPath("/"); String fileName = req.getServletPath().substring(1); File file = new File(rootPath, fileName); if (!file.exists()) { InputStream input = null; OutputStream output = null; try { input = yourDAO.find(fileName); output = response.getOutputStream(); byte[] buffer = new byte[10240]; for (int length = 0; (length = input.read(buffer)) > 0;) { output.write(buffer, 0, length); } } finally { if (output != null) try { output.close(); } catch (IOException ignore) {} if (input != null) try { input.close(); } catch (IOException ignore) {} } } chain.doFilter(request, response); } 适用于这项工作。

这是一个启动示例:

<servlet-name>

将其映射到FacesServlet的{​​{1}}。