3

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.

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

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

 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.

please guide me how can i go from here.

Thank in advance.

web.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

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

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");


    }
}
vicky
  • 890
  • 4
  • 23
  • 54

4 Answers4

5

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.

If you have the problems in a folder that is not 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

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

or in 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 this and 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.

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

Nabil A.
  • 3,270
  • 17
  • 29
  • I had also tried that, but not working. My url changes to http://localhost:8080/abc/zul/home.zul and i get error in browser HTTP Status 404 - /zul/home.zul and description The requested resource (/zul/home.zul) is not available. remember both login and home pages are in zul under web-inf. – vicky Jan 01 '13 at 05:13
  • to forward the from index which is at webRoot, i am using following statement: – vicky Jan 01 '13 at 05:24
  • I have added web.xml, zk.xml and LoginComposer.java files. Unfortunately I am still getting 404. I followed your previous guides. – vicky Jan 01 '13 at 07:32
  • The files look ok, so it seems to be a deployment problem. Check your deployment options and make a clean deployment. Means delet everything from the server and deploy. If you use eclipse, right click the server and click the both clean options you found there. – Nabil A. Jan 01 '13 at 07:55
  • 1
    i did that but still the same error. May be zk doesn't support this feature and i have to use spring to do that. What do you say ??? – vicky Jan 01 '13 at 08:08
  • I also tried a lot but doesn't get through. In jsp also response.sendRedirect() doesn't work but RequestDispatcher works fine... Still trying to solve this issue... – Narayan Subedi Jan 01 '13 at 08:58
  • Thank you all @Nabil special thanks to you. so it's decided that spring is the way to do the job done. – vicky Jan 02 '13 at 12:59
3

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().

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 here to learn how to restrict page access using Spring Security

kachhalimbu
  • 2,190
  • 17
  • 14
  • you are also suggesting to use Spring. ZK doesn't have it's own facility to redirect file inside WEB-INF folder. – Narayan Subedi Jan 03 '13 at 09:52
  • 2
    I suggested to use Spring Security to secure pages outside WEB-INF not for redirection. ZK doesn't have any facility to redirect file inside WEB-INF because it isn't allowed by Servlet specification. I don't believe even Spring can do that. Can you point to any resource/article that demonstrates Spring's feature that allows you to redirect to file under WEB-INF? I'm curious. – kachhalimbu Jan 04 '13 at 02:31
0

I hope this help some one:

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

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

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

<include src="/directory_name/zul_fileName.zul" > </include>
Sonu patel
  • 353
  • 1
  • 8
-1

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.

Narayan Subedi
  • 1,343
  • 2
  • 20
  • 46