I plan write a login example with Netbean6.5 and Spring2.5.
I write a
jsp page(login.jsp) to save user's account and password into command object,
then send to controller (LoginAction.java).
On LoginAction.java I return a model to login.jsp in order to display error message,
but I dont know how to display it on jsp page.
I read API, errors.getModel() is a model with hashmap class,
and I add two error messages by rejectValue(),
but What express language should I write on login.jsp,
then I can display the error messages on login.jsp?
This example is from book,but it's can't run,
so I fix the code.
Has anyone can tell me what should I do?
login.jsp
----------------------------------------------------------------------------------------
<%--
Document : login
Created on : 2009/4/3, 上午 09:55:47
Author : Administrator
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h3>登入表單</h3>
請輸入使用者名稱與密碼:
<%--錯誤訊息--%>
<spring:bind path="command.*">
</spring:bind>
<form name="loginform" method="post" action="validator.do">
<spring:bind path="command">
名稱<input type="text" name="account" value="${command.account}"/>
密碼<input type="password" name="password" value="${command.password}"/>
</spring:bind>
<input type="submit" value="送出"/>
</form>
<%--<vld:validate validationName="loginform" page="0"/>--%>
</body>
</html>
----------------------------------------------------------------------------------------
LoginAction.java
----------------------------------------------------------------------------------------
package edu.chenghao.internal.common.web.controller;
import java.util.ArrayList;
import java.util.HashMap;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import edu.chenghao.internal.common.web.form.LoginForm;
import java.util.List;
public class LoginAction extends SimpleFormController
{
public LoginAction()
{
//this.setSessionForm(true);
setCommandClass(LoginForm.class);
}
@Override
protected ModelAndView onSubmit(Object arg0, BindException errors) throws Exception
{
System.out.println("-----------分隔線-----------");
LoginForm loginForm=(LoginForm)arg0;
System.out.println("-----------usrerName:"+loginForm.getAccount()+"---userPwd:"+loginForm.getPassword());
if(loginForm.getAccount().equals("test") && loginForm.getPassword().equals("1234"))
{
HashMap hm=new HashMap();
hm.put("loginForm",loginForm);
ArrayList al=new ArrayList();
al.add("messege1");
al.add("messege2");
al.add("messege3");
al.add("messege4");
al.add("messege5");
hm.put("messages",al);
return new ModelAndView(this.getSuccessView(),hm);
}
else
{
errors.rejectValue("account",null,null,"使用者名稱錯誤");
errors.rejectValue("password",null,null,"使用者密碼錯誤");
List list = errors.getAllErrors();
for( int i=0; i<list.size(); i++){
System.out.println( "第"+i+"個"+ list.get(i) );
}
return new ModelAndView(this.getFormView(),errors.getModel());
//return new ModelAndView(this.getFormView(),this.getCommandName(),loginvo);
}
}
}
---------------------------------------------------------------------------------------->