Thursday, March 31, 2011

Passing form parameters to Spring controller from a FormPanel

Hello,

I'll like some help on how to get form parameters from HTTP POST request to a Spring Controller within a FormPanel. Below is the code I'm working with:

public class SignupWidget extends Composite {

    private Logger logger = Logger.getLogger(SignupWidget.class.getName());

    FlowPanel signupWrapper = new FlowPanel();
    VerticalPanel signupPanel = new VerticalPanel();
    FormPanel frontSignupFormPanel = new FormPanel();
    
    private Label signupEmailLbl = new Label("Email");
    private TextBox signupemailTextBox = new TextBox();
    
    private Label signupPasswordLbl = new Label("Password");
    private PasswordTextBox signuppasswordTextBox = new PasswordTextBox();

    private Label signupListBoxLbl = new Label("I'm a");
    private ListBox typeListBox = new ListBox();

    private Button mainSignUpButton = new Button("Sign up");

public SignupWidget() {

initWidget(signupWrapper);
        
        signupWrapper.add(frontSignupFormPanel);
        signupWrapper.setStyleName("gwt-signupPanel");
        
        frontSignupFormPanel.setAction("signup/account");
        frontSignupFormPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
        frontSignupFormPanel.setMethod(FormPanel.METHOD_POST);
        frontSignupFormPanel.setWidget(signupPanel);
        
        signupPanel.add(signupEmailLbl);
        signupemailTextBox.setName("email");
        signupPanel.add(signupemailTextBox);
        
        signupPanel.add(signupPasswordLbl);
        signuppasswordTextBox.setName("pass");
        signupPanel.add(signuppasswordTextBox);

signupPanel.add(signupListBoxLbl);
        typeListBox.addItem("Agent");
        typeListBox.addItem("Buyer");
        typeListBox.setName("type");
        signupPanel.add(typeListBox);

signupPanel.add(mainSignUpButton);

signup().addClickHandler(new ClickHandler() {
            @Override
          public void onClick(ClickEvent event) {
            logger.info("onClick is called");
            GWT.log("onClick is called" + signupemailTextBox.getValue());
            frontSignupFormPanel.submit();
          }
        });

frontSignupFormPanel.addSubmitHandler(new FormPanel.SubmitHandler() {
          public void onSubmit(SubmitEvent event) {
              logger.info("onSubmit is called");

// How can I pass the field parameters to the Spring Controller
          }
        });

frontSignupFormPanel.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
          public void onSubmitComplete(SubmitCompleteEvent event) {
              logger.info("onSubmitComplete is called");
              GWT.log("onSubmitComplete is called" + signupemailTextBox.getValue());
              final RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, GWT.getHostPageBaseURL() + "signup/account");
                    try {
                        rb.sendRequest(null, new RequestCallback(){
                        @Override
                        public void onResponseReceived(Request request, Response response) {
                            if (response.getStatusCode() == 200) {

                                Window.Location.assign(rb.getUrl());
                            } else {
                                Window.alert(response.getStatusText());
                            }
                        }

                        @Override
                        public void onError(Request request, Throwable exception) {
                            Window.alert("onError, request is: "+request.toString());
                        }
                });
                }catch(RequestException re) {
                    logger.severe("RequestException occurred");
                    GWT.log("RequestException occurred");
                }
          }
        });

}

//Can be done via MVP
public HasClickHandlers signup() {
        return mainSignUpButton;
    }

}

/* The SpringMVC Controller class */

@RequestMapping("/signup/account")
public class SignupAccountTypeController extends SimpleFormController {

    private static final Logger log = Logger.getLogger(SignupAccountTypeController.class.getName());

    private AgentEntity  agententity;
    private BuyerEntity buyerentity;

    public void setAgententity(AgentEntity agententity) {
        this.agententity = agententity;
    }

    public void setBuyerentity(BuyerEntity buyerentity) {
        this.buyerentity = buyerentity;
    }
    
    public SignupAccountTypeController() {

        log.info("Constructor: constructing SignupAccountTypeController");
    }
    
    @Override
    public ModelAndView handleRequestInternal(HttpServletRequest request,
                                  HttpServletResponse response)
                           throws Exception {
       
        log.info(request.toString());
        log.info(response.toString());
        
        if(request.getParameter("type") == null) {
        log.info("handleRequest is called in SignupAccountTypeController. NULL");
        }else {
            log.info("handleRequest is called in SignupAccountTypeController. NOT NULL");
        }
        Map agentData = new HashMap();
        Map buyerData = new HashMap();
        agententity = new AgentEntity();
        BuyerEntity buyerentity = new BuyerEntity();
        agentData.put("agententity", agententity);
        buyerData.put("buyerentity", buyerentity);

        String type = request.getParameter("type");

        if(type.equals("Agent")) {
            log.info("Account type == Agent");
            setCommandClass(AgentEntity.class);
            setCommandName("agententity");
            setSuccessView("agent");
        return new ModelAndView("agent", agentData);
        }else {
            return new ModelAndView("buyer");
        }
        
    }
}

On Submit, data is persisted successfully (implemented via RPC), but I'm getting a NullPointerException which is also impeding the URL redirect to /signup/account. I have tested the Spring Controller class outside of GWT and it's working perfectly.

[java] WARNING: /signup/account
     [java] java.lang.NullPointerException
     [java]         at org.opevel.web.SignupAccountTypeController.handleRequestInternal(SignupAccountTypeController.java:80)
     [java]         at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
     [java]         at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
     [java]         at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
     [java]         at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
     [java]         at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
     [java]         at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
     [java]         at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
     [java]         at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
     [java]         at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
     [java]         at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
     [java]         at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:58)
     [java]         at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
     [java]         at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
     [java]         at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
     [java]         at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:122)
     [java]         at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
     [java]         at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
     [java]         at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
     [java]         at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
     [java]         at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
     [java]         at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
     [java]         at com.google.apphosting.utils.jetty.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:70)
     [java]         at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
     [java]         at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:351)
     [java]         at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
     [java]         at org.mortbay.jetty.Server.handle(Server.java:326)
     [java]         at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
     [java]         at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
     [java]         at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:547)
     [java]         at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
     [java]         at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
     [java]         at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
     [java]         at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

Any ideas will be very much appreciated

Regards
--
Odeyemi 'Kayode O.

B.Sc(Hons) Econs, Application Developer & Systems Engineer (Sun Certified Professional),
Oracle Certified Associate, Solaris Systems Administrator, Drupal Developer

Website: http://sinati.com
Socialize with me: http://profile.to/charyorde, http://twitter.com/charyorde,
http://www.google.com/profiles/dreyemi
Skype:drecute

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment