是否可以从数据库加载jsf 2页面,而不是从xhtml文件加载? 例如,请求来自/faces/foo.xhtml,FacesServet拦截请求,VieHanlder通过从数据库加载foo.xhtml而不是从服务器创建视图foo.xhtml?
由于
答案 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}}。