Struts 表单不验证问题 谢谢 !!

pittzhao 2004-08-21 03:55:10
jsp提交之后不验证 好像根本没有到 ActionForm 大家看看

这是jsp
--------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>



<html:html>
<HEAD>

<%@ page
language="java"

contentType="text/html; charset=GB2312"

pageEncoding="GB2312"

%>


<META http-equiv="Content-Type" content="text/html; charset=GB2312">
<META name="GENERATOR" content="IBM WebSphere Studio">

<META http-equiv="Content-Style-Type" content="text/css">
<LINK href="../../../theme/Master.css" rel="stylesheet"
type="text/css">
<TITLE>Flow_add.jsp</TITLE>
</HEAD>

<BODY>

<html:form action="/flow">
<TABLE border="0">
<TBODY>

<TR>
<TH>seqno</TH>
<TD><html:text property="seqno" /></TD>
</TR>

<TR>
<TH>flowname</TH>
<TD>
<html:text property="flowname" />
</TD>
</TR>

<TR>
<TH>flowmemo</TH>
<TD><html:text property="flowmemo" /></TD>
</TR>

<TR>
<TH>validflag</TH>
<TD><html:text property="validflag" /></TD>
</TR>
<TR>
<TD>
<html:submit property="submit" value="Submit" />
</TD>
<TD><html:reset /></TD>
</TR>
</TBODY>
</TABLE>
</html:form>
</BODY>
</html:html>

-------------------

这是ActionForm

---------------------
package com.itown.decl.struts.forms;

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;

/**
* Form bean for a Struts application.
* Users may access 4 fields on this form:
* <ul>
* <li>validflag - [your comment here]
* <li>flowmemo - [your comment here]
* <li>flowname - [your comment here]
* <li>seqno - [your comment here]
* </ul>
* @version 1.0
* @author
*/
public class FlowActionForm extends ActionForm {



private String flowmemo = null;
private String validflag = null;
private String flowname = null;
private String seqno = null;

//System.out.println("--1->");
/**
* Get flowmemo
* @return String
*/
public String getFlowmemo() {
return flowmemo;
}

/**
* Set flowmemo
* @param <code>String</code>
*/
public void setFlowmemo(String f) {
this.flowmemo = f;
}

/**
* Get validflag
* @return String
*/
public String getValidflag() {
return validflag;
}

/**
* Set validflag
* @param <code>String</code>
*/
public void setValidflag(String v) {
this.validflag = v;
}

/**
* Get flowname
* @return String
*/
public String getFlowname() {
return flowname;
}

/**
* Set flowname
* @param <code>String</code>
*/
public void setFlowname(String f) {
this.flowname = f;
}

/**
* Get seqno
* @return String
*/
public String getSeqno() {
return seqno;
}

/**
* Set seqno
* @param <code>String</code>
*/
public void setSeqno(String s) {
this.seqno = s;
}

public void reset(ActionMapping mapping, HttpServletRequest request) {

// Reset values are provided as samples only. Change as appropriate.

flowmemo = null;
validflag = null;
flowname = null;
seqno = null;
System.out.println("in reset");

}

public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) {

ActionErrors errors = new ActionErrors();
System.out.println("in validate");
// Validate the fields in your form, adding
// adding each error to this.errors as found, e.g.

// if ((field == null) || (field.length() == 0)) {
// errors.add("field", new org.apache.struts.action.ActionError("error.field.required"));
// }
// 输入数据校验!
if(flowname == null || flowname.length() == 0){
errors.add("flowname", new org.apache.struts.action.ActionError("error.login.nameerror!"));
System.out.println("in errors");
}
if(validflag == null || validflag.length() == 0){
errors.add("validflag", new org.apache.struts.action.ActionError("不能为空!"));
System.out.println("in errors");
}
if(seqno == null || seqno.length() == 0){
errors.add("seqno", new org.apache.struts.action.ActionError("不能为空!"));
System.out.println("in errors");
}
if(flowmemo == null || flowmemo.length() == 0){
errors.add("flowmemo", new org.apache.struts.action.ActionError("不能为空!"));
System.out.println("in errors");
}
return errors;

}
}

-------------------------
这是config.xml
---------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>

<!-- 数据源 -->
<data-sources>
</data-sources>

<!-- 表单 Bean -->

<!-- 全局异常 -->
<form-beans>
<form-bean name="SampleActionForm" type="com.itown.struts.forms.SampleActionForm">
</form-bean>
<form-bean name="FlowActionForm" type="com.itown.decl.struts.forms.FlowActionForm">
</form-bean>
</form-beans>
<global-exceptions>
</global-exceptions>

<!-- 全局转发 -->
<global-forwards>
</global-forwards>

<!-- 操作映射 -->

<!-- 消息资源 -->
<action-mappings>
<action name="SampleActionForm" path="/sample" scope="request" type="com.itown.struts.actions.SampleAction" input="sample.jsp">
</action>
<action name="FlowActionForm" path="/flow" scope="request" type="com.itown.decl.struts.actions.FlowAction" input="Flow_add.jsp" validate="true">
</action>
</action-mappings>

</struts-config>

---------------------
...全文
105 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
niyboy 2004-08-23
  • 打赏
  • 举报
回复

1,可以用validate来验证;

2,可以通过javascript来验证

这两种方法都行
ybsoft 2004-08-23
  • 打赏
  • 举报
回复
在CONFIG.XML里面将ACTIONMAPPING标签的VALIDATE属性设置为TRUE
shangqiao 2004-08-22
  • 打赏
  • 举报
回复
“好像根本没有到 ActionForm”
为什么是“好像”,你完全可以使用log工具跟踪,看看到底是不是进入了方法
shangqiao 2004-08-22
  • 打赏
  • 举报
回复
将validate设为true试试

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