我正在尝试在Struts中制作登录页面。想法是验证用户是否存在等,如果出现错误,则返回带有红色错误的登录页面(典型的登录或任何表单页面验证)。我想知道是否有人知道Struts中的错误管理教程。我特别寻找一个教程(或示例)。
<html:errors>
我认为可以使用标签来解决我的问题。
<html:errors>
我认为可以使用标签来解决我的问题。
errors.header=<h3><font color="red">Errors:</font></h3><ul>
errors.footer=</ul>
ActionErrors errors = new ActionErrors();
if (badInput) {
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError("error.bad.input", badString); // key in messages resource file
// badString will replace {0} in message
}
saveErrors(request, errors);
error.bad.input=<li>Bad input: '{0}' is invalid.</li>
<html:errors/>
标签时,它将被转换为:<h3><font color="red">Errors:</font></h3><ul>
<li>Bad input: 'xxyyzzz' is invalid.<li>
</ul>
这是一个快速摘要。你有一个ActionForm
类,例如MyForm
:
<form-bean name="myForm" type="myapp.forms.MyForm"/>
你有一个叫做MyAction
的Action
类:You have an Action
class, say MyAction
:
<action path="/insert" type="myapp.actions.MyAction" name="myForm"
input="/insert.jsp" validate="true" />
<forward name="success" path="/insertDone.jsp"/>
</action>
在这里,"name"指的是表单bean中的"name"。因为您在ActionForm
类MyForm
上设置了validate="true"
,所以您必须定义一个validate()
方法,该方法将被自动调用:
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if ((username==null) || (username.length() < 1))
errors.add("username", new ActionError("error.username.required"));
return errors;
}
如果返回一个空的ActionErrors对象,Struts会继续调用MyAction.execute()。否则,Struts会显示/insert.jsp(因为这是您给出的input=参数),并扩展html.errors标签以显示来自ActionErrors的错误。ActionErrors errors = new ActionErrors();
errors.add("", new ActionMessage("login.msg.err"));
saveErrors(request, errors);
"hello.msg.err"被定义在MessageBundle.properties中。
"login.msg.err=Wrong user name or password
<h:errors/>