日期:2014-05-19  浏览次数:20640 次

关于struts2 的验证?????????
页面部分:
<%@ 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="test" validate="true">
  <s:textfield name="username"></s:textfield>
  <s:submit></s:submit>
</s:form>
</body>
</html>


TestAction.java

@Controller
public class TestAction extends ActionSupport{

private String username;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String add(){
return "add";
}

}

TestAction-validation.xml 部分

 <validators>
  <field name="username">
  <field-validator type="requiredstring">
  <message>名字为必填项</message>
  </field-validator>
  </field>
 </validators>

struts.xml配置部分
  <action name="test" class="testAction">
  <result name="add">/index.jsp</result>
  <result name="input">/test.jsp</result>
  </action>

为什么我提交的时候一直进入到index.jsp 不进行验证啊??????????


------解决方案--------------------
[code=Java][/code]<s:textfield name="username"></s:textfield>
是不是这里面少了点什么东西。
------解决方案--------------------
Java code


<s:form action="test_add" validate="true">
  <s:textfield name="username"></s:textfield>
  <s:submit></s:submit>
</s:form>
</body>
</html>

  <action name="test_*" class="testAction" method="{1}">
  <result name="add">/index.jsp</result>
  <result name="input">/test.jsp</result>
  </action>



public String add(){
return "add";
}
这样试试呢

------解决方案--------------------
把TestAction-validation.xml名字改成TestAction-add-validation.xml