Java <html:errors> struts 教程或示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/937584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
<html:errors> struts tutorial or example
提问by Tere
I'm trying to make a login page in Struts. The idea is to validate if the user exists, etc, and then if there is an error, return to the login page with the errors in red (the typical login or any form page validation).
我正在尝试在 Struts 中创建一个登录页面。这个想法是验证用户是否存在等,然后如果有错误,返回登录页面,错误显示为红色(典型登录或任何表单页面验证)。
I would like to know if someone knows an errors management tutorial in Struts. I'm looking specially for a tutorial (or example) of the
我想知道是否有人知道 Struts 中的错误管理教程。我正在寻找一个教程(或示例)
<html:errors>
tag, which I think would solve my problem.
标签,我认为这可以解决我的问题。
采纳答案by Mark Lutton
Here's a quick summary. You have an ActionForm
class, say MyForm
:
这是一个快速总结。你有一ActionForm
堂课,说MyForm
:
<form-bean name="myForm" type="myapp.forms.MyForm"/>
You have an Action
class, say MyAction
:
你有一Action
堂课,说MyAction
:
<action path="/insert" type="myapp.actions.MyAction" name="myForm"
input="/insert.jsp" validate="true" />
<forward name="success" path="/insertDone.jsp"/>
</action>
"name" in the action refers to "name" in the form-bean. Because you have validate="true"
your ActionForm
class MyForm
must define validate()
method which will automatically be called:
动作中的“name”指的是form-bean中的“name”。因为您validate="true"
的ActionForm
类MyForm
必须定义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;
}
If it returns an empty ActionErrors object, Struts goes on to call your MyAction.execute(). Otherwise, Struts displays /insert.jsp (because that's the input= parm you gave) and expands the html.errors tag to display your errors from ActionErrors.
如果它返回一个空的 ActionErrors 对象,Struts 将继续调用您的 MyAction.execute()。否则,Struts 会显示 /insert.jsp(因为这是您提供的 input=parm)并扩展 html.errors 标记以显示您在 ActionErrors 中的错误。
回答by Mark Lutton
Here's one: //struts.apache.org/1.3.5/struts-taglib/apidocs/org/apache/struts/taglib/html/package-summary.html#package_description
这是一个://struts.apache.org/1.3.5/struts-taglib/apidocs/org/apache/struts/taglib/html/package-summary.html#package_description
Here I'm assuming Struts 1. I don't know if it has changed for Struts 2.
这里我假设的是 Struts 1,不知道 Struts 2 有没有变化。
You can put an errors.header and errors.footer into your message resources file:
您可以将 errors.header 和 errors.footer 放入消息资源文件中:
errors.header=<h3><font color="red">Errors:</font></h3><ul>
errors.footer=</ul>
The header and footer are displayed only if the ActionErrors object has any errors in it.
仅当 ActionErrors 对象中有任何错误时才会显示页眉和页脚。
In your Action class, do this:
在您的 Action 类中,执行以下操作:
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
}
Then before returning:
然后在返回之前:
saveErrors(request, errors);
In your messages resource file:
在您的消息资源文件中:
error.bad.input=<li>Bad input: '{0}' is invalid.</li>
Now when the <html:errors/>
tag is processed, it will turn into:
现在当<html:errors/>
标签被处理时,它会变成:
<h3><font color="red">Errors:</font></h3><ul>
<li>Bad input: 'xxyyzzz' is invalid.<li>
</ul>
回答by JavaEE guy
In struts action:
在struts动作中:
ActionErrors errors = new ActionErrors();
errors.add("", new ActionMessage("login.msg.err"));
saveErrors(request, errors);
The "hello.msg.err" is define in MessageBundle.properties
“hello.msg.err”在 MessageBundle.properties 中定义
login.msg.err=Wrong user name or password
In you JSP, error will be shown by h:errors tag:
在您的 JSP 中,错误将通过 h:errors 标记显示:
<h:errors/>
This video shows you step by step & explain you all those things: https://youtu.be/YcCsJtqI72A
该视频将逐步向您展示并向您解释所有这些内容:https: //youtu.be/YcCsJtqI72A