2013-12-15 21 views
0

我如何映射URL localhost/testproject/testfolder/all/profile.xhtmllocalhost/testproject/profile.xhtml?我不想顯示後面的文件夾的整個結構。我與URL模式,但不起作用。任何人都可以給我一個提示,這可以在JSF 2.2中工作嗎?JSF地圖顯示從本地主機/測試項目/ testfolder /所有/ profile.xhtml URL到本地主機/測試項目/ profile.xhtml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.xom/xml/ns/javaee/web-app_3_0.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="WebApp_ID" version="3.0"> 
    <description>Testprojekt</description> 
    <servlet> 
    <servlet-name>Faces Servlet</servlet-name> 
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>Faces Servlet</servlet-name> 
    <url-pattern>*.xhtml</url-pattern> 
    </servlet-mapping> 
    <context-param> 
    <param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name> 
    <param-value>true</param-value> 
    </context-param> 
    <context-param> 
    <param-name>javax.faces.PROJECT_STAGE</param-name> 
    <param-value>Development</param-value> 
    </context-param> 
    <context-param> 
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name> 
    <param-value>true</param-value> 
    </context-param> 
    <listener> 
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class> 
    </listener> 
    <welcome-file-list> 
    <welcome-file>faces/users/welcome.xhtml</welcome-file> 
    </welcome-file-list> 
</web-app> 

回答

0

你可以使用過濾器(URL重寫)爲你做。

有很多URL重寫圖書館在線或

一些東西一樣

public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain filerChain) throws IOException, ServletException { 
     HttpServletRequest httrequest = (HttpServletRequest) request; 
     String url = httrequest.getRequestURI().trim(); 
     HttpSession session = httrequest.getSession(true); 
     //be aware of infinite loops. in case your url is only profile.xhtml and the redirect contains this file too, so i added the pattern faces before it 
     if (url.contains("faces/profile.xhtml")) { // this what pub User see in the url 
      StringBuilder forward = new StringBuilder();      
      forward.append("/testfolder/all/profile.xhtml?"); 
      forward.append(httrequest.getQueryString()); 
      request.getRequestDispatcher(forward.toString()).forward(request, response); 


     } 

       if(!response.isCommitted()){ 
      try{ 
       filerChain.doFilter(request, response); 
      }catch(Exception ex){ 
       ... 
      } 
     } 
0

你需要URL重寫,我使用prettyfaces作爲我的URL重寫庫您實現自己的過濾器。 你可以甚至友好的URL

prettyfaces

你只需將它添加到您的pom.xml

<!-- for JSF 2.x --> 
<dependency> 
    <groupId>org.ocpsoft.rewrite</groupId> 
    <artifactId>rewrite-servlet</artifactId> 
    <version>2.0.8.Final</version> 
</dependency> 
<dependency> 
    <groupId>org.ocpsoft.rewrite</groupId> 
    <artifactId>rewrite-config-prettyfaces</artifactId> 
    <version>2.0.8.Final</version> 
</dependency> 

然後在你的情況下創建pretty-config.xml/WEB-INF/文件夾

內,這裏是一個例子pretty-config.xml

<pretty-config xmlns="http://ocpsoft.org/schema/rewrite-config-prettyfaces" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://ocpsoft.org/schema/rewrite-config-prettyfaces 
        http://ocpsoft.org/xml/ns/prettyfaces/rewrite-config-prettyfaces.xsd"> 

    <url-mapping id="profile"> 
     <pattern value="/profile" /> 
     <view-id value="/testfolder/all/profile.xhtml" /> 
    </url-mapping> 

</pretty-config>