Struts 之 手动验证
服务器端的验证
一、写在Action的execute之中
二、写在Form的validate函数之中
1、在LoginForm.java中修改validate函数
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.amaker.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
/**
* MyEclipse Struts Creation date: 03-09-2015
*
* XDoclet definition:
*
* @struts.form name="loginForm"
*/
public class LoginForm extends ActionForm {
/*
* Generated fields
*/
/** username property */
private String username;
/** password property */
private String password;
/*
* Generated Methods
*/
/**
* Method validate
*
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
ActionErrors errors = new ActionErrors();
if (username == null || username.trim().equals("")) {
// ActionError actionerror = new ActionError("username", new
// ActionMessage("login.username"));
errors.add("username", new ActionMessage("login.username"));
return errors;
// ActionMessage
}
return null;
}
/**
* Method reset
*
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
/**
* Returns the username.
*
* @return String
*/
public String getUsername() {
return username;
}
/**
* Set the username.
*
* @param username
* The username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* Returns the password.
*
* @return String
*/
public String getPassword() {
return password;
}
/**
* Set the password.
*
* @param password
* The password to set
*/
public void setPassword(String password) {
this.password = password;
}
}
2、修改ApplicationResources.properties文件,里面添加配置显示错误信息
# Resources for parameter 'com.amaker.struts.ApplicationResources'
# Project Java_Struts_04
login.username=username is required!
学习_Java_Base_Struts_手动验证之Struts框架实现_02
最新推荐文章于 2022-03-13 16:46:02 发布