如何在struts 1.3.10中显示警告框?

时间:2013-12-12 11:47:32

标签: javascript jsp jsp-tags struts-1

如何在登录表单中显示警告框以进行验证?

我得到了以下输出,但我想要警报框?

enter image description here

这是我的索引页面:index.jsp

 <html:form styleId="validate" action="/account_login" styleClass="account" method="post">
                        <div class="content controls single_advance">
                            <!-- <div class="form-row user-change-row">-->
                            <div class=form-row>
                                <div class=col-sm-3></div>
                                <div class=col-sm-4>
                                    <bean:write name="account_bean" property="error" filter="false"/>
                                </div>

                            </div>
                            <div class="form-row">
                                <div class="col-md-12">
                                    <div class=input-group>
                                        <div class=input-group-addon> <span class=icon-user></span> </div>
                                        <!--<input type=text class=form-control placeholder="Account Name"/>-->
                                        <html:text property="name" styleClass="validate[required] form-control" styleId="Account_Name"/>
                                    </div>
                                </div>
                            </div>
                            <div class=form-row>
                                <div class="col-md-12">
                                    <div class=input-group>
                                        <div class=input-group-addon> <span class=icon-key></span> </div>
                                        <!--<input type=password class=form-control placeholder="Password"/>-->
                                        <html:password property="password" styleClass="validate[required] form-control" styleId="Password"/>
                                    </div>
                                </div>
                            </div>
                            <div class=form-row>
                                <div class="col-md-6 col-md-offset-3">
                                    <input class="btn btn-default btn-block btn-clean btn-success" value="Log In" type="submit">
                                </div>
                            </div>
                            <!--<div class=form-row>
                              <div class="col-md-12"> <a href=# class="btn btn-link btn-block">Forgot your password?</a> </div>
                            </div>-->
                            <div class=form-row>
                                <div class="col-md-12 tac"><strong>OR USE </strong></div>
                            </div>
                            <div class=form-row>
                                <div class="col-md-12"> <a class="btn btn-primary btn-block imeino"><span class="icon-mobile-phone"></span> &nbsp; IMEI NO LOGIN</a> </div>
                            </div>
                        </div>
                    </html:form>

这是我的struts动作类:account_actoin.java

 if (account_name.equals(acc_name) && account_password.equals(acc_password)) {

        HttpSession http_session = request.getSession(true);
        http_session.setAttribute("login_name", acc_name);


        ArrayList<String> name_list = new ArrayList<String>();

        Query enc_password = session.createQuery("from account_dao");
        List list = enc_password.list();
        for (Iterator iterator = list.iterator(); iterator.hasNext();) {
            account_dao account = (account_dao) iterator.next();
            System.out.println("Id : " + account.getId());
            System.out.println("Account Name : " + account.getName());
            System.out.println("Account Password : " + account.getPassword());
            int i = 0;
            name_list.add(i,account.getName());
            i++;
        }
        request.setAttribute("namelist", name_list);
        dealer_bean Bean = new dealer_bean();
        Bean.setCustomerList(name_list);
        System.out.println("List : "+name_list);


        return mapping.findForward(SUCCESS);
    } else {
        fromBean.setError("<span style='color:red'><h6>Login failed.</h6></span>");
        return mapping.findForward(FAILURE);
    }

如何在警告框中显示错误(或)成功消息?

3 个答案:

答案 0 :(得分:1)

html:form关闭代码后附加下一个代码并尝试:

<logic:present name="account_bean" property="error">
<div>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.9.1.js"></script>
    <script src="//code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <script>
        // When DOM is ready
        $(function() {
            $("#errorDialog").dialog({
                title: "Login",
                autoOpen: false,
                resizable: false,
                show: "clip",
                hide: "clip",
                modal: true,
                dialogClass: "ui-state-active"
            });
        });
        // When Window is ready
        $(window).load(function() {
            $("#errorDialog").dialog("open");
        });
    </script>
    <div id="errorDialog">
        <bean:write name="account_bean" property="error" filter="false"/>
    </div>
</div>
</logic:present>

答案 1 :(得分:0)

只需尝试使用struts逻辑标记即可。这里我使用了jQuery ready()函数,

<logic:present name="account_bean" property="error">
<script>
    $(document).ready(function() {
        alert('Login failed.');
    });
</script>
</logic:present>

希望这会有所帮助..

答案 2 :(得分:0)

我没有尝试过,我甚至不确定这是否有效。写作评论太久了。

在表单bean中创建一个字段,如下所示:

public class MyForm{
    String isUserValid;

    //its getters and setters
}

然后在你的account_actoin.java

if (account_name.equals(acc_name) && account_password.equals(acc_password)) {
    //rest of your code

    formBean.setIsUserValid("Y")//------------Add this
    return mapping.findForward(SUCCESS);
else {
        formBean.setIsUserValid("N")//--------And this
        return mapping.findForward(FAILURE);
    }

在你的jsp文件中添加一个隐藏的输入和以下脚本,如下所示:

<head>
<script type="text/javascript">
    $(document).ready(function(){
         var isLoggedIn = $("#isUserValid").val();
         if(isLoggedIn == "N"){
               alert("Log in failed");
         }
    });
</script>
</head>
<body>
<input type="hidden" id="isUserValid" name="isUserValid" />