`

struts中如何处理异常和消息

阅读更多
 
在struts中的国际化如何处理? JSP页面上直接<bean:message key="login.title" /> 解决国际化显示的问题
ActionErrors errors = new ActionErrors() ;
if (this.username != null && this.username.length() < 4 ) {
    errors.add("username", new ActionMessage("username.tooshort",new Object[]{username,"x","xxx"})) ; ;
}
在ActionErrors中的错误消息在JSP页面上用
<html:errors property="username" />
显示

ActionMessages中的消息如何在JSP页面上显示?
 
  ActionMessages messages = new ActionMessages() ;
		String forward = "fail" ;
		if ("admin".equalsIgnoreCase(uaf.getUsername()) && "admin".equalsIgnoreCase(uaf.getPassword())) {
			messages.add("success", new ActionMessage("login.success",uaf.getUsername())) ;
			
			forward = "success" ;
		
			this.addMessages(request, messages) ;
		}else{
			messages.add("fail", new ActionMessage("login.fail",uaf.getUsername())) ;
			this.addMessages(request, messages) ;
		}
ActionMessages中的信息在JSP页面上用下面的标签来显示:
<html:messages id="msg" message="true" property="success" >
       		<bean:write name="msg" />
       </html:messages>

自定义的异常在struts中如何处理?
自定义一个异常类,在处理业务逻辑中一直往上抛,在struts中统一管理
     struts.cfg.xml中配置如下
   
  <global-exceptions>
    	<exception type="com.cs.util.SystemException" key="system.error" path="/common/exception.jsp" />
      </global-exceptions>
     所有的forword,只要没有跳转,就统一跳到这个全局异常页面 
     在exception.jsp页面中用<html:errors />即可得到所有的异常信息 
     注意:在资源文件中配置:system.error  {0} ,对应上面的key值(内部处理:有错误就把错误放到{0}中去) 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics