0

如何将xhtml文件放入WEB-INF以便只有应用程序可以直接访问它们?具体来说,这样鸟类页面就不能直接公开访问。该项目来自apress 示例

项目树:

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

网页.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>

索引.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>

菜单.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>

模板.xhtml:

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

鹦鹉.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>
4

2 回答 2

2

如果您不希望使用地址栏访问页面,我认为您可以做的最好的事情是避免在您的应用程序中重定向,这样 url 将不会显示给最终用户,因此不会被收藏. 但是,这不会阻止最终用户使用浏览器端调试器或尝试一组 url 并找到属于您页面的那个。发生这种情况是因为您使用GET请求来访问视图。

如果你真的想避免你应该使用POST从一个视图导航到另一个视图,即使这样也是完全不鼓励的。而不是使用模板,只需访问您的母版页并使用 ajax 动态地包含您的子视图ui:include并更改位置。这可能会带来一些问题,例如浏览器的后退按钮无法正常工作。

我的建议是选择避免重定向的首选。您应该放在 WEB-INF 目录中的是template.xhtml文件,最终用户直接访问它是没有意义的。

也可以看看:

于 2014-09-29T07:03:23.807 回答
1

对不起,如果我误解了你的方法!

无需将 xhtml 文件放在 web-inf 目录中,因为您的 faces servlet 模式已如上定义:

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

在这种情况下,任何公共用户都无法获得 JSF XHTML 文件的直接/真实来源。Web App Container 使用已编译的 html 内容响应对 *.xhtml 的每次调用。

也别担心!

于 2014-09-29T15:47:49.463 回答