html:message和html:errors 小技巧

这次项目中要求所有的后台操作中涉及对数据库的操作要求向页面打印是否成功的信息,成功用ActionMessages向页面传信息,并且使用蓝色字;失 败时使用ActionErrors向页面传信息,使用红色字.原以为必须都使用<fontcolor='red'></font& gt;来实现颜色问题,但页面多了很多代码.后来发现可以在ApplicationResources.properties中控制字体的颜色,给页面节 省了很多的代码.很适用的,自己快点去似着实现吧.

 

1.在ApplicationResources.properties加如下代码:

errors.header=<font color='red'>

 

errors.footer=</font>


messages.header=<font color='blue'>


messages.footer=</font>


errors.prefix=<li>


errors.suffix=</li>

 注:header,footer主要用于对字体的颜色,大小做限制,prefix,suffix可以在错误信息前面添加列表.

2.在页面中两者的使用有些区别:

    <html:errors />直接这样写就可以了.

<html:messages message="true" id="msg" header="messages.header" footer="messages.footer"><bean:write name="msg" /> </html:messages>必须加上配制文件中加的header和footer.

 

总结 : 此例子为了在配制文件中定义<html:messages/><html:errors />的颜色,如果需要修改颜色的话,仅在配置文件中进行修改,

 

LoginForm loginForm = (LoginForm) form;
        ActionMessages errors = new ActionMessages();
       
        ActionMessages actionMessages=new ActionMessages();
       
        String username = loginForm.getUsername();
        String password = loginForm.getPassword();

        if ("admin".equals(username) && "admin".equals(password)) {

            errors.add("admin", new ActionMessage("admin.error"));
           
            actionMessages.add("qunqun", new ActionMessage("admin.good","admin"));
           
            this.saveMessages(request, actionMessages);
            this.saveErrors(request, errors);
           
            return mapping.findForward("error");
        } else {
            return mapping.findForward("success");
        }
    }

 

<message-resources key="qun"
                    null="false"
        parameter="cn.qun.struts.ApplicationResources" />

 

 

<form action="login.do" method="post">
              <html:errors bundle="qun" property="admin"/><br>
             
              <html:messages id="qunqun" bundle="qun" message="true">
                  <bean:write name="qunqun"  bundle="qun"/>
              </html:messages><br/>

             <bean:message key="user.password.error" arg0="admin" bundle="qun"/><br/>


              <bean:message bundle="qun" key="user.name"/><input type="text" name="username" value="${loginForm.username }"><html:errors bundle="qun" property="name.error"/><br>
              <bean:message bundle="qun" key="user.password"/><input type="password" name="password" value="${loginForm.password }"><html:errors bundle="qun" property="password.error"/><br>
              <input type="submit" value="send"><input type="reset" value="reset">
          </form>

资源文件


user.name=username
user.password=password




#error
user.password.error={0} must input
user.name.error=username must imput
admin.error=we will not to say hello to admin
admin.good={0} is good

 

效果:

当用户名和密码都输入admin的时候

  就会提示:

we will not to say hello to admin
admin is good
admin must input

 

当直接<bean:write name="qunqun" bundle="qun"/>的时候(没有放在bean:message标签中的时候),就会

 

 Cannot find bean: "qunqun" in any scope 
的错误???
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值