本文介绍了我的登录/索引 zul 页面加载了两次,因为我只调用了一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 zk ce 6.5 版,但遇到了问题.我的登录/索引页面被加载了两次.

  1. 当我在注销按钮上调用这个:

    Executions.createComponents("login", center, null);

上面的页面有两个字段,我使用的是 MVVM 方法.

现在加载正常,但是只要我在文本框中输入用户名或在文本框中输入密码,页面就会自动刷新.我已经检查了我的代码,我没有在此处的进程中的任何其他地方调用上述方法.

请告诉我为什么在文本框/密码文本框上出现登录页面的这种行为.一旦它重新加载两次,它就会获取用户名并将我带到主页.现在我注销和相同的场景.

两次,我都调用了此方法,一次很明显,因为注销时我调用了它,但是在文本框中输入任何文本并从中移除焦点时.然后再次调用该方法.

@AfterComposepublic void afterCompose(@ContextParam(ContextType.VIEW) 组件视图){Selectors.wireComponents(view, this, false);}

我也检查了我的代码,任何文本字段上都没有事件.

登录.zul

<window id="loginWin" border="none" width="100%" height="100%"apply="org.zkoss.bind.BindComposer"viewModel="@id('vmc') @init('com.example.LoginViewModel')"validationMessages="@id('vmsgs')"><边框布局><west border="normal" size="70%" title="What Intelliops Do?"><div width="100%" height="100%">asdasd asd asd asdasd asd asd asd asd asd asd asd asd asdasd asd a d asdasd a sd asd asd asd a d asdasd a sd asdassd asd a d asdasd a sd asd assd asd

</西><east border="normal" size="30%" title="登录"><div width="100%" height="100%"><grid sizedByContent="true"form="@id('lv') @load(vmc.userDto)@save(vmc.userDto, before='loginButtonInvoke')@validator('com.example.LoginValidator')"><column width="50%"></column><column width="50%"></column></列><row align="left" width="100%"style="background:white;.row:hover{background-color: #fff};border:none"><cell style="text-align:right">用户名</单元格><单元格><textbox id="userNameTextBox" type="text"value="@bind(lv.user.username)" maxlength="50"/><image src="@load(vmsgs['userNameTextBoxError'])" width="12px"height="12px" tooltiptext="@load(vmsgs['userNameTextBox'])"></image></单元格></row><行宽="100%"style="background:white;.row:hover{background-color: #fff};border:none"><cell style="text-align:right">密码</单元格><单元格><textbox id="passwordTextBox" type="密码"value="@bind(lv.user.password)" maxlength="20"/><image src="@load(vmsgs['passwordTextBoxError'])" width="12px"height="12px" tooltiptext="@load(vmsgs['passwordTextBox'])"/></单元格></row><行宽="100%"style="background:white;.row:hover{background-color: #fff};border:none"><cell style="text-align:right"><button id="loginButton" label="登录"onClick="@command('loginButtonInvoke')"/></单元格><单元格><label id="loginErrorLabel" style="color:red"/></单元格></row><row spans="2" width="100%"style="background:white;.row:hover{background-color: #fff};border:none"><cell colspan="2" style="text-align:right"><a id="forgetPasswordLink">忘记密码?</a></单元格></row></rows></网格>

</东></borderlayout></窗口></zk>

登录视图模式类

