为什么我的struts2验证框架错误和成功都跳向input视图
2个回答
展开全部
主要代码如下:
NewValidateAction-validation.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="msg">
<field-validator type="requiredstring">
<message>请输入信息</message>
</field-validator>
</field>
<field name="age">
<field-validator type="int">
<param name="min">13</param>
<param name="max">20</param>
<message>必须在13至20之间</message>
</field-validator>
</field>
</validators>
struts.xml配置:(class="validate“ 是用了spring依赖注入)
<action name="newvalidate" class="validate">
<result name="input">/validate_form.jsp</result>
<result name="two">/two.jsp</result>
</action>
validate_form.jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>验证数据</title>
</head>
<body>
<s:form action="newvalidate" validate="true">
<s:textfield name="msg" label="姓名"></s:textfield>
<s:textfield name="age" label="年龄"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>
NewValidateAction.java代码:
package action;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class NewValidateAction extends ActionSupport {
private String msg;
private int age;
public String execute(){
System.out.println("没有执行?");
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("err", "it is ok!!!!");
return "two";
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
首页,我不输入信息,也就是验证没通过,会跳到input视图,也就是validate_form.jsp;
int 改成Integer
或者在validate_form.jsp加入如下句子<s:fielderror></s:fielderror>
NewValidateAction-validation.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="msg">
<field-validator type="requiredstring">
<message>请输入信息</message>
</field-validator>
</field>
<field name="age">
<field-validator type="int">
<param name="min">13</param>
<param name="max">20</param>
<message>必须在13至20之间</message>
</field-validator>
</field>
</validators>
struts.xml配置:(class="validate“ 是用了spring依赖注入)
<action name="newvalidate" class="validate">
<result name="input">/validate_form.jsp</result>
<result name="two">/two.jsp</result>
</action>
validate_form.jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>验证数据</title>
</head>
<body>
<s:form action="newvalidate" validate="true">
<s:textfield name="msg" label="姓名"></s:textfield>
<s:textfield name="age" label="年龄"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>
NewValidateAction.java代码:
package action;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class NewValidateAction extends ActionSupport {
private String msg;
private int age;
public String execute(){
System.out.println("没有执行?");
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("err", "it is ok!!!!");
return "two";
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
首页,我不输入信息,也就是验证没通过,会跳到input视图,也就是validate_form.jsp;
int 改成Integer
或者在validate_form.jsp加入如下句子<s:fielderror></s:fielderror>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询