Revision history [back]

click to hide/show revision 1
initial version

answered 2014-07-09 10:17:24 +0800

cyiannoulis gravatar image cyiannoulis

You shouldn't have any problem regarding the session attributes. Here is a complete example tested with ZK 7.0.2

The Login page:

<zk> <window apply="org.zkoss.bind.BindComposer" viewmodel="@id('vm') @init('snippets.login.LoginVM')" border="none" <br=""> xmlns:n="native" style="overflow: auto;">

<div align="center" style="margin-top: 10%">
    <vlayout>
        <textbox type="text" placeholder="Username" value="@bind(vm.username)" 
                sclass="input-lg" />
        <textbox type="password" placeholder="Password" value="@bind(vm.password)" 
                sclass="input-lg" />
        <hlayout>
            <div class="btn-group">
                <button label="Login" sclass="btn btn-lg btn-success" width="50%" 
                        onClick="@command('login')" />
                <button label="Quit" sclass="btn btn-lg btn-danger" width="50%" 
                        href="http://www.pixar.com" />
            </div>
        </hlayout>
    </vlayout>
</div>

</window> </zk>

The Login View Model:

import org.zkoss.bind.annotation.Command;
import org.zkoss.zk.ui.Executions;

public class LoginVM {

    private String username;
    private String password;

