首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Exception >

struts1.0中用html:errors返回null是怎么回事?

2014-01-26 
我的代码:LogonForm.javapublic class LogonForm extends ActionForm {private String password nullpri

我的代码:
  LogonForm.java
 
  public class LogonForm extends ActionForm {
 
 
  private String password = null;
  private String username = null;
 
  public String getPassword() {
 
  return (this.password);
 
  }
  public void setPassword(String password) {
 
  this.password = password;
 
  }
 
  public String getUsername() {
 
  return (this.username);
 
  }
  public void setUsername(String username) {
 
  this.username = username;
 
  }
 
 
  public ActionErrors validate(ActionMapping mapping,
  HttpServletRequest request) {
 
  ActionErrors errors = new ActionErrors();
  if ((username == null) || (username.length() < 1))
  errors.add("username", new ActionError("error.username.required"));
  if ((password == null) || (password.length() < 1))
  errors.add("password", new ActionError("error.password.required"));
 
  return errors;
 
  }
  /******************************************************************/
  LogonAction.java
  public class LogonAction extends Action{
  public ActionForward perform(ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response)throws java.io.IOException, ServletException{
  ActionErrors errors = new ActionErrors();
  errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.test"));
  if (!errors.empty()) {
  saveErrors(request, errors);
  return (new ActionForward(mapping.getInput()));
  }
 
  String userName = ((LogonForm)form).getUsername();
  String password = ((LogonForm)form).getPassword();
  System.out.println(userName);
  System.out.println(password);
  return mapping.findForward("failure");
  }
 
  }
  /*****************************************************************/
  Logon.jsp
  <%@ page language="java" %>
  <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
  <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
 
 
  <html:html>
  <head>
  <title>Logon Page</title>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  </head>
  <body>
  <form name="form1" method="POST" action="logon.do">
  <input name = "username" value="">
  <br>
  <input name = "password" value="">
  <br>
  <input type =submit>
  </form>
  </body>         

热点排行