Java论坛网»Java技术»急,struts Formbean 验证问题

急,struts Formbean 验证问题

问?:
比如在提交表单时,有些字段是必填字段,我在formbean的验证失败时,在返回原提交表单窗口时
以填的数据就没了,要想保存填的数据怎么解决?
答!: 1:
request
答!: 2:
那我建议你就用javascript算了
或者 用 Ajax技术
答!: 3:
javascript 那是客户端验证,
主要我不是达到效果,是想知道有没有好的解决方法
请高手指点
答!: 4:
struts不可能出现这种情况吧!我觉的可能是你写的问题。贴出代码看看。
答!: 5:
用js的 validate.js这个验证框架,非常好使.就是你要的那种效果
答!: 6:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<jsp:include flush="false" page="../web/session.jsp"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
<link href="../css/style.css" rel="stylesheet" type="text/css">
</head>

<body>
${requestScope.message}
<table width="85%" align="center" cellspacing="1" bgcolor="#CEDDFF">
<tr align="left" bgcolor="#FFFFFF">
<td colspan="4"><font color="#FF0000">请认真填写公司基本信息:(带*是必填)</font></td>
</tr>
<html:form action="/cus/cusinfo.do?method=add" >
<tr bgcolor="#FFFFFF">
<td height="27" align="center">公司名称</td>
<td height="28">
<input name="cusName" type="text" id="cusName">
* <font color="#FF0000"><html:errors property="cusname"/></font></td>
<td height="28" align="center">联系人</td>
<td height="28">
<input name="cusConnect" type="text" id="cusConnect">
*<font color="#FF0000"><html:errors property="cusconnect"/></font></td>
</tr>
<tr bgcolor="#FFFFFF">
<td align="center">联系电话</td>
<td height="28">
<input name="cusTel" type="text" id="cusTel">
*<font color="#FF0000"><html:errors property="custel"/></font></td>
<td height="28" align="center">邮 编</td>
<td height="28">
<input name="cusPost" type="text" id="cusPost"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td align="center">通讯地址</td>
<td height="28" colspan="3">
<input name="cusAddress" type="text" id="cusAddress"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2" align="right"><input type="submit" name="Submit" value="提交"></td>
<td colspan="2"><input type="reset" name="Submit2" value="重置"></td>
</tr>
</html:form>
</table>
</body>
</html>
这是addcus.jsp
答!: 7:
<?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 />
<form-beans >
<form-bean name="yonghuForm" type="com.form.YonghuForm" />
<form-bean name="changpwForm" type="com.form.ChangpwForm" />
<form-bean name="cusinfoForm" type="com.form.CusinfoForm" />
</form-beans>

<global-exceptions />
<global-forwards >
<forward name="index" path="/web/index.jsp" />
<forward name="listcusinfo" path="/cus/listcus.jsp" />
</global-forwards>

<action-mappings >
<action
attribute="yonghuForm"
name="yonghuForm"
parameter="method"
path="/web/checklogin"
scope="request"
type="com.action.CheckloginAction"
input="/web/index.jsp" >
<forward name="webmain" path="/web/webmain.jsp" />
</action>
<action
attribute="changpwForm"
input="/web/changpw.jsp"
name="changpwForm"
path="/web/changpw"
scope="request"
type="com.action.ChangpwAction" />
<action path="/web/exit" type="com.action.ExitAction" />
<action
attribute="cusinfoForm"
name="cusinfoForm"
parameter="method"
path="/cus/cusinfo"
scope="request"
type="com.action.CusinfoAction"
input="/cus/addcus.jsp" >
</action>
<action path="/cus/cusinfolist" parameter="method" type="com.action.CusinfolistAction" >
<forward name="editcus" path="/cus/editcus.jsp" />
</action>
</action-mappings>

<message-resources parameter="com.ApplicationResources_ch" key="org.apache.struts.action.MESSAGE" />
</struts-config>

答!: 8:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.form;

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;

/**
* MyEclipse Struts
* Creation date: 08-28-2006
*
* XDoclet definition:
* @struts.form name="cusinfoForm"
*/
public class CusinfoForm extends ActionForm {
/*
* Generated fields
*/
private String cusId;
/** addDate property */
private String addDate;

/** cusTel property */
private String cusTel;

/** cusAddress property */
private String cusAddress;

/** cusConnect property */
private String cusConnect;

/** cusPost property */
private String cusPost;

/** cusName property */
private String cusName;

/*
* Generated Methods
*/

/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
ActionErrors errors=new ActionErrors();
String cusName=request.getParameter("cusName");
String cusTel=request.getParameter("cusTel");
String cusConnect=request.getParameter("cusConnect");
if (cusName==null ||cusName.length()<1)
errors.add("cusname",new ActionError("cusname.not"));
if (cusTel==null ||cusTel.length()<1)
errors.add("custel",new ActionError("custel.not"));
if (cusConnect==null ||cusConnect.length()<1)
errors.add("cusconnect",new ActionError("cusconnect.not"));
return errors;
}

/**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub

}

/**
* Returns the addDate.
* @return String
*/
public String getAddDate() {
return addDate;
}
public String getCusId() {
return cusId;
}

/**
* Set the addDate.
* @param addDate The addDate to set
*/
public void setAddDate(String addDate) {
this.addDate = addDate;
}
public void setCusId(String cusId) {
this.cusId = cusId;
}

