处理在JSF中命中部分URL

时间:2012-05-09 13:09:29

标签: java jsf prettyfaces

我正在使用JSF 2.0.5,Tomcat 7.0.12和prettyfaces。

我的web.xml文件是:

<welcome-file-list>
    <welcome-file>/index.jsp</welcome-file>
</welcome-file-list> 

我漂亮的文件配置:

<url-mapping>
    <pattern>/</pattern>
    <view-id>/jsp/index.jsf</view-id>
</url-mapping> 

我的index.jsp文件为空。

我的问题是,当我在网址'foo.com'中写道时,他最终会重定向(301) http://www.foo.com/jsp/index.jsf

表示在我的阶段监听器中:

HttpServletRequest req = (HttpServletRequest)context.getExternalContext().getRequest(); 
StringBuffer requestUrl = req.getRequestURL(); // i get http://www.foo.com/jsp/index.jsf

有人可以解释他为什么要点击jsp / index.jsf吗?

因为我想知道有人点击foo.comwww.foo.com

2 个答案:

答案 0 :(得分:1)

如果有人使用您的配置访问http://foo.com/http://www.foo.com/,则PrettyFaces会拦截该请求并在内部将其转发到/jsp/index.jsf。您的PhaseListener只会看到此转发的请求,因此只会看到/jsp/index.jsf

如果您想获取原始网址,可以使用PrettyContext.getCurrentInstance().getRequestURL().toURL()

但是,在您的情况下,有一点奇怪的是您在访问根路径时写下用户被重定向(301)。这可能会导致我上面的建议无法正常工作,因为在重定向后,您会丢失原始请求中的所有信息。

也许您可以尝试从welcome-file中删除web.xml条目,并确保您的pretty-config.xml中没有可能导致此重定向的任何其他重写规则

答案 1 :(得分:0)

“/”模式表示应用程序的上下文根,在您的情况下将表示www.foo.com。由于您有从“/”到/jsp/index.jsf的映射,Servlet容器会将您带到那里。