Announcement Announcement Module
No announcement yet.
Form as View - Forwarding to another Controller Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Form as View - Forwarding to another Controller

    Hello -

    I am fairly new to the Spring framework and have been working w/ it for the past couple of months. I selected it as the web framework for an off-hours project I am doing since it touts "simplicity" and that is kind of what I was looking for. I didn't really want the bloat of Struts for this particular project and had heard good things about Spring.

    Anyway, there is one simple thing that I am trying to do though that I just can't seem to find an example of anywhere.

    Basically, I have a search form where I would like the search form to remain in the same view (a JSP) as the results. Well, everything works fine and dandy until after the user submits their query and I try and hyrdrate the same JSP (as the form is in) w/ my results.

    The error I am getting seems to be related to the fact that the JSP no longer has access to the command object (i.e. formBackingObject) since i didn't route through the controller. I am also using the "bind" tag in my JSP.

    I also tried using a redirectView to go back to the controller and this doesn't work because then I lose my request scoped data that I put into the ModelAndView w/ the search results (the form backed object is created though).

    This seems simple (I know in Struts you can chain actions together to accomplish this) but I must be missing something.

    Any help at all would be greatly appreciated!
    Thanks, Greg

  • #2
    what do you mean by this "...since i didn't route through the controller..."

    Maybe post some code?

    You're right in not using redirect view since that will reset your form state.


    • #3
      This seems simple (I know in Struts you can chain actions together to accomplish this) but I must be missing something.
      You can forward to another controller using the forward: prefix. This requires Spring 1.1.3.


      • #4
        What about using the interface Controller instead of the class SimpleFormController?
        I did this way:
        public class UnidadeController implements Controller {
            private final Log log = LogFactory.getLog(UnidadeController.class);
            private UnidadeManager mgr = null;
            public void setUnidadeManager(UnidadeManager unidadeManager) {
                this.mgr = unidadeManager;
             public ModelAndView handleRequest(HttpServletRequest request,
                                              HttpServletResponse response)
            throws Exception {
                if (log.isDebugEnabled()) {
                    log.debug("entering 'handleRequest' method...");
                String cdClassificacaoInstitucional = null; 
                String cdEntidade = null;
                String dcEntidade = null;
                Map model = new HashMap();
        		cdClassificacaoInstitucional = request.getParameter("cdClassificacaoInstitucional");        
        		cdEntidade = request.getParameter("cdEntidade");
        		dcEntidade = request.getParameter("dcEntidade");
                //Primeira vez que este formulario for requisitado, a consulta será efetuada com valores default
                if (cdClassificacaoInstitucional == null){
                  [u]model.put("entidades"[/u], mgr.getEntidadesPorCdClassificacao(new Integer(2)));        
                   [u] model.put(Constants.UNIDADE_LIST[/u], mgr.getEntidadesPorCdClassificacao(new Integer(2)));        
                }else	if (cdEntidade == null){
                        model.put("entidades", mgr.getEntidadesPorCdClassificacao(Integer.valueOf(cdClassificacaoInstitucional)));        
                        model.put(Constants.UNIDADE_LIST, mgr.getEntidadesPorCdClassificacao(Integer.valueOf(cdClassificacaoInstitucional)));        
                	}else	if(cdEntidade == null || dcEntidade.length() == 0) {        
                            model.put("entidades", mgr.getEntidadesPorCdEntidadeMembroDE(Integer.valueOf(cdEntidade)));        
                            model.put(Constants.UNIDADE_LIST, mgr.getEntidadesPorCdEntidadeMembroDE(Integer.valueOf(cdEntidade)));        
                		}else  if (log.isDebugEnabled()) {
                                log.debug("Requisicoes subsequentes, neste caso o usuario OPTOU PELA CONSULTA PERSONALIZADA"+dcEntidade);
                			model.put("entidades", mgr.getEntidadesPorDcEntidade(Integer.valueOf(cdEntidade),dcEntidade));        
                            model.put(Constants.UNIDADE_LIST, mgr.getEntidadesPorDcEntidade(Integer.valueOf(cdEntidade),dcEntidade));        
                return new ModelAndView("unidadeList",model );
        I hope this help!


        • #5
          Originally posted by irbouho
          You can forward to another controller using the forward: prefix. This requires Spring 1.1.3.
          Please I don't understand, can you make a sample? forward:virtual address of controller ?
          One another question: I can use forward: also with SimpleMappingExceptionResolver ?


          • #6
            If you want to return your command object to your form using a SimpleFormController, set sessionForm(true) for your controller then return showForm(request, errors, view) from your onSubmit method rather than returning a new ModelAndView. This will put the command object back into the session and make it available to the spring:bind tags.