/**
* Returns the cusTel.
* @return String
*/
public String getCusTel() {
return cusTel;
}

/**
* Set the cusTel.
* @param cusTel The cusTel to set
*/
public void setCusTel(String cusTel) {
this.cusTel = cusTel;
}

/**
* Returns the cusAddress.
* @return String
*/
public String getCusAddress() {
return cusAddress;
}

/**
* Set the cusAddress.
* @param cusAddress The cusAddress to set
*/
public void setCusAddress(String cusAddress) {
this.cusAddress = cusAddress;
}

/**
* Returns the cusConnect.
* @return String
*/
public String getCusConnect() {
return cusConnect;
}

/**
* Set the cusConnect.
* @param cusConnect The cusConnect to set
*/
public void setCusConnect(String cusConnect) {
this.cusConnect = cusConnect;
}

/**
* Returns the cusPost.
* @return String
*/
public String getCusPost() {
return cusPost;
}

/**
* Set the cusPost.
* @param cusPost The cusPost to set
*/
public void setCusPost(String cusPost) {
this.cusPost = cusPost;
}

/**
* Returns the cusName.
* @return String
*/
public String getCusName() {
return cusName;
}

/**
* Set the cusName.
* @param cusName The cusName to set
*/
public void setCusName(String cusName) {
this.cusName = cusName;
}
}
答!: 9:
1。你的表单是不是验证表单?
2。你的验证Form里有一个固定的方法不知你写了没有,是实现验证的
答!: 10:
3.有一个ApplicationResources配置文件是专门用来写出错信息的,配置多个那个文件可以用来实现国际化的
4.还有一个xml配制文件,要专门指定是那个FormBean,还要指定这个FormBean里面的各个字段做

那些验证,传那些参数到ApplicationResources文件中的 如{0} ...{1}里面去作为显示错误信息

这样你在前台jsp 文件中输入<html:errors property="cusname"/>才会有用
答!: 11:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.DataFormfat;
import com.PageBean;
import com.cus.Cusinfo;
import com.cus.CusinfoDao;
import com.form.CusinfoForm;

/**
* MyEclipse Struts
* Creation date: 08-28-2006
*
* XDoclet definition:
* @struts.action parameter="method" validate="true"
*/
public class CusinfoAction extends DispatchAction {
/*
* Generated Methods
*/

/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception{
// TODO Auto-generated method stub
CusinfoForm cusform=(CusinfoForm) form;
Cusinfo cus=new Cusinfo();
BeanUtils.copyProperties(cus,cusform);
DataFormfat adddata=new DataFormfat();
cus.setAddDate(adddata.getDateTime(""));
if (CusinfoDao.findByname(cus.getCusName()).size()>0)
{
request.setAttribute("message","<script>alert('该公司名称已经存在!');window.history.go(-1);</script>");
return new ActionForward(mapping.getInput());
}

if (CusinfoDao.creatObj(cus))
{
request.setAttribute("message","<script>alert('添加成功!');</script>");
return new ActionForward(mapping.getInput());
}

else
{
request.setAttribute("message","<script>alert('添加失败!');</script>");
return new ActionForward(mapping.getInput());
}
}
public ActionForward list(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception{
String page=request.getParameter("page");
System.out.print(page);
String sql="from Cusinfo";
int page1=1;
if (page!=null)
page1=Integer.parseInt(page);
List list1=CusinfoDao.getList(sql);
PageBean pb=new PageBean();
pb.getCurpage(list1, page1);
List list=pb.getCurpagelist(sql);
request.setAttribute("listcusinfo", list);
request.setAttribute("pb",pb);
return mapping.findForward("listcusinfo");

}
public ActionForward edit(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception{
CusinfoForm cusform=(CusinfoForm) form;
Cusinfo cus=new Cusinfo();
BeanUtils.copyProperties(cus,cusform);
if (CusinfoDao.mdfObj(cus))
{
request.setAttribute("message","<script>alert('修改成功!');</script>");
}
else
{
request.setAttribute("message","<script>alert('修改失败!');</script>");
}

return list(mapping,form,request,response);

}

}
答!: 12:
scope="request"
答!: 13:
例如:<input name="cusName" type="text" id="cusName">
我从来没这么用过。
上面对应应该写成:
<html:text property="cusName" />.
我不明白你既然引入struts,页面为什么没有使用struts标签。
答!: 14:
改成<html:text property="cusName" />.
还是不行
答!: 15:
可以了
多谢了hotmail1981()
答!: 16:
能不能给个好的分页程序给我呀?
shuhongwang613@163.com
答!: 17:
偶也想要个分页的 呷呷

dianziermu@eyou.com

相关JAVA教程:
WEB SERVICE 在TOMCAT上能运行,ORACLE AS不能运行的问题
求助:Hibernate,空指针错误
请教一个Hibernate的AssertionFailure问题
weblogic上连接ms sql连接池错误
求助---加载图片时怎样设置路径?
跨页面的事务问题
求助---加载图片时怎样设置路径?
请各位大侠指点一下,数据连接的问题!
JTree如何编辑节点,大家帮忙看看
各位高手!好,怎么在JButton中设置标题的布局啊!
想学XML,看什么书比较好?
求助:jbuilder2006运行struts程序出错