Struts(简单的Form验证)

来源:互联网 发布:qt 调用windows api 编辑:程序博客网 时间:2024/06/03 15:58

Struts(简单的Form验证) - 398198920 - 冰冻三尺非一日之寒

login.jsp

<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
 
<html>
 <head>
  <title>JSP for LoginForm form</title>
 </head>
 <body>
  <html:form action="/login" focus="username">
      username : <html:text property="username"/><html:errors property="username"/><br/>
   password : <html:password property="password"/><html:errors property="password"/><br/>
   
   <html:submit/><html:cancel/>
  </html:form>
 </body>
</html>

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  <data-sources />
  <form-beans >
    <form-bean name="loginForm" type="org.eimhe.struts.form.LoginForm" />

  </form-beans>

  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      attribute="loginForm"
      input="/login.jsp"
      name="loginForm"
      path="/login"
      scope="request"
      type="org.eimhe.struts.action.LoginAction" />

  </action-mappings>

  <message-resources parameter="org.eimhe.struts.ApplicationResources" />
</struts-config>

LoginForm.java

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package org.eimhe.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;

/**
 * MyEclipse Struts
 * Creation date: 09-27-2006
 *
 * XDoclet definition:
 * @struts.form name="loginForm"
 */
public class LoginForm extends ActionForm {
 /*
  * Generated fields
  */

 /** password property */
 private String password;

 /** username property */
 private String username;

 /*
  * 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();
   if(username.equals("")||username.equals("guest"))
   {
    errors.add("username",new ActionMessage("mytest.error"));
   }
  return errors;
 }

 /**
  * Method reset
  * @param mapping
  * @param request
  */
 public void reset(ActionMapping mapping, HttpServletRequest request) {
  username="guest";
  password="guest";
  // TODO Auto-generated method stub
 }

 /**
  * Returns the password.
  * @return String
  */
 public String getPassword() {
  return password;
 }

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

 /**
  * Returns the username.
  * @return String
  */
 public String getUsername() {
  return username;
 }

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

LoginAction.java

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package org.eimhe.struts.action;

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.eimhe.struts.form.LoginForm;

/**
 * MyEclipse Struts
 * Creation date: 09-27-2006
 *
 * XDoclet definition:
 * @struts.action path="/login" name="loginForm" input="/login.jsp" scope="request" validate="true"
 */
public class LoginAction extends Action {
 /*
  * Generated Methods
  */

 /**
  * Method execute
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return ActionForward
  */
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
  return null;
 }
}

ApplicationResources.properties

# Resources for parameter 'org.eimhe.struts.ApplicationResources'
# Project P/eimheStruts2
mytest.error=\该\用\户\名\无\法\进\一\不\操\作

0 0