在struts 2的validation.xml文件中未显示错误消息

时间:2018-09-21 08:04:47

标签: struts2

我有结果输入,并根据struts.xml中提到的名称和操作将validation.xml命名为AdminLoginAction-AdminLogin-validation.xml。将validation.xml与操作类放在同一程序包中,但没有任何显示为错误消息的内容,并且请求通过并到达成功页面

我的login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form action="AdminLogin" method="post">
<s:label value="Admin Login" />
<s:textfield name="adminform.username" label="Enter User Name" /> 
<s:password name="adminform.password" label="Enter Password" />
<s:submit value="submit"/>
</s:form>
</body>
</html>

我的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.devmode" value="true"></constant>

       <package name="login"  extends="struts-default">

       <action name="AdminLogin" class="loginactionclasses.AdminLoginAction" method="execute">
       <result name="input">login.jsp</result>
       <result name="success">success.jsp</result>
       </action>

       </package>
   </struts>

动作类

package loginactionclasses;



import com.opensymphony.xwork2.ActionSupport;

import formclasses.AdminForm;

public class AdminLoginAction extends ActionSupport {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public AdminForm adminform;




    public String execute()
    {
         System.out.println(adminform.getUsername());
        return "success";

    }






    public AdminForm getAdminform() {
        return adminform;
    }


    public void setAdminform(AdminForm adminform) {
        this.adminform = adminform;
    }


}

validation.xml

<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
        "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">

<validators>
  <field name="adminform.username">
      <field-validator type="required">
          <message>You must enter a value for username</message>
      </field-validator>
  </field>
</validators>

1 个答案:

答案 0 :(得分:0)

您的验证将不起作用,因为您在验证String时使用<field-validator type="required">,那么您应该使用<field-validator type="requiredstring">

字段验证器类型为必填和必填字符串:

  • 必需检查字段是否为空。查看更多:required
  • requiredstring检查字段是否为非空且长度>0。查看更多:requiredstring