弹出异常对话框alert('error')-struts
弹出错误对话框alert(error)--struts唉!很累!就把代码贴出来!希望对初学者有所帮助!? 简要说明:addError
弹出错误对话框alert('error')--struts
唉!很累!就把代码贴出来!希望对初学者有所帮助!?
简要说明:addError方法用于添加要弹出的消息或错误!
??????????????????? addMsg方法用于添加消息或错误!
Java代码
java 代码- package?com.dotease.common; ?? ??
- import?javax.servlet.http.HttpServletRequest; ?? ??
- import?org.apache.struts.Globals; ?? import?org.apache.struts.action.ActionErrors; ??
- import?org.apache.struts.action.ActionMessage; ?? import?org.apache.struts.action.Action; ??
- import?org.apache.struts.action.ActionMessages; ?? import?org.apache.struts.util.MessageResources; ??
- import?java.util.ArrayList; ?? /** ?
- ?*?@author?Administrato?2007-10-9 ? ?*2007-10-9 ?
- ?*/?? public?class?Msg?{ ??
- ?? ????/** ?
- ?????*?save?the?error?messages!?This?like?Action.saveErrors();? ? ?????*?@param?request ?
- ?????*?@param?errors ? ?????*/??
- ????protected??static?final?void?saveErrors(HttpServletRequest?request,ActionMessages?errors)?{ ?? ??
- ????????if?((errors?==?null)?||?errors.isEmpty())?{ ?? ????????????request.removeAttribute(Globals.ERROR_KEY); ??
- ????????????return; ?? ????????} ??
- ????????//?Save?the?error?messages?we?need ?? ????????request.setAttribute(Globals.ERROR_KEY,?errors); ??
- ???????? ?? ????} ??
- ???? ?? ????/** ?
- ?????*?保存弹出消息到Request,使用默认的key:showError? ? ?????*?此信息在前台设置后,弹出! ?
- ?????*?建议: 放在紧挨着的地方! ? ?????*?适合有错误,就返回!?如果多次调用,会覆盖上次的Errors ?
- ?????*?@param?request?? ? ?????*?@param?ActionMessage???? ?
- ?????*? ? ?????*/??
- public?static?final?void?saveError(HttpServletRequest?request,ActionMessage?error)?{ ?? ???????? ??
- ????????MessageResources?msgResource?=?((MessageResources)?request.getAttribute(Globals.MESSAGES_KEY)); ?? ????????String?msg=msgResource.getMessage(error.getKey()); ??
- ????????int?length=0; ?? ????????if(error.getValues()!=null) ??
- ????????????length=error.getValues().length; ?? ????????for(int?i=0;i
- ????????{ ?? ????????????msg=msg.replace("{"+i+"}",?error.getValues()[i].toString()); ??
- ???????????? ?? ????????} ??
- ????????ActionMessages?errors?=?new?ActionMessages(); ?? ????????errors.add("showError",new?ActionMessage("showError",msg)); ??
- ????????saveErrors(request,errors);? ?? ????} ??
- ?? ????/** ?
- ?????*?把ActionMessage对象转化为文本字符串 ? ?????*?@param?request ?
- ?????*?@param?msg ? ?????*?@return ?
- ?????*/?? public?static?final?String?msgToString(HttpServletRequest?request,ActionMessage?error)? ??
- { ?? ????MessageResources?msgResource?=?((MessageResources)?request.getAttribute(Globals.MESSAGES_KEY)); ??
- ????String?msg=msgResource.getMessage(error.getKey()); ?? ????msg=filterString(msg); ??
- ????int?length=0; ?? ????if(error.getValues()!=null) ??
- ????????length=error.getValues().length; ?? ????for(int?i=0;i
- ????{ ?? ????????msg=msg.replace("{"+i+"}",?error.getValues()[i].toString()); ??
- ???????? ?? ????} ??
- ????return?msg; ?? } ??
- ????/** ? ?????*?返回MessageResources对象,通过它可以获得配置资源 ?
- ?????*?@param?request ? ?????*?@return ?
- ?????*/?? public?static?final?MessageResources?getMsgResources(HttpServletRequest?request)? ??
- {??? ?? ????return?((MessageResources)?request.getAttribute(Globals.MESSAGES_KEY));? ??
- } ?? ??
- ?? ??
- protected?static?final?ActionMessages?getErrors(HttpServletRequest?request) ?? { ??
- ????ActionMessages?errors?=?(ActionMessages)request.getAttribute(Globals.ERROR_KEY); ?? ????if(errors?==?null) ??
- ????????errors?=?new?ActionMessages(); ?? ????return?errors; ??
- ???? ?? } ??
- ????/** ? ?????*?保存弹出消息到Request,使用默认的key:showError? ?
- ?????*?@param?request ? ?????*?@param?error ?
- ?????*/?? public?static?final?void?addError(HttpServletRequest?request,ActionMessage?error) ??
- { ?? ????ActionMessages?errors=getErrors(request); ??
- ????errors.add("showError",?new?ActionMessage("showError",msgToString(request,error))); ?? ????saveErrors(request,errors); ??
- } ?? ????/** ?
- ?????*?保存弹出消息到Request,使用默认的key:showError? ? ?????*?@param?request ?
- ?????*?@param?key ? ?????*/??
- public?static?final?void?addError(HttpServletRequest?request,String?key) ?? { ??
- ????ActionMessages?errors=getErrors(request); ?? ????String?temp=key; ??
- ????temp=getMsgResources(request).getMessage(key); ?? ????if(temp!=null) ??
- ????????key=temp; ?? ????errors.add("showError",?new?ActionMessage("showError",key)); ??
- ????saveErrors(request,errors); ?? } ??
- public?static?final?void?addMsg(HttpServletRequest?request,String?key,ActionMessage?error) ?? { ??
- ????ActionMessages?errors=getErrors(request); ?? ????errors.add(key,?error); ??
- ????saveErrors(request,errors); ?? } ??
- public?static?final?void?addMsg(HttpServletRequest?request,String?key,String??value) ?? { ??
- ????ActionMessages?errors=getErrors(request); ?? ????errors.add(key,?new?ActionMessage("showMsg",value)); ??
- ????saveErrors(request,errors); ?? } ??
- public?static?final?String?filterString(String?s) ?? { ??
- ????if(s.indexOf("<")==-1) ?? ????????return?s; ??
- ????int?len=s.length(); ?? ????int?ps=s.indexOf("<"); ??
- ????int?pe=s.indexOf(">"); ?? ????s=s.substring(0,?ps)+s.substring(pe+1,s.length()); ??
- ????return?filterString(s);? ?? } ??
- //error.DBError??? 数据库错误提示public?static?final?void?addError(HttpServletRequest?request,Exception?e) ??
- { ?? ????ActionMessages?errors=getErrors(request); ??
- ????String?temp=getMsgResources(request).getMessage("error.DBError"); //数据库错误提示?? ????errors.add("showError",?new?ActionMessage("showError",temp+e.getMessage())); ??
- ????saveErrors(request,errors); ?? ????e.printStackTrace(); ??
- }??
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 谢了谢了谢了谢了