笑看风云

一切从头开始
posts - 28, comments - 1, trackbacks - 0, articles - 2

表单错误信息的显示

Posted on 2008-07-10 14:05 笑看风云 阅读(169) 评论(0)  编辑  收藏 所属分类: struts
1.login.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> 
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
 
<html> 
    
<head>
        
<title>login</title>
    
</head>
    
<body>
        
<html:form action="/login">
         
<html:errors property="handle"/><br/>
            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>
2.loginAction
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 
*/

package com.my.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
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;

import com.my.struts.form.LoginForm;

/** 
 * MyEclipse Struts
 * Creation date: 06-18-2008
 * 
 * XDoclet definition:
 * @struts.action path="/login" name="loginForm" input="/form/login.jsp" scope="request" validate="true"
 * @struts.action-forward name="ok" path="/login/login_suc.jsp"
 
*/

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)
throws Exception {
        LoginForm loginForm 
= (LoginForm) form;// TODO Auto-generated method stub
        String username=loginForm.getUsername();
        String password
=loginForm.getPassword();
        
if(username.equals("admin")&&password.equals("111111"))
         
return mapping.findForward("ok");
        
//--------------------------------------------------------------------------------
        
        
//第一种方法
        ActionErrors errors=new ActionErrors();
        ActionMessage msg;
        msg
=new ActionMessage("login.failed");
        errors.add(
"handle",msg);
        saveErrors(request, errors);
        
//--------------------------------------------------------------------------------
        
        
//第二种方法(把login.jsp中<html:errors property="handle"/><br/>改为<html:errors /><br/>)
        
//        ActionMessages msg=this.getMessages(request);
//        msg.add(Globals.ERROR_KEY, new ActionMessage("login.failed2"));
//        this.addErrors(request, msg);
        
//--------------------------------------------------------------------------------
        
        
return mapping.getInputForward();
    }

}

3.ApplicationResources.properties
# Resources for parameter 'com.my.struts.ApplicationResources'
# Project MessageTest
login.failed
=<font color="#FF0044">错误的用户名或密码</font>
login.failed2
=<font color="#FF0044">出错啦!</font>

当然啦,也可以能过loginform里的validate来实现:
ActionErrors errors = new ActionErrors(); // create ActionErrors instance  
  ActionMessage msg;
if (username==null||username.length() < 1) {
    msg = new ActionMessage("error.notnull", username);
    errors.add("username", msg);
   }
   if (password==null||password.length() < 1) {
    msg = new ActionMessage("error.notnull", password);
    errors.add("password", msg);
   }
  return errors;

只有注册用户登录后才能发表评论。


网站导航: