首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > Ruby Rails >

弹出异常对话框alert('error')-struts

2012-11-04 
弹出错误对话框alert(error)--struts唉!很累!就把代码贴出来!希望对初学者有所帮助!? 简要说明:addError

弹出错误对话框alert('error')--struts

唉!很累!就把代码贴出来!希望对初学者有所帮助!?

简要说明:addError方法用于添加要弹出的消息或错误!

??????????????????? addMsg方法用于添加消息或错误!

    Java代码

    java 代码
    1. package?com.dotease.common; ?? ??
    2. import?javax.servlet.http.HttpServletRequest; ?? ??
    3. import?org.apache.struts.Globals; ?? import?org.apache.struts.action.ActionErrors; ??
    4. import?org.apache.struts.action.ActionMessage; ?? import?org.apache.struts.action.Action; ??
    5. import?org.apache.struts.action.ActionMessages; ?? import?org.apache.struts.util.MessageResources; ??
    6. import?java.util.ArrayList; ?? /** ?
    7. ?*?@author?Administrato?2007-10-9 ? ?*2007-10-9 ?
    8. ?*/?? public?class?Msg?{ ??
    9. ?? ????/** ?
    10. ?????*?save?the?error?messages!?This?like?Action.saveErrors();? ? ?????*?@param?request ?
    11. ?????*?@param?errors ? ?????*/??
    12. ????protected??static?final?void?saveErrors(HttpServletRequest?request,ActionMessages?errors)?{ ?? ??
    13. ????????if?((errors?==?null)?||?errors.isEmpty())?{ ?? ????????????request.removeAttribute(Globals.ERROR_KEY); ??
    14. ????????????return; ?? ????????} ??
    15. ????????//?Save?the?error?messages?we?need ?? ????????request.setAttribute(Globals.ERROR_KEY,?errors); ??
    16. ???????? ?? ????} ??
    17. ???? ?? ????/** ?
    18. ?????*?保存弹出消息到Request,使用默认的key:showError? ? ?????*?此信息在前台设置后,弹出! ?
    19. ?????*?建议: 放在紧挨着的地方! ? ?????*?适合有错误,就返回!?如果多次调用,会覆盖上次的Errors ?
    20. ?????*?@param?request?? ? ?????*?@param?ActionMessage???? ?
    21. ?????*? ? ?????*/??
    22. public?static?final?void?saveError(HttpServletRequest?request,ActionMessage?error)?{ ?? ???????? ??
    23. ????????MessageResources?msgResource?=?((MessageResources)?request.getAttribute(Globals.MESSAGES_KEY)); ?? ????????String?msg=msgResource.getMessage(error.getKey()); ??
    24. ????????int?length=0; ?? ????????if(error.getValues()!=null) ??
    25. ????????????length=error.getValues().length; ?? ????????for(int?i=0;i
    26. ????????{ ?? ????????????msg=msg.replace("{"+i+"}",?error.getValues()[i].toString()); ??
    27. ???????????? ?? ????????} ??
    28. ????????ActionMessages?errors?=?new?ActionMessages(); ?? ????????errors.add("showError",new?ActionMessage("showError",msg)); ??
    29. ????????saveErrors(request,errors);? ?? ????} ??
    30. ?? ????/** ?
    31. ?????*?把ActionMessage对象转化为文本字符串 ? ?????*?@param?request ?
    32. ?????*?@param?msg ? ?????*?@return ?
    33. ?????*/?? public?static?final?String?msgToString(HttpServletRequest?request,ActionMessage?error)? ??
    34. { ?? ????MessageResources?msgResource?=?((MessageResources)?request.getAttribute(Globals.MESSAGES_KEY)); ??
    35. ????String?msg=msgResource.getMessage(error.getKey()); ?? ????msg=filterString(msg); ??
    36. ????int?length=0; ?? ????if(error.getValues()!=null) ??
    37. ????????length=error.getValues().length; ?? ????for(int?i=0;i
    38. ????{ ?? ????????msg=msg.replace("{"+i+"}",?error.getValues()[i].toString()); ??
    39. ???????? ?? ????} ??
    40. ????return?msg; ?? } ??
    41. ????/** ? ?????*?返回MessageResources对象,通过它可以获得配置资源 ?
    42. ?????*?@param?request ? ?????*?@return ?
    43. ?????*/?? public?static?final?MessageResources?getMsgResources(HttpServletRequest?request)? ??
    44. {??? ?? ????return?((MessageResources)?request.getAttribute(Globals.MESSAGES_KEY));? ??
    45. } ?? ??
    46. ?? ??
    47. protected?static?final?ActionMessages?getErrors(HttpServletRequest?request) ?? { ??
    48. ????ActionMessages?errors?=?(ActionMessages)request.getAttribute(Globals.ERROR_KEY); ?? ????if(errors?==?null) ??
    49. ????????errors?=?new?ActionMessages(); ?? ????return?errors; ??
    50. ???? ?? } ??
    51. ????/** ? ?????*?保存弹出消息到Request,使用默认的key:showError? ?
    52. ?????*?@param?request ? ?????*?@param?error ?
    53. ?????*/?? public?static?final?void?addError(HttpServletRequest?request,ActionMessage?error) ??
    54. { ?? ????ActionMessages?errors=getErrors(request); ??
    55. ????errors.add("showError",?new?ActionMessage("showError",msgToString(request,error))); ?? ????saveErrors(request,errors); ??
    56. } ?? ????/** ?
    57. ?????*?保存弹出消息到Request,使用默认的key:showError? ? ?????*?@param?request ?
    58. ?????*?@param?key ? ?????*/??
    59. public?static?final?void?addError(HttpServletRequest?request,String?key) ?? { ??
    60. ????ActionMessages?errors=getErrors(request); ?? ????String?temp=key; ??
    61. ????temp=getMsgResources(request).getMessage(key); ?? ????if(temp!=null) ??
    62. ????????key=temp; ?? ????errors.add("showError",?new?ActionMessage("showError",key)); ??
    63. ????saveErrors(request,errors); ?? } ??
    64. public?static?final?void?addMsg(HttpServletRequest?request,String?key,ActionMessage?error) ?? { ??
    65. ????ActionMessages?errors=getErrors(request); ?? ????errors.add(key,?error); ??
    66. ????saveErrors(request,errors); ?? } ??
    67. public?static?final?void?addMsg(HttpServletRequest?request,String?key,String??value) ?? { ??
    68. ????ActionMessages?errors=getErrors(request); ?? ????errors.add(key,?new?ActionMessage("showMsg",value)); ??
    69. ????saveErrors(request,errors); ?? } ??
    70. public?static?final?String?filterString(String?s) ?? { ??
    71. ????if(s.indexOf("<")==-1) ?? ????????return?s; ??
    72. ????int?len=s.length(); ?? ????int?ps=s.indexOf("<"); ??
    73. ????int?pe=s.indexOf(">"); ?? ????s=s.substring(0,?ps)+s.substring(pe+1,s.length()); ??
    74. ????return?filterString(s);? ?? } ??
    75. //error.DBError??? 数据库错误提示public?static?final?void?addError(HttpServletRequest?request,Exception?e) ??
    76. { ?? ????ActionMessages?errors=getErrors(request); ??
    77. ????String?temp=getMsgResources(request).getMessage("error.DBError"); //数据库错误提示?? ????errors.add("showError",?new?ActionMessage("showError",temp+e.getMessage())); ??
    78. ????saveErrors(request,errors); ?? ????e.printStackTrace(); ??
    79. }??
    ApplicationResources.properties

????????????? showError= <script language="JavaScript">alert('{0}')</script>

?

?

??? 3.使用示例:

? ?????? Msg.addError(request, "你还没有办理公司××业务!");

???????? Msg.saveError(request, new ActionMessage("mccc.failmsg"));

???????? Msg.addError(request, new ActionMessage("mccc.lessmczh"));

?????? ? 显示所有的:? <html:errors/>

?

????????

???????? 显示指定的错误!

<html:errors property="showError"/>

?

?

?

?

??? 太累!没有详细注释,说明!见谅!?

1 楼 little fish 2008-05-14   这个太难,太复杂了,我就是想知道在jsp页面中要删除一条记录的时候怎么弹出对话框,应该不用写的这么复杂吧 2 楼 xiao_xgy 2008-09-25   高手啊,javaEye怎么会有这么多高手! 3 楼 wangjian3q 2008-11-02   为什么是乱码呀!
  请求帮助。 4 楼 anny5 2008-11-24   谢了谢了谢了谢了

热点排行