View Full Version : Problems with
carlos_santiago
Jul 27th, 2005, 09:59 AM
Hi, I have a problem using <spring:bind> in my jsp.
An exception is throw:
27/07/2005 09:50:33 org.springframework.web.servlet.tags.RequestContex tAwareTag doStartTag
SEVERE: Neither Errors instance nor plain target object for bean name 'bean' ava
ilable as request attribute
javax.servlet.jsp.JspTagException: Neither Errors instance nor plain target obje
ct for bean name 'bean' available as request attribute
at org.springframework.web.servlet.tags.BindTag.doSta rtTagInternal(BindTag.java
:118)
at org.springframework.web.servlet.tags.RequestContex tAwareTag.doStartTag(Reque
stContextAwareTag.java:)
at org.apache.jsp.WEB_002dINF.views.pedidosPecas.Pedi dosAutorizados_jsp._jspSer
vice(PedidosAutorizados_jsp.java:423)
and so on...
In my jsp I have:
<spring:bind path="bean.campo">
<td><div align="right">Dados para a consulta : </div></td>
<td><input name="<c:out value="${status.expression}"/>" value="<c:out value="${status.value}"/>" type="text" class="formulario">
<input name="Button" type="submit" class="formulario" value="Localizar"></td>
<FONT color="red" size="1">
<c:out value="${status.errorMessage}"/><br>
</FONT>
<!-- <td></td> -->
<td></td>
</spring:bind>
My controller:
/**
* @author Carlos Santiago
* Projeto: LinkPecas
* Arquivo: PedidosAutorizadosControlador.java
*/
public class PedidosAutorizadosControlador extends AbstractCommandController{
/** variáveis */
private PedidosAutorizados pedidosAutorizados;
/** construtor */
PedidosAutorizadosControlador(){
}
/**
* Este método manuzeia as informações vindas do form
*/
protected ModelAndView handle(HttpServletRequest request,HttpServletResponse response, Object command, BindException erros)throws Exception{
/* pega as informações do form e seta no bean */
PedidosAutorizadosBean bean = (PedidosAutorizadosBean)command;
List pedidos = new ArrayList();
/* verifica qual botão de radio foi selecionado */
if(bean.getBotaoRadio().equals(DominioForm.PEDIDO) )
bean = pedidosAutorizados.getPedidosAutorizadosByNumeroPe dido(new Integer(bean.getCampo()));
else if(bean.getBotaoRadio().equals(DominioForm.SINISTR O))
bean = pedidosAutorizados.getPedidosAutorizadosByNumeroSi nistro(bean.getCampo());
else if(bean.getBotaoRadio().equals(DominioForm.PLACA))
bean = pedidosAutorizados.getPedidosAutorizadosByPlaca(be an.getCampo());
/* verifica se existe dados no bean */
if(bean.getNumeroPedido() > 0)
pedidos.add(bean);
return new ModelAndView("/pedidosPecas/PedidosAutorizados", "pedidos", pedidos);
}
/**
* @param Seta o parâmetro pedidosAutorizados para o campo pedidosAutorizados.
*/
public void setPedidosAutorizados(PedidosAutorizados pedidosAutorizados) {
this.pedidosAutorizados = pedidosAutorizados;
}
}
And my context:
<bean id="pedidosAutorizadosControlador" class="br.com.rodobens.web.pedidospecas.PedidosAutorizado sControlador">
<property name="pedidosAutorizados" ref="pedidosAutorizados"/>
<property name="sessionForm" value="true"/>
<property name="commandName" value="bean"/>
<property name="commandClass" value="br.com.rodobens.beans.pedidospecas.PedidosAutoriza dosBean"/>
<property name="validator" ref="pedidosAutorizadosValidacao"/>
</bean>
What's is wrong ?
Thank's
Carlos
katentim
Jul 28th, 2005, 06:13 PM
Are you rendering the JSP via the controller, or hitting it directly with the browser?
carlos_santiago
Jul 28th, 2005, 07:59 PM
Are you rendering the JSP via the controller, or hitting it directly with the browser?
Directly with the browser !
katentim
Jul 28th, 2005, 08:33 PM
Directly with the browser !
You need to go via the controller. This is the MVC pattern - the controller returns the model to your view. If you hit the view (i.e. the JSP) directly, you don't have the model to render it correctly. Take a look at the samples for an example.
carlos_santiago
Jul 29th, 2005, 08:52 AM
Look, I call, in my jsp, the Controller, he pass by Validator, but not "validate" , pass by Controller, go to the jsp, however the status.messageError don't display any error !
Carlos
Colin Yates
Jul 29th, 2005, 10:19 AM
Keep your shirt on ;)
First thing; if you want to do validation and binding, don't use AbstractCommandController, use SimpleFormController.
Second, you need to understand what katentim is saying; if you go to a view which is mapped to a jsp, then *just the jsp will be called*. If you want to go a view which requires staging by a controller, then the view must call that controller (e.g. RedirectView).
It is a mistake to think that rendering a jsp calls the controller that happens to return that view. Views do not equate to controllers ;) Urls equate to controllers which eventually render a view.
Second thing; don't get arsey otherwise people will ignore you.
carlos_santiago
Jul 29th, 2005, 11:24 AM
OK yatesco. I am calm. Sorry ! :D
But I do not obtain to make to function my application.
Now I am using SimpleFormController, but my formulario has that to present a List when will be presented.
Then I overring the referenceDate(), but does not function.
- the form is presented without problems, but it does not show the List.
- When I submit the form it it is validated and it presents the message of error without problems.
- When sending the filled form I get an error with relation to the name of mine bean in my jsp
- my page of success is the same one "formView"
The message is:
29/07/2005 11:03:28 org.springframework.web.servlet.tags.RequestContex tAwareTag
doStartTag
SEVERE: Neither Errors instance nor plain target object for bean name 'ped' avai
lable as request attribute
javax.servlet.jsp.JspTagException: Neither Errors instance nor plain target obje
ct for bean name 'ped' available as request attribute
at org.springframework.web.servlet.tags.BindTag.doSta rtTagInternal(BindTag.java
:118)
at org.springframework.web.servlet.tags.RequestContex tAwareTag.doStartTag(Reque
stContextAwareTag.java:
at org.apache.jsp.WEB_002dINF.views.pedidosPecas.Pedi dosAutorizados_jsp._jspSer
vice(PedidosAutorizados_jsp.java:423)
Then, I do not know what it happens !
My controller is:
public class PedidosAutorizadosControlador extends SimpleFormController{
/** variáveis */
private PedidosAutorizados pedidosAutorizados;
/** construtor */
PedidosAutorizadosControlador(){
}
protected Map referenceDate(){
Map lista = new HashMap();
lista.put("pedidos", pedidosAutorizados.getListaPedidosAutorizados());
return lista;
}
/**
* Este método encaminha a requisição solicitada e não executa mais nada !
*/
protected ModelAndView onSubmit(Object obj, BindException erros) throws Exception {
/* pega as informações do form e seta no bean */
PedidosAutorizadosBean bean = (PedidosAutorizadosBean)obj;
List pedidos = new ArrayList();
/* verifica qual botão de radio foi selecionado */
if(bean.getBotaoRadio().equals(DominioForm.PEDIDO) )
bean = pedidosAutorizados.getPedidosAutorizadosByNumeroPe dido(new Integer(bean.getCampo()));
else if(bean.getBotaoRadio().equals(DominioForm.SINISTR O))
bean = pedidosAutorizados.getPedidosAutorizadosByNumeroSi nistro(bean.getCampo());
else if(bean.getBotaoRadio().equals(DominioForm.PLACA) )
bean = pedidosAutorizados.getPedidosAutorizadosByPlaca(be an.getCampo());
/* verifica se existe dados no bean */
if(bean.getNumeroPedido() > 0)
pedidos.add(bean);
return new ModelAndView(getSuccessView(), "pedidos", pedidos);
}
/**
* @param Seta o parâmetro pedidosAutorizados para o campo pedidosAutorizados.
*/
public void setPedidosAutorizados(PedidosAutorizados pedidosAutorizados) {
this.pedidosAutorizados = pedidosAutorizados;
}
}
In my jsp have:
<spring:bind path="ped.campo">
<td><div align="right">Dados para a consulta : </div></td>
<td><input name="<c:out value="${status.expression}"/>" type="text" class="formulario">
<input name="Button" type="submit" class="formulario" value="Localizar"></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td colspan="3">
<FONT color="red" size="0">
<c:out value="${status.errorMessage}"/><br>
</FONT>
</td>
<td></td>
<td></td>
<td></td>
</spring:bind>
.
.
.
<c:forEach var="pedidos" items="${pedidos}">
<tr>
<td width="126"><c:out value="${pedidos.numeroSinistro}"/></td>
<td width="78"><a href="<c:url value="/pedidosPecas/alteraPedido.lpw"/>"><c:out value="${pedidos.numeroPedido}"/></a>
<FONT color="red" size="1">
<c:out value="${status.errorMessage}"/><br>
</FONT>
</td>
<td width="75"><c:out value="${pedidos.placa}"/></td>
<td width="133"><c:out value="${pedidos.nomeOficina}"/> </td>
<td width="145"><c:out value="${pedidos.dsVeiculo}"/> </td>
</tr>
</c:forEach>
My context file:
<bean id="pedidosAutorizadosControlador" class="br.com.rodobens.web.pedidospecas.PedidosAutorizado sControlador">
<property name="sessionForm" value="true"/>
<property name="commandName" value="ped"/>
<property name="commandClass" value="br.com.rodobens.beans.pedidospecas.PedidosAutoriza dosBean"/>
<property name="validator" ref="pedidosAutorizadosValidacao"/>
<property name="formView" value="pedidosPecas/PedidosAutorizados"/>
<property name="successView" value="pedidosPecas/PedidosAutorizados"/>
<property name="pedidosAutorizados" ref="pedidosAutorizados"/>
</bean>
What it is made a mistake , please ?
Thank's.
Carlos
Colin Yates
Jul 29th, 2005, 12:28 PM
OK, so let me clarify ;)
Step 1: you view the controller "/yourcontroller.htm".
referenceData should be called and the list is populated.
Step 2: you fill in the values and then submit
validation fails, and you see validation errors.
Step 3: fix valiation errors, resubmit and then you get the error.
The reason step 3 is giving you an error is because the model is indeed not populated correctly ;)
As stated earlier ;) the *view* is simply a jsp. onSuccess is returning a ModelAndView comprising view=pedidosPecas/PedidosAutorizados, and a model containing a single object called "pedidos". When your jsp is rendered it tries to bind to a form called "peb", but that isn't in your model!
You have two options, either create another view which *redirects* to your controller, or change your onSubmit to:
return new ModelAndView(getSuccessView(), "pedidos", pedidos).addAll(errors.getModel));
Let me re-iterate, a JSP view is *not* your controller. Every time you call the url that is mapped to your controller, the controller will populate the model and the view defined under "pedidosPecas/PedidosAutorizados" will be rendered with that model. BUT rendering the view "pedidosPecas/PedidosAutorizados" will not cause the controller to populate it!
In essence, onSubmit (which is called if no validation errors) is returning a model which does not contain the command form.
carlos_santiago
Jul 29th, 2005, 01:18 PM
Step 1: you view the controller "/pedidosPecas/PedidosAutorizados.lpw".
OK, it's fine !
But don't call referenceDate() and not populate my List !
Step 2: you fill in the values and then submit
validation fails, and you see validation errors.
Ok no problem here.
Step 3: fix valiation errors, resubmit and then you get the error.
Ok, the problem was decided with:
return new ModelAndView(getSuccessView(), "pedidos", pedidos).addAllObjects(erros.getModel());
However when mine form is shown the first time it does not load the list, why?
carlos_santiago
Jul 29th, 2005, 08:32 PM
Lack of attention!
I did not see that referendeDatE(), would have to be referenceDatA(HttpServletRequest request)!
The application functions well.
Thank's yatesco!
Colin Yates
Aug 1st, 2005, 07:13 AM
No probs ;)
Try not to use so many!!! :) It appears you are shouting ;)
ypj
Sep 5th, 2005, 12:55 AM
Hello all. I got the same Exception of
javax.servlet.jsp.JspTagException: Neither Errors instance nor plain target object for bean name 'blah blah...
The discuss here is very helpful for me :lol: . But I still have some questions about the spring-mvc.
I use a SimpleFormController to show my view. I want to show something at the begining, not jus blank input areas waiting for others input. In fact, I need to edit some object already exist. So I use showForm to display the existing object.
protected ModelAndView showForm(HttpServletRequest request,
HttpServletResponse response,
BindException errors) throws Exception {
System.out.println("Creating a LoadRawWord View");
try {
WordObj wo = (WordObj)service.getRawWords(1).iterator().next ();
return new ModelAndView( "LoadRawWordForm.jsp", "rawWord", wo );
} catch (Exception e){
return new ModelAndView( "NoMoreWords.jsp" );
}
Then I got the "javax.servlet.jsp.JspTagException". and nothing will be show. If I use
return new ModelAndView( "LoadRawWordForm.jsp", "rawWord", wo ).addAllObjects(errors.getModel());
The object can be show, everything seems ok.
Now I know that we can use referenceData() and onSubmit() to display existing objects and edit them :D . But I'm not clear about the difference between using showForm() and not using shwoForm. Why do we have to add the addAllObjects(errors.getModel()) to the ModelAndView?
Could any one please give me the answer, or just show me a map of the spring mvc? Thanks. :)
Colin Yates
Sep 5th, 2005, 04:47 AM
Now I know that we can use referenceData() and onSubmit() to display existing objects and edit them Very Happy . But I'm not clear about the difference between using showForm() and not using shwoForm. Why do we have to add the addAllObjects(errors.getModel()) to the ModelAndView?
Reference data should really only be used to populate readonly lists. I (personally) wouldn't expect data loaded in reference data to be modified. No reason it can't be, just not what I would expect.
showForm basically retrieves the errors object, populates the model with the errors object and returns the form view with the populated model, exactly what return new ModelAndView( "LoadRawWordForm.jsp", "rawWord", wo ).addAllObjects(errors.getModel()); does.
However, I would recommend using showForm because it makes it more explicit, and showForm also includes some other important logic.
HTH
ypj
Sep 5th, 2005, 07:19 AM
yatesco, thank you very much.
:)
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.