wood burning stoves 2.0*
The moose likes Other Application Frameworks and the fly likes How display errormessage from model on jsp page? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "How display errormessage from model on jsp page?" Watch "How display errormessage from model on jsp page?" New topic
Author

How display errormessage from model on jsp page?

Cheng-Hao Hsu
Greenhorn

Joined: Feb 23, 2009
Posts: 3
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]



1
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Cheng-Hao Hsu
Greenhorn

Joined: Feb 23, 2009
Posts: 3
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.

 
 
subject: How display errormessage from model on jsp page?
 
Similar Threads
Failed to obtain specified collection Help Me
Compiler error in Action class - configuring data source in struts-config.xml. Please help!
Need an attribute to be available in different places (SOLVED)
Spring form input binding problem
problem in Struts Validation