商务合作:179001057@qq.com

Struts 1.3.8 Action實現類中消息的輸出及JSP頁面中消息標簽的使用總結

技术2022-05-12  0


某平台价值19860元的编程课程资料免费领取【点我领取】


(1) Action中保存消息及錯誤的簡單實現: …… //此處是ActionMessages是帶’s’的類名 ActionMessages messages = new ActionMessages(); //向messages添加一個消息對象,并設定property為“sucess” messages.add(“sucess”, new ActionMessage(“source.key”)); //將消息集合保存到request中,并以“org.apache.struts.action.ACTION_MESSAGE”為關鍵字;此方法可在下面看到Action類中實現的源代碼 saveMessages(request,messages); //將消息集合保存到request中,并以“org.apache.struts.action.ERROR”為關鍵字;此方法可在下面看到Action類中實現的源代碼 saveErrors(request,messages); …… (2) Struts 1.3.8中的Action中保存消息和錯誤的源代碼如下: protected void addMessages(HttpServletRequest request, ActionMessages messages) { if (messages == null) return; ActionMessages requestMessages = (ActionMessages)request.getAttribute("org.apache.struts.action.ACTION_MESSAGE"); if (requestMessages == null) requestMessages = new ActionMessages(); requestMessages.add(messages); if (requestMessages.isEmpty()) { request.removeAttribute("org.apache.struts.action.ACTION_MESSAGE"); return; } else { request.setAttribute("org.apache.struts.action.ACTION_MESSAGE", requestMessages); return; } } protected void addErrors(HttpServletRequest request, ActionMessages errors) { if (errors == null) return; ActionMessages requestErrors = (ActionMessages)request.getAttribute("org.apache.struts.action.ERROR"); if (requestErrors == null) requestErrors = new ActionMessages(); requestErrors.add(errors); if (requestErrors.isEmpty()) { request.removeAttribute("org.apache.struts.action.ERROR"); return; } else { request.setAttribute("org.apache.struts.action.ERROR", requestErrors); return; } } (3) JSP頁面中用 和 標簽輸出: …… …… MessagesTag的實現類構造如下: public MessagesTag() { iterator = null; processed = false; id = null; bundle = null; locale = "org.apache.struts.action.LOCALE"; name = "org.apache.struts.action.ERROR"; property = null; header = null; footer = null; message = null; }

最新回复(0)