憋不住了,请教大虾

wangwang1120 2010-12-22 04:51:32
这个问题让快疯掉了,大虾请教啊
问题描述:
利用struts2里的validate方法对所有action里的属性进行校验,不输入名称的时候可以在input视图显示错误信息(正常情况),如果输入字符就提示在【this.student.getName() == null || "".equals(this.student.getName().trim())】行提示空指针。
以下是源码,

public class Student {
private String name;

public String getName() {
return age;
}

public void setName(String name) {
this.name = name;
}
}

public class StudentAction extends ActionSupport {
private Student student;

public Student getStudent() {
return student;
}

public void setStudent(Student student) {
this.student = student;
}

@Override
public void validate() {
if( this.student.getName() == null
|| "".equals(this.student.getName().trim())){ //提示这行出现空指针
this.addFieldError("name", "名称不能为空");
}
}

}

jsp页面相关代码如下
<input type="text" name="student.name"/>

还有不清楚的地方,请大家提出来哦。谢谢咯!!!急啊。在线等了
...全文
102 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangwang1120 2010-12-22
  • 打赏
  • 举报
回复
知道原因了。validate方法是没有错的,是我的action里add方法调用其他的同一个action里的view方法,所以调用了2次validate方法,第二次formbean里是没有值的,所以validate方法报nullpointException,对指定的方法进行校验就可以了。解决了。
谢谢上面的两位的回帖!
a21768541 2010-12-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wangwang1120 的回复:]
如果改成&&的话,校验逻辑就变了。这好像没关系吧。

引用 2 楼 a21768541 的回复:
把或者(||)改成&amp;&amp; 试试。。。
[/Quote]
额...你先试试嘛。。
wangwang1120 2010-12-22
  • 打赏
  • 举报
回复
如果改成&&的话,校验逻辑就变了。这好像没关系吧。
[Quote=引用 2 楼 a21768541 的回复:]
把或者(||)改成&& 试试。。。
[/Quote]
wangwang1120 2010-12-22
  • 打赏
  • 举报
回复
我debug了下,第一次调用validate方法里的时候检验都可以通过,
this.student.getName()里是有值的,后来又调用了一次这个validate方法里的校验,第二次的时候this.student.getName()里是空的,为什么会调用两次校验啊?
[Quote=引用 1 楼 happyxlh111 的回复:]
看看对象里有没有值啊?
[/Quote]
a21768541 2010-12-22
  • 打赏
  • 举报
回复
把或者(||)改成&& 试试。。。
happyxlh111 2010-12-22
  • 打赏
  • 举报
回复
看看对象里有没有值啊?

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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