我有一个userCreateDate字段,当我第一次添加数据时,它会被插入到resp中。表,但是当我尝试通过onClick of Javascript更新相同的记录并将值设置为相应的文本字段时,它为字段" userTypeEntity.userCreatedDate"。提供无效字段值在validate()方法中,日期字段值为空。 你能帮我摆脱这个错误吗? 我的JSP是:
<table width="100%" style="border-collapse: collapse;" border="0" bordercolor="#006699" align="center">
<tr>
<s:hidden id="id" name="userGroupEntity.userGroupId"/>
<s:textfield id="name" name="userGroupEntity.userGroupName" key="label.groupName" maxlength="15" size="30" required="true" labelSeparator=""/>
<s:textfield id="desc" name="userGroupEntity.userGroupDesc" key="label.groupDesc" maxlength="20" size="30" required="true" labelSeparator=""/>
<s:textfield id="cdate" name="userGroupEntity.userCreatedDate" key="label.CreatedDt" size="30" readonly="true" cssStyle="background-color:#E7EBDD;" labelSeparator=""/>
<s:textfield id="mdate" name="userGroupEntity.userModifiedDate" key="label.ModifiedDt" size="30" readonly="true" cssStyle="background-color:#E7EBDD;" labelSeparator=""/>
<s:radio id="status" name="userGroupEntity.userActive" key="label.status" list="userGroupStatus" required="true" labelSeparator=""/>
</tr>
<tr>
<td colspan="100%" align="center"><hr></td>
</tr>
<tr>
<td colspan="100%" align="center">
<s:submit id="save" theme="simple" name="save" key="label.save"/>
<s:reset id="reset" theme="simple" name="reset" key="label.reset"/>
<s:a href="userGroupView">
<img border="0" src='<s:url value="/images/b_cancel.gif"></s:url>' width="50" height="20" alt="Go to User Group Master">
</s:a>
</td>
</tr>
<tr>
<td colspan="100%">
<s:if test="hasActionMessages()"><div style="color:red;"><s:actionmessage/></div></s:if>
</td>
</tr>
</table>
<s:if test="{groupList.size() != 0}">
<s:iterator value="groupList" var="group">
<tr>
<td><s:property value="userGroupName"/></td>
<td><s:property value="userGroupDesc"/></td>
<td><s:property value="userCreatedDate"/></td>
<td><s:property value="userModifiedDate"/></td>
<td align="center"><b><a href="#" onclick="javascript:userGroupEdit('edit','${userGroupId}','${userGroupName}','${userGroupDesc}','${userCreatedDate}','${userModifiedDate}');">Edit</a></b>
| <b><a href="#" onclick="javascript:userGroupDelete('userGroupDelete.action?groupId=${userGroupId}');">Delete</a></b>
</td>
</tr>
</s:iterator>
</s:if>
我的行动类是:
public class UserGroupAction extends ActionSupport implements Preparable{
private static Log log = LogFactory.getLog(UserGroupAction.class);
/**
*
*/
private static final long serialVersionUID = 1L;
private String result;
private UserGroupEntity userGroupEntity;
private Map<String, String> userGroupStatus;
private List<UserGroupEntity> groupList;
private int userSaveStatus;
private int groupId;
public String userGroupSave() throws Exception{
log.info("...In UserGroupAction.userGroupSave...");
// int userSaveStatus = 1;
try {
int groupId = userGroupEntity.getUserGroupId();
System.out.println("Group Id: "+groupId);
System.out.println("Group Name: "+userGroupEntity.getUserGroupName());
if(groupId == 0){
userSaveStatus = UserGroupController.userGroupSave(userGroupEntity);
System.out.println("userSaveStatus: "+userSaveStatus);
if(userSaveStatus == 1 ){
addActionMessage("Group Added Successfully.");
result = SUCCESS;
}else{
addActionMessage("Group Already Exists.");
result = SUCCESS;
}
}else{
userSaveStatus = UserGroupController.userGroupSave(userGroupEntity,groupId);
System.out.println("userSaveStatus: "+userSaveStatus);
if(userSaveStatus == 1 ){
addActionMessage("Group Updated Successfully.");
result = SUCCESS;
}else{
addActionMessage("Group Already Exists.");
result = SUCCESS;
}
}
} catch (Exception e) {
System.out.println("...In Catch of UserGroupAction.execute ...");
result = ERROR;
e.printStackTrace();
}
return result;
}
@Override
public void validate(){
System.out.println("...In UserGroupAction.validate ...");
if(isEmpty(userGroupEntity.getUserGroupName())){
addFieldError("userGroupEntity.userGroupName", "Group name can't be empty.");
}
if(isEmpty(userGroupEntity.getUserGroupDesc())){
addFieldError("userGroupEntity.userGroupDesc", "Group description can't be empty.");
}
if(isEmpty(userGroupEntity.getUserActive())){
addFieldError("userGroupEntity.userActive", "Please select status.");
}
//System.out.println("Create Date:: "+userGroupEntity.getUserCreatedDate());
}
}
此处所有其他testField值正在运行,但日期字段值将为空...请帮助。我被困在这里......
我的Javascript是:
function userGroupEdit(flag,id,name,desc,cdate,mdate){
alert(flag+id+name+desc+cdate+mdate);
if(flag=='edit'){
document.getElementById("id").style.display="block";
document.getElementById("id").value=parseInt(id);
document.getElementById("name").value=name;
document.getElementById("desc").value=desc;
document.getElementById("cdate").value=cdate;
document.getElementById("mdate").value=mdate;
document.getElementById("statusY").checked=true;
}else{
//document.getElementById("save").style.display="block";
}
//window.location = url;//"${pageContext.request.contextPath}/"+url;
return true;
}
答案 0 :(得分:0)
我得到了解决方案:在Javascript中,我将日期格式化为
function setDateFormat(strDate){
//strDate '2014-06-18'
//alert(strDate.length);
if(strDate!=''){
var y,m,d;
y = strDate.substring(0,4);//alert(y);
m = strDate.substring(5,7);//alert(m);
d = strDate.substring(8,10);//alert(d);
var newDate = m+"/"+d+"/"+y;
//alert(newDate);
return newDate;
}else{
if(typeof(strDate) == 'string'){
newDate = '';
return newDate;
}
}
}
这些日期字段现在在DB中保留,并且没有Struts2字段消息,例如来自validate方法的字段的字段值无效。