Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How display errormessage from model on jsp page?

 
Cheng-Hao Hsu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
}

---------------------------------------------------------------------------------------->
TestLoginForm.jpg
[Thumbnail for TestLoginForm.jpg]
(jpg to zip) This is my code.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I can't help you much with how you are doing it there. As I believe that is how you might have done it before Spring 2.5 Maybe Spring 2.0 or before.

But for me, I create a login page exactly like I would with straight JSP using the j_ properties in the login page.

Then in Spring, I would configure Spring Security and set a <login-page> to your page.

In your web.xml you use FORM for security, all this is from the Servlet/JSP spec, and it follows it to a tee.

I hope that helps move you in the right direction.

Good Luck

Mark
 
Cheng-Hao Hsu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks your advise.

I need a job, so I learn Spring just because many companies project use it.
But you are right that use Servlet/jsp are basic and suitable.

Maybe I should learn the basic skills again.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic