错误信息包括两种,一种是异常信息的显示,一种是后台验证信息显示。而且显示又分直接页面跳转的提交,和通过Ajax的提交。 先看,异常信息与直接页面跳转的例子。在例子中,运用play框架的catch注解标签统一处理: 自定义异常类 app.models.MyException:
package models; public class MyException extends Exception{ public MyException(Exception e) { super(e); } public MyException(String msg){ super(msg); } public MyException(String msg, Exception e) { super(msg,e); } }
控制器父类 app.controllers.Application.java中的拦截方法,这里处理了html请求与ajax请求
@Catch(MyException.class) public static void ExceptionCatcher(MyException e){ String accept= request.headers.get("accept").toString(); String s[]=accept.split(","); String msg = e.getMessage(); if (s[0].equalsIgnoreCase("[text/html")){ render("errors/550.html",msg); } else if (s[0].equalsIgnoreCase("[application/json")){ Validation.clear(); //有了异常只显示异常信息,清空之前的validation数据 Validation.addError("ServerError" ,msg); Response.current().status = 550; renderText(JSONUtil.parseObject(Validation.errors())); } else{ render("errors/550.html",msg); } }
制造一个简单的异常 app.controllers.CalcAction.java:
package controllers; import models.MyException; public class CalcAction extends Application{ public static void zero() throws MyException{ try{ int ka = 2/0; }catch(Exception e){ throw new MyException(e); } } }
routes中路由节点
GET /zero CalcAction.zero
错误信息的i18n对应文档,conf/messages
# Validation messages validation.required=%s不能为空 validation.minSize=%s长度必须大于%d位 validation.maxSize=%s长度不能超过%d位 validation.range=%s必须在 %d和 %d之间 validation.equals=%s必须一致 validation.email=电子邮箱(Email)格式不正确 validation.phone=手机号格式不正确 validation.isTrue=%s must be checked validation.match=%s格式不正确 validation.min=%s必须大于%d validation.invalid=%s输入无效 validation.unique=%s已存在 serverError=服务器异常,请联系管理员
错误显示页面 views/errors/550.html
Application error <h1>Oops, an error occured</h1> This exception has been logged with id <strong>${msg}</strong>.
显示结果: 然后是后台的验证信息与ajax方式提交的显示,这种情况在数据校验的时候常用。统一校验已经在系列前文中提到,这里就直接上代码了,重在显示验证错误信息的显示。 使用的models,其中包含了验证字段,app.models.User.java:
package models; import java.util.Date; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import play.data.validation.Email; import play.data.validation.MinSize; import play.data.validation.Required; import play.db.jpa.GenericModel; @Entity @Table(name="user") public class User extends GenericModel { @Id public long id; @Required public String code; @MinSize(value = 4) public String name; public String pwd; public int sex; @Email public String email; public String regip; public Date regtime; }
控制器父类 app.controllers.Application.java中的拦截方法
@Catch(MyException.class) @Before public static void validationIntercept() { if(validation.hasErrors()){ response.status = 550; renderText(JSONUtil.parseObject(validation.errors())); } }
routes路由
POST /user UserAction.save
添加用户的界面veiw/Application/reg.html,这里为了方便,将html与javascript代码写在一起了。通过引入#{i18n /}可以直接调用i18n函数,进行国际化。
#{extends 'main.html' /} #{set title:'Home' /} #{i18n /} <script type="text/javascript"> var submitform = function(){ $.ajax({ url: "/user", type: "POST", dataType: "json", data: $("#myform").serialize(), success: function(data){ window.location.href="/user/"+data; }, error: function(XMLHttpRequest, textStatus, errorThrown){ if (XMLHttpRequest.status == 550) { var exp = JSON.parse(XMLHttpRequest.responseText); showMsg(exp); } } }); } var showMsg = function(exp,obj){ for(var i=exp.length-1;i>=0;i--){ var object=$('#myform input[name="'+exp[i].key+'"]'); if(exp[i].key!="ServerError"){ object.parent().next().css('color','red'); var error=object.prev().html(); object.parent().next().html(i18n(exp[i].message,error,exp[i].variables[0],exp[i].variables[1])); if(i==0){ object.focus(); } }else{ showMsg(i18n(exp[i].message, exp[i].key)); } } } </script> <body> <div style="padding-left:20px;padding-top: 20px;"> <form action="/user" method="post" id="myform"><div> <label>用户名:</label><input type="text" id="code" name="user.code"/></div><span></span><br/> <div><label>昵称:</label><input type="text" id="name" name="user.name"/></div><span></span><br/> <div><label>密码:</label><input type="password" id="pwdid" name="user.pwd"/></div><span></span><br/> <div><label>邮箱:</label><input type="text" id="email" name="user.email"/></div><span></span><br /> <div><label>性别:</label><label><input type="radio" name="user.sex" value="1"checked="checked">男</label> <label><input type="radio" name="user.sex" value="0">女</label></div><span></span> <input type="button" value="submit" onclick="submitform()"> </form> </div> </body>