JSF Map显示从localhost / testproject / testfolder / all / profile.xhtml到localhost / testproject / profile.xhtml的URL

时间:2013-12-15 18:55:08

标签: jsf url web.xml jsf-2.2

如何将网址localhost/testproject/testfolder/all/profile.xhtml映射到localhost/testproject/profile.xhtml?我不想显示后面的文件夹的整个结构。我用URL-Pattern但它不起作用。任何人都可以给我一个提示,这在JSF 2.2中是如何工作的?

<?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>

2 个答案:

答案 0 :(得分:0)

您可以使用过滤器(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){
                ...
            }
        }

答案 1 :(得分:0)

你需要重写URL,我使用prettyfaces作为我的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>