struts2 的validate()方法不起作用
我用struts2 里面的validate()验证,不起作用,而且我感觉页面连action也没有进去,可是struts.xml里面配置应该都是对的啊!真的不知道是什么原因了,希望大家多多指教!
validate.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>手动校验数据</title>
</head>
<body>
<center>
<b>用户登录</b>
<p>
<s:fielderror/>
<s:form name="validate" method="post">
<s:textfield label="姓名" name="name"/>
<s:textfield label="密码" name="pass"/>
<s:submit value="提交"/>
</s:form>
</center>
</body>
</html>
ValidateAction.action:
package tmp.action;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class ValidateAction extends ActionSupport{
private String name;
private String pass;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String execute(){
return SUCCESS;
}
public void validate(){
System.out.println("validate()方法执行");
/* if(name() == null || name().length()<6 || name().length()>15){
add
FieldError("name","用户姓名的长度不符合要求");
}
if(pass() == null || pass().length()<8 || pass().length()>20){
addFieldError("pass","用户密码的长度不符合要求");
}*/
if(name == null){
addFieldError("name","用户名不能为空");
}
}
}
struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.i18n.encoding" value="gb2312"/>
<constant name="struts.custom.i18n.resources" value="globalMessages"/>
&