Struts3_2.txt

来源:互联网 发布:网络信息平台 编辑:程序博客网 时间:2024/04/23 15:06

html表单就像多情的浪子,四处留情却不负责任。提交了数据之后,表单就不再管怎么处理了,这些累活自然也得有人干啊,于是LogonForm出场了。
public String getPassword() {
return (this.password);
}
/**
* Set the password.
*
* @param password The new password
*/
public void setPassword(String password) {
this.password = password;
}
/**
* Return the username.
*/
public String getUsername() {
return (this.username);
}
/**
* Set the username.
*
* @param username The new username
*/
public void setUsername(String username) {
this.username = username;
}
他总体上是一个bean,同时个还有两个方法,reset和vilidate。
当actionFrom作为工作流的一部分,他是很有用的,但是如果mapping限制了范围例如限制在请求作用域中,就没有必要了。
当 mapping 设置为 validate=true,
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"));
if ((password == null) || (password.length() < 1))
errors.add("password",
new ActionError("error.password.required"));
return errors;
}

validate 返回的ActionErrors 对象是另一个框架类。如果返回的不是null,那就是验证有错误了,<html:errors> 就知道出错了,把错误信息写出来。
<html:errors property="userName"/>
<html:errors property="userPass"/>
各验证各的,返回也不一样。
记号 error.username.required 和 error.password.required 也都是关键字。
它们用于从Struts 消息资源文件中查找要显示的实际消息。每个场所可以有其自己的资源
文件, 这使得消息容易被本地化。
Struts 消息资源文件也是用名-值对格式。我们所用的消息条目是:
error.username.required=<li>Username is required</li>
error.password.required=<li>Password is required</li>

原创粉丝点击