在服务器端进行异常处理:
1.
各种类型的异常类:
public
class
ValidateException
extends
RuntimeException {
public
ValidateException() {}
public
ValidateException(String msg) {
super
(msg);
}
public
ValidateException(
int
msgId) {
super
();
}
}
public
class
CreateException
extends
RuntimeException {
public
CreateException() {}
public
CreateException(String msg) {
super
(msg);
}
public
CreateException(Throwable throwable) {
super
(throwable);
}
}
2.定义VO中的验证接口及方法:
public
interface
Validator {
static
final
int
INVALID_ACCOUNT_USERNAME
= 1000;
//"Invalid username: must 6-12 characters.";
static
final
int
INVALID_ACCOUNT_PASSWORD
= 1001;
static
final
int
APPLICATION_ERROR
= 909091;
void
validate()
throws
ValidateException;
}
public
class
Account
implements
Validator {
public
static
final
int
STATE_ACTIVE
= 0;
public
static
final
int
STATE_INACTIVE
= 1;
public
static
final
int
STATE_LOCKED
= 2;
// blogger login info:
private
int
accountId
;
private
String
username
;
private
String
password
;
private
String
email
=
""
;
private
Date
createdDate
;
// personal setting:
。。。。。。。。。。。。。。
public
void
validate()
throws
ValidateException {
if
(
username
==
null
||
username
.equals(
""
))
throw
new
ValidateException(
"
用户名不能为空
"
);
// if(!username.matches("[a-z0-9]{6,20}"))
// throw new ValidateException(INVALID_ACCOUNT_USERNAME);
if
(
password
==
null
||
password
.equals(
""
))
throw
new
ValidateException(
“密码不能为空”
);
}
}
3.sturts
中公共的处理类:
public
class
AppExceptionHandler
extends
ExceptionHandler {
/**
*
Logger
for
this
class
*/
private
static
final
Log
logger
= LogFactory
.getLog(AppExceptionHandler.
class
);
public
ActionForward execute(Exception ex,
ExceptionConfig ae,
ActionMapping mapping,
ActionForm formInstance,
HttpServletRequest request,
HttpServletResponse response)
throws
ServletException {
ActionForward forward =
null
;
if
(ae.getPath() !=
null
) {
forward =
new
ActionForward(ae.getPath());
}
else
{
forward = mapping.getInputForward();
}
String url = request.getRequestURL().toString();
String param = request.getQueryString();
if
(param!=
null
&& !param.equals(
""
))
url = url +
"?"
+ param;
String message =ex.getMessage();
request.setAttribute(
"url"
, url);
request.setAttribute(
"message"
, message);
return
forward;
}
}
4
.
Struts-config.xml
中的配置:
<
global-exceptions
>
<
exception
key
=
"error.sysError"
type
=
"java.lang.Exception"
handler
=
"com.yourcompany.struts.action.AppExceptionHandler"
path
=
"/error.jsp"
/>
</
global-exceptions
>
5
.
Error.jsp
页面内容:
<%@
page
contentType
=
"text/html;charset=GBK"
%>
<%@
taglib
uri
=
"/WEB-INF/c.tld"
prefix
=
"c"
%>
<!
DOCTYPE
HTML
PUBLIC
"-//w3c//dtd html 4.0 transitional//en"
>
<
html
>
<
head
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=iso-8859-1"
/>
<
title
>
ERROR
</
title
>
<
link
href
=
"normal.css"
rel
=
"stylesheet"
type
=
"text/css"
>
</
head
>
<
body
bgcolor
=
"#FFFFFF"
>
<
table
width
=
"100%"
height
=
"100%"
border
=
"0"
cellpadding
=
"0"
cellspacing
=
"0"
>
<
tr
>
<
td
align
=
"center"
><
table
width
=
"570"
border
=
"01"
cellpadding
=
"03"
cellspacing
=
"0"
bordercolor
=
"#A5B6D1"
>
<
tr
>
<
td
bgcolor
=
"#A5B6D1"
>
Error
</
td
>
</
tr
>
<
tr
>
<
td
bordercolor
=
"#FFFFFF"
>
错误信息
:
</
td
>
</
tr
>
<
tr
>
<
td
bordercolor
=
"#FFFFFF"
>
<
b
>
请求
URL
</
b
>
:
<
c:out
value
=
"${url}"/><br/>
<
b
>
错误原因
</
b
>
:
<
c:out
value
=
"${message}"/>
</
td
>
</
tr
>
<
tr
>
<
td
bordercolor
=
"#FFFFFF"
>
Please go back to the previous page and check your input.
Also you can contact the
<
a
href
=
"mailto:asklxf@163.com"
>
author
</
a
>
if you are sure this is a bug of the system. Thank you!
</
td
>
</
tr
>
</
table
></
td
>
</
tr
>
</
table
>
</
body
>
</
html
>
注意事项:
<%@ page errorPage="/common/errorpage.jsp" %>
表示如果是页面有错误就会转到
errorpage.jsp
中去。
同时
errorpage.jsp
中要声明
<%@ page isErrorPage="true" %>