在Struts中的各个配置文件都正确,就是比较字符串时出错,
这是ActionForm的实现类Lpackage hello.struts.form;import javax.servlet.http.HttpServletRequest;import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;public class HelloForm extends ActionForm {
 
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors error=new ActionErrors();
//if(name==null||name.length()==0){
//error.add("hello_error", new ActionMessage("error_hello_message"));
//}
if(error.isEmpty())
return null;
return error;
}


}
这是Action的实现类package hello.struts.form;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;public class HelloAction extends Action { @Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HelloForm hf=(HelloForm)form;
ActionMessages error=new ActionMessages();
String s=hf.getName();
if(s.equals("god")){          //就在这一行出错,错误信息我马上给出 请大哥大姐指点一下.
error.add("action_error",new ActionMessage("error_action_message"));
this.saveErrors(request, error);
return mapping.getInputForward();
}


return mapping.findForward("success");
}
}这是资源文件内容error_hello_message=name not is nullerror_action_message=name not is god
小弟才开始学习struts1.x 三天时间,感到还打不到什么门路,也没有老师指点,所以就在网上找个视频,出错了也没有人指点,所到网上请大哥大姐来指点一下.

解决方案 »

  1.   

    if(s.equals("god")){ s是空字符串了吧
    比较前判断下null,
    if(s!=null && s.equals("god")){ 
    或者反过来
    if("god".equals(s)){ 
      

  2.   

    你报的应该是空指针异常吧就用楼上的方式首先判断是否为空,在进行判断还有比较为空为null个人建议用null!=object || "".equals(object);
      

  3.   


    一楼正解,对于字符串的比较最好是null在前