2014-09-29 48 views
0

我怎樣才能將xhtml文件放在WEB-INF中,這樣只有應用程序才能直接訪問它們?具體而言,以便鳥頁不能直接公開訪問。該項目來自apress example如何將xhtml從/ web /移動到/ web/WEB-INF /?

項目樹:

NetBeansProjects/Birds/ 
├── build.xml 
├── nbproject 
│   ├── ant-deploy.xml 
│   ├── build-impl.xml 
│   ├── genfiles.properties 
│   ├── private 
│   │   └── private.properties 
│   ├── project.properties 
│   └── project.xml 
├── src 
│   ├── conf 
│   │   └── MANIFEST.MF 
│   └── java 
│    └── dur 
│     └── Hello.java 
└── web 
    ├── eagle.xhtml 
    ├── faces 
    ├── falcon.xhtml 
    ├── index.xhtml 
    ├── menu.xhtml 
    ├── parrot.xhtml 
    ├── resources 
    │   └── css 
    │    ├── cssLayout.css 
    │    └── default.css 
    ├── template.xhtml 
    └── WEB-INF 
     └── web.xml 

的web.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> 
    <context-param> 
     <param-name>javax.faces.DEFAULT_SUFFIX</param-name> 
     <param-value>.xhtml</param-value> 
    </context-param> 
    <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> 
    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
    <welcome-file-list> 
     <welcome-file>index.xhtml</welcome-file> 
    </welcome-file-list> 
</web-app> 

的index.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:ui="http://java.sun.com/jsf/facelets" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core"> 

    <body> 
     This and everything before will be ignored 
     <ui:composition template="template.xhtml"> 
      <ui:define name="navigation"> 
       <ui:include src="menu.xhtml"/> 
      </ui:define> 
     </ui:composition> 
     This and everything after will be ignored 
    </body> 
</html> 

名爲menu.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:ui="http://java.sun.com/jsf/facelets" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core"> 
    <body> 
     This and everything before will be ignored 
     <ui:composition> 
      <h3>Contents table</h3> 
      <hr/> 
      <h:panelGrid columns="1"> 
       <h:commandLink value="Home" action="home" /> 
       <h:commandLink value="Parrot" 
           action="parrot" /> 
       <h:commandLink value="Eagle" 
           action="eagle" /> 
       <h:commandLink value="Falcon" 
           action="falcon" /> 
      </h:panelGrid> 
     </ui:composition> 
     This and everything after will be ignored 
    </body> 
</html> 

所引用:

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:ui="http://java.sun.com/jsf/facelets" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core"> 
    <head> 
     <title>The Happy Birds Directory</title> 
     <style type="text/css"> 
      <!-- 
      .box { 
       float: right; 
       width: 50%; 
       border: black dotted 1px; 
       padding: 5px 
      } 
      --> 
     </style> 
    </head> 
    <body> 
     <h:form> 
      <h1>The Happy Birds Directory</h1> 
      <div class="box"> 
       <ui:insert name="navigation"/> 
      </div> 
      <ui:insert name="main"> 
       Welcome to the nest! 
      </ui:insert> 
     </h:form> 
    </body> 
</html> 

parrot.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:ui="http://java.sun.com/jsf/facelets" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core"> 
    <body> 
     This and everything before will be ignored 
     <ui:composition template="template.xhtml"> 
      <ui:define name="navigation"> 
       <ui:include src="menu.xhtml"/> 
      </ui:define> 
      <ui:define name="main"> 
       <h1>Parrot</h1> 
       <p> 
        Parrots are interesting birds... 
       </p> 
      </ui:define> 
     </ui:composition> 
     This and everything after will be ignored 
    </body> 
</html> 

回答

1

對不起,如果我missunderstand你的方法!

沒有必要把XHTML文件的WEB-INF導演,因爲你的臉的servlet模式被定義爲你上面:

<servlet-mapping> 
     <servlet-name>Faces Servlet</servlet-name> 
     <url-pattern>*.xhtml</url-pattern> 
    </servlet-mapping> 

在這種情況下,沒有公共的用戶可以得到的直接/真正來源您的JSF XHTML文件。 Web應用程序容器響應每次調用* .xhtml和編譯的html內容。

也別擔心!

+0

好吧,我喜歡這樣,用戶只能得到編譯結果 - 這似乎足夠安全,我的目的。你指的是什麼誤解? – Thufir 2014-09-29 23:34:40

+0

@Thufir:如果你想通過瀏覽器無法訪問這些文件,在這種情況下,我誤解了你的目的:)。 – 2014-09-30 12:14:16

2

如果你想不使用地址欄訪問的頁面,我認爲你能做的最好的事情就是避免在您的應用中重定向,這樣url不會顯示給最終用戶,所以不會被收藏。但是,這並不妨礙最終用戶使用瀏覽器端調試器或嘗試使用一組url並找到屬於您的頁面的URL。發生這種情況是因爲您使用GET請求來訪問視圖。

如果你真的想避免這種情況,你應該使用POST從視圖導航,即使這是完全不鼓勵。不要使用模板,只需使用ui:include就可以訪問您的母版頁幷包含您的子視圖,並使用ajax更改位置。這可能會帶來一些問題,例如,瀏覽器的後退按鈕無法正常工作。

我的建議是與避免重定向的第一選擇。您應該在WEB-INF目錄中放入的文件是template.xhtml文件,最終用戶無法直接訪問它。

參見: