View Full Version : Q: how can i make a ModelAndView in a SimpleFormController???
Dari
Jun 1st, 2007, 01:00 PM
Hello everbody,
i have a problem.
I want to post some stuff from a normal Model(ModelAndView) but it must be also a Form(SimpleFormController)
How can i get a ModelAndView in the FormView from a SimpleformController?
i works with the funktion showForm(request, response, exception) but then Spring couldn't find my Bind-Object and shows this error:
"Neither Errors instance nor plain target object for bean name 'blah' available as request attribute"
how can i solve this problem?
greetings
Jörg Heinicke
Jun 1st, 2007, 02:07 PM
What about modifying the ModelAndView returned from the default implementations of the methods in SimpleFormController? Overwrite one method, call the same method on super, modify the return ModelAndView, return the modified ModelAndView.
Jörg
Dari
Jun 2nd, 2007, 06:53 AM
Thank you, it works....
this was my Code before:
@Override
protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException e)
throws Exception {
//get the entrys from a file
List<Names> names;
names = this.fileManager.getNames("txtFileName.txt"); //fileManager is a Member Variable
int countEntries = names.size();
Map myModel = new HashMap();
myModel.put("names", names);
myModel.put("countEntries", countEntries);
return new ModelAndView("names", "model", myModel);
}
here is the importent part of my Controller if someone has the same problem:
@Override
protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException e)
throws Exception {
ModelAndView myModelAndView = super.showForm(request, response, e);
//get the entrys from a file
List<Names> names;
names = this.fileManager.getNames("txtFileName.txt"); //fileManager is a Member Variable
int countEntries = names.size();
Map myModel = new HashMap();
myModel.put("names", names);
myModel.put("countEntries", countEntries);
myModelAndView.addObject("model", myModel);
return myModelAndView;
}
whithout this, it was for me impossible to make a <spring:bind>something</spring:bind> in my *.jsp file.
it shows every time the error:
"Neither Errors instance nor plain target object for bean name 'blah' available as request attribute"
greetings
Dari
fuzziman
Jun 2nd, 2007, 07:05 AM
i think there is built in support for what you are doing...
showForm(HttpServletRequest request, HttpServletResponse response, BindException errors, Map controlModel)
where controlModel is a map of additional data you want to add to your model. note that the implementation is model.putAll(controlModel) so your JSP will access "names" and "countEntries" directly (rather than "model.names" and "model.countEntries" ) if using this method.
Jörg Heinicke
Jun 2nd, 2007, 08:27 AM
Ah, I forgot this possibility completely. I only would have suggested to add the objects directly to myModelAndView instead of myModel, but the solution with the control model is even better.
Jörg
sureshsoftpjx
Jun 3rd, 2007, 01:13 PM
Hi all,
I am also getting this error.i.e
javax.servlet.ServletException: Neither Errors instance nor plain target object for bean name 'commandBean' available as request attribute
But i used onSubmit() instead of showForm().
My code is
code:
protected ModelAndView onSubmit(HttpServletRequest req,HttpServletResponse res,Object command,BindException errors) throws Exception
{
ContactForm cf=(ContactForm)command;
FileHandler fh=new FileHandler(logLocation);
String name = cf.getName();
String addr = cf.getAddress();
String tno = cf.getTeleNum();
String pcode = cf.getPostcode();
String plats = cf.getPlats();
String email = cf.getEmail();
String opmer=cf.getOpmer();
//System.out.println("\nsmtp host: \t "+smtpHost+"\n");
//System.out.println("\nname:\t"+name+"\n");
//Send logger output to our FileHandler.
logger.addHandler(fh);
//Request that every detail gets logged
logger.setLevel(Level.ALL);
Map model=new HashMap();
model.put("commandBean",this.formBackingObject(req));
String rep[]={"ratna@spind.com","praveen@spind.com"};
String msg="Naam : "+name+"\nAdres : "+addr+"\nPostcode : "+pcode+"\nPlaats : "+plats+"\nTelefoonnumber : "+tno+"\nEmail : "+email+"\nOpmerkingen : "+opmer;
try{
postMail(rep,"Form Mail",msg,"suresh@spind.com");
}
catch(MessagingException me){
logger.warning("Message cannot be sent to "+rep+" from "+email);
return new ModelAndView(this.getSuccessView(),model);
}
//Log a simple INFO message.
logger.info("Message send to "+rep+" from "+email);
return new ModelAndView(this.getSuccessView(),model);
}//End of doSubmitAction()
Where i am doing mistake can anybody point out my mistake.
iam new to spring .
please help me..
--suresh--
Jörg Heinicke
Jun 3rd, 2007, 01:25 PM
1. PLEASE use [C O D E][/C O D E] tags to get your source code formatted in this forum.
Map model=new HashMap();
model.put("commandBean",this.formBackingObject(req));
Why do you put this "model" together again? Spring is doing it for your. Just use errors.getModel().
For more information see this thread (http://forum.springframework.org/showthread.php?t=39374#6) regarding custom property editors. The same applies here as well.
Jörg
sureshsoftpjx
Jun 7th, 2007, 06:58 AM
Hi,
I followed your thread about errors.getModel(), and put it in the following Controller.
Code:
<code>
protected ModelAndView onSubmit(HttpServletRequest req,HttpServletResponse res,Object command,BindException errors) throws Exception
{
ContactForm cf=(ContactForm)command;
FileHandler fh=new FileHandler(logLocation);
String name = cf.getName();
String addr = cf.getAddress();
String tno = cf.getTeleNum();
String pcode = cf.getPostcode();
String plats = cf.getPlats();
String email = cf.getEmail();
String opmer=cf.getOpmer();
//System.out.println("\nsmtp host: \t "+smtpHost+"\n");
//System.out.println("\nname:\t"+name+"\n");
//Send logger output to our FileHandler.
logger.addHandler(fh);
//Request that every detail gets logged
logger.setLevel(Level.ALL);
String rep[]={"ratna@spind.com","praveen@spind.com"};
String msg="Naam : "+name+"\nAdres : "+addr+"\nPostcode : "+pcode+"\nPlaats : "+plats+"\nTelefoonnumber : "+tno+"\nEmail : "+email+"\nOpmerkingen : "+opmer;
try{
postMail(rep,"Form Mail",msg,"suresh@spind.com");
}
catch(MessagingException me){
logger.warning("Message cannot be sent to "+rep+" from "+email);
return new ModelAndView("statusmsg.jsp",errors.getModel());
}
//Log a simple INFO message.
logger.info("Message send to "+rep+" from "+email);
return new ModelAndView("statusmsg.jsp",errors.getModel());
}//End of doSubmitAction()
</code>
But still i am getting the same error.
Then i removed <spring:bind> tags in the jsp page. Then when i click the jsp it is viewing correctly. But when i click submit button it is giving the following error.
<code>
javax.servlet.ServletException: Servlet.init() for servlet formcontroller threw exception
at org.apache.catalina.core.StandardWrapper.loadServl et(StandardWrapper.java:1075)
at org.apache.catalina.core.StandardWrapper.allocate( StandardWrapper.java:701)
at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:186)
at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:562)
at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:974)
at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:205)
at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:562)
at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:974)
at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:177)
at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:151)
at org.apache.catalina.valves.ErrorDispatcherValve.in voke(ErrorDispatcherValve.java:171)
at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:149)
at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:172)
at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:562)
</code>
My jsp is:
<code>
<form name="feedback" action="statusmsg.html" method="post">
Amsterdamse woningcorporatie van woningen en bedrijfsruimte
<hr/>
<strong>Donec tincidunt accumsan quam. Aliquam <br/>metus leo, faucibus in, luctus eget, ornare id.</strong>
<table>
<tr>
<td>Naam</td>
<td>*
<input type="text" name="${status.expression}" value="${status.value}" />
</td>
</tr>
<tr>
<td >Adres</td>
<td >*
<input type="text" name="${status.expression}" value="${status.value}" />
</td>
</tr>
<tr>
<td >Postcode</td>
<td >*
<input type="text" name="${status.expression}" value="${status.value}" />
</td>
</tr>
<tr>
<td >Plaats</td>
<td >*
<input type="text" name="${status.expression}" value="${status.value}" />
</td>
</tr>
<tr>
<td >Telefoonnummer</td>
<td >*
<input type="text" name="${status.expression}" value="${status.value}" />
</td>
</tr>
<tr>
<td >Email</td>
<td >*
<input type="text" name="${status.expression}" value="${status.value}"/>
</td>
</tr>
<tr>
<td >Opmerkingen</td>
<td > <textarea name="opmer" cols="20" rows="4"></textarea></td>
</tr>
</table>
<br/>Velden gemarkeerd met een * zijn verplicht</div>
<input type="submit" value="Verzend knop" />
</form>
</code>
I am not understanding what the error is:
Would u please give the answer for this..
-suresh--
Jörg Heinicke
Jun 7th, 2007, 10:39 AM
javax.servlet.ServletException: Servlet.init() for servlet formcontroller threw exception
at org.apache.catalina.core.StandardWrapper.loadServl et(StandardWrapper.java:1075)
at org.apache.catalina.core.StandardWrapper.allocate( StandardWrapper.java:701)
This error is even still inside Tomcat. No idea where it goes wrong, but it is not directly related to the stuff we had up to now. I'd guess the URL you are posting your form to is wrong. Or it's your setup of the formcontroller. At least formcontroller should not be tried to be loaded as servlet.
Jörg
Marten Deinum
Jun 7th, 2007, 10:55 AM
duplicate/hijacing of thread. The original thread is here (http://forum.springframework.org/showthread.php?t=39770). In which I gave an answer and suggested him twice that he has to take a look at the sample apps.
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.