    @Command
    public void login() {
        Executions.getCurrent().getSession().setAttribute("username", this.username);
        Executions.getCurrent().getSession().setAttribute("password", this.password);
        Executions.sendRedirect("welcome.zul");
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

The Welcome page:

<?page title="Login" contentType="text/html;charset=UTF-8"?>
<zk>
<window apply="org.zkoss.bind.BindComposer" 
        viewModel="@id('vm') @init('snippets.login.WelcomeVM')" 
        border="none"  
        xmlns:n="native"
        style="overflow: auto;">

    <div align="center" style="margin-top: 10%">
        <n:h1>Welcome back <label value="@load(vm.username)" style="font-size: 26pt" /> </n:h1>
        Password:<label value="@load(vm.password)" />
    </div>
</window>
</zk>

And the Welcome view model:

import org.zkoss.zk.ui.Executions;

public class WelcomeVM {

    public String getUsername() {
        return (String) Executions.getCurrent().getSession().getAttribute("username");
    }

    public String getPassword() {
        return (String) Executions.getCurrent().getSession().getAttribute("password");
    }
}

Hope that helps

Costas

You shouldn't have any problem regarding the session attributes. Here is a complete example tested with ZK 7.0.2

The Login page:

<zk> <window apply="org.zkoss.bind.BindComposer" viewmodel="@id('vm') @init('snippets.login.LoginVM')" border="none" <br=""> xmlns:n="native" style="overflow: auto;">

<div align="center" style="margin-top: 10%">
    <vlayout>
        <textbox type="text" placeholder="Username" value="@bind(vm.username)" 
                sclass="input-lg" />
        <textbox type="password" placeholder="Password" value="@bind(vm.password)" 
                sclass="input-lg" />
        <hlayout>
            <div class="btn-group">
                <button label="Login" sclass="btn btn-lg btn-success" width="50%" 
                        onClick="@command('login')" />
                <button label="Quit" sclass="btn btn-lg btn-danger" width="50%" 
                        href="http://www.pixar.com" />
            </div>
        </hlayout>
    </vlayout>
</div>

</window> </zk>

The Login View Model:

import org.zkoss.bind.annotation.Command;
import org.zkoss.zk.ui.Executions;

public class LoginVM {

    private String username;
    private String password;

    @Command
    public void login() {
        Executions.getCurrent().getSession().setAttribute("username", this.username);
        Executions.getCurrent().getSession().setAttribute("password", this.password);
        Executions.sendRedirect("welcome.zul");
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

The Welcome page:

<?page title="Login" contentType="text/html;charset=UTF-8"?>
<zk>
<window apply="org.zkoss.bind.BindComposer" 
        viewModel="@id('vm') @init('snippets.login.LoginVM')" 
        border="none"  
        xmlns:n="native"
        style="overflow: auto;">

    <div align="center" style="margin-top: 10%">
        <vlayout>
            <textbox type="text" placeholder="Username" value="@bind(vm.username)" 
                    sclass="input-lg" />
            <textbox type="password" placeholder="Password" value="@bind(vm.password)" 
                    sclass="input-lg" />
            <hlayout>
                <div class="btn-group">
                    <button label="Login" sclass="btn btn-lg btn-success" width="50%" 
                            onClick="@command('login')" />
                    <button label="Quit" sclass="btn btn-lg btn-danger" width="50%" 
                            href="http://www.pixar.com" />
                </div>
            </hlayout>
        </vlayout>
    </div>
</window>
</zk>

The Login View Model:

import org.zkoss.bind.annotation.Command;
import org.zkoss.zk.ui.Executions;

public class LoginVM {

    private String username;
    private String password;

    @Command
    public void login() {
        Executions.getCurrent().getSession().setAttribute("username", this.username);
        Executions.getCurrent().getSession().setAttribute("password", this.password);
        Executions.sendRedirect("welcome.zul");
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

The Welcome page:

<?page title="Login" contentType="text/html;charset=UTF-8"?>
<zk>
<window apply="org.zkoss.bind.BindComposer" 
        viewModel="@id('vm') @init('snippets.login.WelcomeVM')" 
        border="none"  
        xmlns:n="native"
        style="overflow: auto;">

    <div align="center" style="margin-top: 10%">
        <n:h1>Welcome back <label value="@load(vm.username)" style="font-size: 26pt" /> </n:h1>
        Password:<label value="@load(vm.password)" />
    </div>
</window>
</zk>

And the Welcome view model:

import org.zkoss.zk.ui.Executions;

public class WelcomeVM {

    public String getUsername() {
        return (String) Executions.getCurrent().getSession().getAttribute("username");
    }

    public String getPassword() {
        return (String) Executions.getCurrent().getSession().getAttribute("password");
    }
}

Hope that helps

Costas

You shouldn't have any problem regarding the session attributes. Here is a complete example tested with ZK 7.0.2

The Login page:

<?page title="Login" contentType="text/html;charset=UTF-8"?>
<zk>
<window apply="org.zkoss.bind.BindComposer" 
        viewModel="@id('vm') @init('snippets.login.LoginVM')" 
        border="none"  
        xmlns:n="native"
        style="overflow: auto;">

    <div align="center" style="margin-top: 10%">
        <vlayout>
            <textbox type="text" placeholder="Username" value="@bind(vm.username)" 
                    sclass="input-lg" />
            <textbox type="password" placeholder="Password" value="@bind(vm.password)" 
                    sclass="input-lg" />
            <hlayout>
                <div class="btn-group">
                    <button label="Login" sclass="btn btn-lg btn-success" width="50%" 
                            onClick="@command('login')" />
                    <button label="Quit" sclass="btn btn-lg btn-danger" width="50%" 
                            href="http://www.pixar.com" />
                </div>
            </hlayout>
        </vlayout>
    </div>
</window>
</zk>

The Login View Model:

import org.zkoss.bind.annotation.Command;
import org.zkoss.zk.ui.Executions;

public class LoginVM {

    private String username;
    private String password;

    @Command
    public void login() {
        Executions.getCurrent().getSession().setAttribute("username", this.username);
        Executions.getCurrent().getSession().setAttribute("password", this.password);
        Executions.sendRedirect("welcome.zul");
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

The Welcome page:

<?page title="Login" contentType="text/html;charset=UTF-8"?>
<zk>
<window apply="org.zkoss.bind.BindComposer" 
        viewModel="@id('vm') @init('snippets.login.WelcomeVM')" 
        border="none"  
        xmlns:n="native"
        style="overflow: auto;">

    <div align="center" style="margin-top: 10%">
        <n:h1>Welcome back <label value="@load(vm.username)" style="font-size: 26pt" /> </n:h1>
26pt"/></n:h1>
        Password:<label value="@load(vm.password)" />
    </div>
</window>
</zk>

And the Welcome view model:

import org.zkoss.zk.ui.Executions;

public class WelcomeVM {

    public String getUsername() {
        return (String) Executions.getCurrent().getSession().getAttribute("username");
    }

    public String getPassword() {
        return (String) Executions.getCurrent().getSession().getAttribute("password");
    }
}

Hope that helps

Costas

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More