java zk框架:如何从zul目录下的WEB-INF加载zul页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14101700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:04:30  来源:igfitidea点击:

zk framework: how to load zul pages from WEB-INF under directory zul

javazkzul

提问by vicky

I am using zk framework 6. I am trying to put my zul pages in /WEB-INF/zul directory. My index.zul file forwards the request to /WEB-INF/zul/login.zul which has a composer LoginComposer. But when I am on login page I want to redirect the user to another page e.g. home.zul. But I am getting 404 error.

我正在使用 zk 框架 6。我试图将我的 zul 页面放在 /WEB-INF/zul 目录中。我的 index.zul 文件将请求转发到 /WEB-INF/zul/login.zul,它有一个 Composer LoginComposer。但是当我在登录页面时,我想将用户重定向到另一个页面,例如 home.zul。但我收到 404 错误。

Both login.zul and home.zul are in zul directory along with their respective composers.

login.zul 和 home.zul 以及它们各自的作曲家都在 zul 目录中。

in loginComposer.java i have the following code to redirect to the home page which is called on a button click.

在 loginComposer.java 中,我有以下代码重定向到单击按钮时调用的主页。

 Execution exec = Executions.getCurrent();
                HttpServletResponse response = (HttpServletResponse)exec.getNativeResponse();
                response.sendRedirect(response.encodeRedirectURL("/WEB-INF/zul/home.zul")); //assume there is /login
                exec.setVoided(true); 

I created the project as a zk project from eclipse and i made no changes to web.xml.

我将项目创建为 eclipse 的 zk 项目,但我没有对 web.xml 进行任何更改。

please guide me how can i go from here.

请指导我如何离开这里。

Thank in advance.

预先感谢。

web.xml

网页.xml

<?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.com/xml/ns/javaee/web-app_2_5.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">
  <display-name>abc</display-name>
  <listener>
    <description>
    Used to cleanup when a session is destroyed</description>
    <display-name>ZK Session cleaner</display-name>
    <listener-class>org.zkoss.zk.ui.http.HttpSessionListener</listener-class>
  </listener>
  <servlet>
    <description>
    The ZK loader for ZUML pages</description>
    <servlet-name>zkLoader</servlet-name>
    <servlet-class>org.zkoss.zk.ui.http.DHtmlLayoutServlet</servlet-class>
    <init-param>
        <param-name>update-uri</param-name>
        <param-value>/zkau</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet>
    <description>
    The asynchronous update engine for ZK</description>
    <servlet-name>auEngine</servlet-name>
    <servlet-class>org.zkoss.zk.au.http.DHtmlUpdateServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>zkLoader</servlet-name>
    <url-pattern>*.zul</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>zkLoader</servlet-name>
    <url-pattern>*.zhtml</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>auEngine</servlet-name>
    <url-pattern>/zkau/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>index.zul</welcome-file>
  </welcome-file-list>
</web-app>

zk.xml

zk.xml

<?xml version="1.0" encoding="UTF-8"?>

<!--
    Created by ZK Studio
-->

<zk>

        <device-config>
            <device-type>ajax</device-type>
            <timeout-uri>/timeout.zul</timeout-uri><!-- An empty URL can cause the browser to reload the same URL -->
        </device-config>

    </zk>

LoginComposer.java

登录Composer.java

public class LoginComposer extends SelectorComposer<Component>{

    private static final long serialVersionUID = -1657004425904043268L;

    @Wire
    private Button buttontestButton;

    @Listen("onClick = #testButton")
    public void cancelButton(){


             Executions.sendRedirect("/WEB-INF/zul/home.zul");


    }
}

回答by Nabil A.

It is not possible

这不可能

I looked around and found a german site tht explains, that the spec
of java-servlet define the WEB-INF folder as not client accessable,
cos it contains data as classes that never should be accessed from outside the server.

我环顾四周,发现一个德国站点解释说,
java-servlet的规范将 WEB-INF 文件夹定义为客户端不可访问,
因为它包含作为永远不应从服务器外部访问的类的数据。

If you have the problems in a folder that is not WEB-INF:

如果您在不是 WEB-INF 的文件夹中遇到问题:

You should better use Executions.sendRedirect(java.lang.String uri)
to redirect by a button click with server-side action needed.
If you just want to redirect, set the buttons href.
It should look like

您应该更好地使用 需要服务器端操作的按钮单击来重定向。 如果您只想重定向,请设置按钮href。 它应该看起来像Executions.sendRedirect(java.lang.String uri)


Executions.sendRedirect("/zul/home.zul");

or in java:

或在 Java 中:

myButton.setHref("/zul/home.zul");

in zul:

在祖尔:

<button ... href="/zul/home.zul" ...>

Edit

编辑

I could write much, but the best would be to say, if you

我可以写很多,但最好是说,如果你

  1. do not use Spring follow thisand if you get 404
    check your deploy options/ deployed stuff.
  2. use Spring, what I would prefer because of easy
    ajax login site, security annotations at java methods
    and easy zk integration, follow the zk guide for spring.
  1. 不要使用 Spring 按照这个,如果你得到 404
    检查你的部署选项/部署的东西。
  2. 使用 Spring,我更喜欢使用简单的
    ajax 登录站点、java 方法的安全注释
    和简单的 zk 集成,请遵循springzk 指南

If you still have 404 and can't figure them out, please post your
configuration files or classes.

如果您仍然有 404 并且无法弄清楚,请发布您的
配置文件或类。

回答by kachhalimbu

As others have noted direct/redirect access to pages under WEB-INF is not allowed by servlet specification, The best practice is to keep login.zul outside WEB-INF folder so users of application can have a direct access to it. Now for rest of the files you can keep them under WEB-INF folder and render them using include component or Executions.createComponents().

正如其他人所指出的,servlet 规范不允许直接/重定向访问 WEB-INF 下的页面,最佳实践是将 login.zul 保留在 WEB-INF 文件夹之外,以便应用程序用户可以直接访问它。现在对于其余的文件,您可以将它们保存在 WEB-INF 文件夹下,并使用包含组件或Executions.createComponents().

Generally I keep my partial zul pages in WEB-INF folder so they aren't directly accessible but I keep my layout pages eg. home.zul or main.zul outside WEB-INF folder (also they should be restricted to access if user isn't logged in) Refer hereto learn how to restrict page access using Spring Security

通常我将我的部分 zul 页面保存在 WEB-INF 文件夹中,因此它们不能直接访问,但我保留我的布局页面,例如。WEB-INF 文件夹外的 home.zul 或 main.zul(如果用户未登录,它们也应该被限制访问)请参阅此处了解如何使用 Spring Security 限制页面访问

回答by Sonu patel

I hope this help some one:

我希望这对某人有所帮助:

Forward zul file to another zul file you can use this code

将 zul 文件转发到另一个 zul 文件,您可以使用此代码

<?forward uri="/directory_name/zul_fileName.zul"?>

For Include zul pages on same zul page you can use this code

对于在同一个 zul 页面上包含 zul 页面,您可以使用此代码

<include src="/directory_name/zul_fileName.zul" > </include>

回答by Narayan Subedi

You cannot.

你不能。

Files in /WEB-INF folder are not directly accessible. If you want to have such kind of file system then you should better integrate spring framework.

/WEB-INF 文件夹中的文件不能直接访问。如果你想拥有这样的文件系统,那么你最好集成spring框架。