package com.example;导入 org.apache.log4j.Logger;导入 org.zkoss.bind.annotation.AfterCompose;导入 org.zkoss.bind.annotation.Command;导入 org.zkoss.bind.annotation.ContextParam;导入 org.zkoss.bind.annotation.ContextType;导入 org.zkoss.zk.ui.Component;导入 org.zkoss.zk.ui.select.Selectors;导入 org.zkoss.zk.ui.select.annotation.Wire;导入 org.zkoss.zkplus.spring.SpringUtil;导入 org.zkoss.zul.Label;导入 org.zkoss.zul.Textbox;导入 com.example.UserDTO;导入 com.example.IUserService;导入示例.Pages;导入示例.TemplateUtil;导入示例.UserUtil;公共类 LoginViewModel{私人记录器记录器 = Logger.getLogger(this.getClass().getName());IUserService userService = (IUserService)SpringUtil.getBean("userService");UserDTO userDto = new UserDTO();私有静态最终字符串 USER_ROLES = "userRoles";private static final String MASTER = "master";@Wire("#userNameTextBox")文本框 userNameTextBox;@Wire("#passwordTextBox")文本框密码TextBox;@Wire("#loginErrorLabel")标签登录错误标签;@AfterComposepublic void afterCompose(@ContextParam(ContextType.VIEW) 组件视图){Selectors.wireComponents(view, this, false);}@Command("登录按钮调用")public void loginButtonInvoke(){logger.info("LoginViewModel:afterCompose >>>>>>>>>>>>>>");logger.info("loginButtonInvoke:" + userDto.getUserIntelliOps().getUsername() + " " + userDto.getUserIntelliOps().getPassword());userDto = userService.getUserAuthentication(userDto);if(userDto!=null){UserUtil.setSession(userDto);logger.info("loginButtonInvoke:userDto!=null");/*列表list = userService.getAllOtherOperationsService().userAuthorizationMatrix(userDto.getUserIntelliOps().getRoles().get(0).toString());*///列表list = userService.getAllOtherOperationsService().userAuthorizationMatrix(3+"");//logger.info("asdasdas " + list.size());/*获取用户授权菜单*///TODO 下面这行在做什么????userService.getUserAuthorizedMenu();TemplateUtil.loadContent(Pages.HOME_FILE,true);}别的{if(userDto==null){userDto = new UserDTO();}loginErrorLabel.setValue("用户名或密码无效.");}}公共 UserDTO getUserDto() {返回 userDto;}public void setUserDto(UserDTO userDto) {this.userDto = userDto;}}

TemplateUtil 类来绘制页面

package com.example;导入 org.apache.log4j.Logger;导入 org.zkoss.zk.ui.Executions;导入 org.zkoss.zk.ui.Path;导入 org.zkoss.zul.A;导入 org.zkoss.zul.Borderlayout;导入 org.zkoss.zul.Button;导入 org.zkoss.zul.Center;导入 org.zkoss.zul.Include;导入 org.zkoss.zul.North;公共类 TemplateUtil {静态记录器记录器 = Logger.getLogger(TemplateUtil.class.getName());public static void loadContent(String contentPage, boolean isMenu) {logger.info("TemplateUtil: loadContent().........");Borderlayout mainlayout = (Borderlayout) Path.getComponent("/main/mainLayout");North topHeader = mainlayout.getNorth();topHeader.getFellow("menuDiv").setVisible(isMenu);//TODO 删除以下记录器如果(是菜单==真){anchorUsername.setLabel(UserUtil.getUserName());logger.info("isMenu==true....");Executions.createComponents(Pages.MENU_FILE, topHeader.getFellow("menuDiv"), null);UserUtil.setSession(true);}topHeader.invalidate();中心中心 = mainlayout.getCenter();center.getChildren().clear();logger.info("contentPage:" + contentPage);Executions.createComponents(contentPage, center, null);}}

请帮我解决这个问题

让我更新你.1. 第一次没有刷新 2. 登录后,当我单击注销并转到登录页面时,只要我在任何文本框中添加值,页面就会刷新.没有在任何这些字段上写入任何操作.

解决方案

我不明白你为什么要这样做......

Executions.createComponents("login", center, null);

当您要注销

据我所知,你可以这样做...

//Executions.getCurrent().getDesktop().invalidate();//条件基础如果你需要使用它否则不要使用它Executions.getCurrent().getSession().invalidate();Executions.sendRedirect("/login.zul");

I am using zk ce version 6.5 and i have a problem. My login/index pages is being loaded twice.

  1. when i on logout button call this:

    Executions.createComponents("login", center, null);

above page has two fields and i am using MVVM approach.

Now it loads fine, but as soon as i enter the username in the textbox or enter the password in textbox, page is automatically refreshed. I have checked in my code, i am not calling the above method anywhere else in a process here.

Please tell me why on textbox/password textbox i am getting this behaviour for the loginpage. once it's reloaded twice, it gets the username and takes me to home page. now i logout and same scenario.

for two times, i am getting call to this method, once is obvious, bacause on logout i called it, but on entering any text in textbox and removing the focus from it. then this method is again called.

@AfterCompose
public void afterCompose(@ContextParam(ContextType.VIEW) Component view){
    Selectors.wireComponents(view, this, false);
 }

I have inspected my code too, there is no event on any of the textfield.

login.zul

<zk>
    <window id="loginWin" border="none" width="100%" height="100%"
        apply="org.zkoss.bind.BindComposer"
        viewModel="@id('vmc') @init('com.example.LoginViewModel')"
        validationMessages="@id('vmsgs')">

        <borderlayout>
            <west border="normal" size="70%" title="What Intelliops Do?">
                <div width="100%" height="100%">
                    asdasd as asd a d asdasd a sd asd asasd asd d asdasd as
                    d a d asdasd a sd asd asasd asd a d asdasd a sd asd
                    asasd asd a d asdasd a sd asd asasd asd
                </div>
            </west>
            <east border="normal" size="30%" title="Login">
                <div width="100%" height="100%">
                    <grid sizedByContent="true"
                        form="@id('lv') @load(vmc.userDto)
                       @save(vmc.userDto, before='loginButtonInvoke')
                       @validator('com.example.LoginValidator')">
                        <columns>
                            <column width="50%"></column>
                            <column width="50%"></column>
                        </columns>
                        <rows>
                            <row align="left" width="100%"
                                style="background:white;.row:hover{background-color: #fff};border:none">
                                <cell style="text-align:right">
                                    User Name
                                </cell>
                                <cell>
                                    <textbox id="userNameTextBox" type="text"
                                        value="@bind(lv.user.username)" maxlength="50" />
                                    <image src="@load(vmsgs['userNameTextBoxError'])" width="12px"
                                        height="12px" tooltiptext="@load(vmsgs['userNameTextBox'])"></image>
                                </cell>
                            </row>
                            <row width="100%"
                                style="background:white;.row:hover{background-color: #fff};border:none">
                                <cell style="text-align:right">
                                    Password
                                </cell>
                                <cell>
                                    <textbox id="passwordTextBox" type="password"
                                        value="@bind(lv.user.password)" maxlength="20" />
                                    <image src="@load(vmsgs['passwordTextBoxError'])" width="12px"
                                        height="12px" tooltiptext="@load(vmsgs['passwordTextBox'])" />
                                </cell>
                            </row>
                            <row width="100%"
                                style="background:white;.row:hover{background-color: #fff};border:none">
                                <cell style="text-align:right">
                                    <button id="loginButton" label="Login"
                                        onClick="@command('loginButtonInvoke')" />
                                </cell>
                                <cell>
                                    <label id="loginErrorLabel" style="color:red" />
                                </cell>
                            </row>
                            <row spans="2" width="100%"
                                style="background:white;.row:hover{background-color: #fff};border:none">
                                <cell colspan="2" style="text-align:right">
                                    <a id="forgetPasswordLink">
                                        Forgot Password?
                                    </a>
                                </cell>
                            </row>
                        </rows>
                    </grid>
                </div>
            </east>
        </borderlayout>
    </window>
</zk>

loginviewmode class

package com.example;


import org.apache.log4j.Logger;
import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.select.Selectors;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zkplus.spring.SpringUtil;
import org.zkoss.zul.Label;
import org.zkoss.zul.Textbox;

import com.example.UserDTO;
import com.example.IUserService;
import example.Pages;
import example.TemplateUtil;
import example.UserUtil;

public class LoginViewModel{

    private Logger logger = Logger.getLogger(this.getClass().getName());

    IUserService userService = (IUserService)SpringUtil.getBean("userService");

    UserDTO userDto = new UserDTO();

    private static final String USER_ROLES = "userRoles";
    private static final String MASTER = "master";

    @Wire("#userNameTextBox")
    Textbox userNameTextBox;

    @Wire("#passwordTextBox")
    Textbox passwordTextBox;

    @Wire("#loginErrorLabel")
    Label loginErrorLabel;

    @AfterCompose
    public void afterCompose(@ContextParam(ContextType.VIEW) Component view){
        Selectors.wireComponents(view, this, false);
     }

    @Command("loginButtonInvoke")
    public void loginButtonInvoke(){
        logger.info("LoginViewModel:afterCompose >>>>>>>>>>>>>>>");
        logger.info("loginButtonInvoke: " + userDto.getUserIntelliOps().getUsername() + " " + userDto.getUserIntelliOps().getPassword());
        userDto = userService.getUserAuthentication(userDto);



        if(userDto!=null){
            UserUtil.setSession(userDto);
            logger.info("loginButtonInvoke:userDto!=null ");
            /*List<ViewObjectOperation> list = userService.getAllOtherOperationsService().userAuthorizationMatrix(userDto.getUserIntelliOps().getRoles().get(0).toString());*/
            //List<ViewObjectOperation> list = userService.getAllOtherOperationsService().userAuthorizationMatrix(3+"");
            //logger.info("asdasdas   " + list.size());
            /*getUserAuthorizedMenu*/
            //TODO what is the following line doing here????
            userService.getUserAuthorizedMenu();

            TemplateUtil.loadContent(Pages.HOME_FILE,true);
        }else{
            if(userDto==null){
                userDto = new UserDTO();
            }
        loginErrorLabel.setValue("Invalid username or password.");
        }

    }

    public UserDTO getUserDto() {
        return userDto;
    }

    public void setUserDto(UserDTO userDto) {
        this.userDto = userDto;
    }




}

TemplateUtil class to draw the pages

package com.example;

import org.apache.log4j.Logger;

import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Path;
import org.zkoss.zul.A;
import org.zkoss.zul.Borderlayout;
import org.zkoss.zul.Button;
import org.zkoss.zul.Center;
import org.zkoss.zul.Include;
import org.zkoss.zul.North;


public class TemplateUtil {

    static Logger logger = Logger.getLogger(TemplateUtil.class.getName());

    public static void loadContent(String contentPage, boolean isMenu) {


        logger.info("TemplateUtil: loadContent().......... ");

        Borderlayout mainlayout = (Borderlayout) Path.getComponent("/main/mainLayout");
        North topHeader = mainlayout.getNorth();

        topHeader.getFellow("menuDiv").setVisible(isMenu);

        //TODO remove the following logger

        if(isMenu==true ){
            anchorUsername.setLabel(UserUtil.getUserName());
            logger.info("isMenu==true....");
            Executions.createComponents(Pages.MENU_FILE, topHeader.getFellow("menuDiv"), null);
            UserUtil.setSession(true);
        }

        topHeader.invalidate();

        Center center = mainlayout.getCenter();
        center.getChildren().clear();
        logger.info("contentPage: " + contentPage);
        Executions.createComponents(contentPage, center, null);

    }


}

please help me on this

Let me update you. 1. first time no refresh 2. after login, when i click on logout and go to login page, as soon as i add the value in any of the textbox, pages refreshes. no action is written on any of those fields.

解决方案

I am not able to understand why you are doing ...

Executions.createComponents("login", center, null);

When you going for logout

From my understanding you can do this thing like this...

//Executions.getCurrent().getDesktop().invalidate();//Condition base if you needed use it otherwise dont use it
Executions.getCurrent().getSession().invalidate();
Executions.sendRedirect("/login.zul");

这篇关于我的登录/索引 zul 页面加载了两次,因为我只调用了一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 15:39