我正在尝试实现一个通用"视图,其中(部分)显示的内容取决于URL.例如
I am trying to implement a "generic" view where (part of) the content displayed depends on the URL. E.g.
如果/somepath/somepage.xhtml指向不存在的文件,而不是直接出现404错误,我想尝试使用通用视图/genericview.xhtml从数据库中检索/somepath/somepage.xhtml的内容,在该视图中我有一些东西像:
If /somepath/somepage.xhtml points to a non existing file, instead of going straight to a 404 error I want to try to retrieve /somepath/somepage.xhtml's content from the database using a generic view, /genericview.xhtml, where I have something like:
<h:outputText value="#{genericViewBean.content_lg}" escape="false" />(如果由后备bean找到),它将根据最初请求的viewId从tgenericcontent表中输出数据库条目的内容:
which, if found by the backing bean, would output the content of the database entry from a tgenericcontent table, depending on the originally requested viewId:
webpath | content /somepath/somepage.xhtml | <p>This is a test</p> /someotherpath/someotherpage.xhtml | <p>A different test</p>如果在该表中未找到视图内容,则将返回标准404错误.
If the view content is not found in that table then the standard 404 error would be returned.
我最接近的浪费是克隆/genericview.xhtml,仅更改文件路径(例如,更改为/somepath/somepage.xhtml).但这给我每个视图一个文件的精确副本,这非常混乱,而且它不允许我仅通过向数据库添加一个条目来创建新的URL.
The closest I got wast to clone /genericview.xhtml changing only the file path (for example, to /somepath/somepage.xhtml). But that gets me one exact copy of the file per view, it is quite messy, and it doesn't allow me to create a new url just by adding an entry to my database.
如何在不克隆/genericview.xhtml的情况下获得相同的结果?
How can I get the same result without cloning /genericview.xhtml?
(PS:我已经读过有关prettyfaces的内容,但是没有更简单的解决方案吗?)
(P.S: I have read about prettyfaces, but isn't there a simpler solution?)
推荐答案为此,通常 servlet过滤器正在被使用. PrettyFaces , UrlRewriteFilter 和 FacesViews 也是这样做的.
For that, normally a servlet filter is being used. PrettyFaces, UrlRewriteFilter and FacesViews also do it that way.
您可以通过 HttpServletRequest#getRequestURI() .您可以通过 ServletContext#getResource() ,它将在不存在的资源上返回null.如果资源存在,则只需通过 FilterChain#doFilter() ,否则通过 RequestDispatcher#forward() .
You can get the request URI by HttpServletRequest#getRequestURI(). You can check the existence of a web resource by ServletContext#getResource() which will return null on non-existent resources. If the resource exists, just continue the request by FilterChain#doFilter(), else forward the request to the generic view by RequestDispatcher#forward().
总而言之,这就是过滤器的外观:
All in all, this is how the filter could look like:
@WebFilter("/*") public class GenericViewFilter implements Filter { @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; String relativeRequestURI = request.getRequestURI().substring(request.getContextPath().length()); boolean resourceExists = request.getServletContext().getResource(relativeRequestURI) != null; boolean facesResourceRequest = request.getRequestURI().startsWith(request.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)); if (resourceExists || facesResourceRequest) { chain.doFilter(request, response); } else { request.getRequestDispatcher("/genericview.xhtml").forward(request, response); } } // ... }在/genericview.xhtml中,原始请求URI可用作为以 RequestDispatcher#FORWARD_REQUEST_URI .您可以在与视图关联的支持bean的@PostConstruct中使用它,以便从数据库中提取正确的内容.
In the /genericview.xhtml, the original request URI is available as request attribute keyed with RequestDispatcher#FORWARD_REQUEST_URI. You could use it in @PostConstruct of backing bean associated with the view in order to pull the right content from the DB.
String originalRequestURI = (String) externalContext.getRequestMap().get(RequestDispatcher.FORWARD_REQUEST_URI); // ...更多推荐
有没有一种简单的方法可以在JSF中实现路由?
发布评论