Announcement Announcement Module
Collapse
No announcement yet.
RequestMaping calling two times for one submit Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • RequestMaping calling two times for one submit

    Ciao Tutti,

    I have one problem in the Spring MVC . As i am new to it.

    I am using the annotation in the controller

    PHP Code:
    @Controller
    public class WelcomeAnnotatedController {

        
    Log log LogFactory.getLog(this.getClass());

        @
    Autowired
        
    private SecondBean secondBean;
        
    //it will call the method, when the submitting the form
        
    @RequestMapping(value="/actionName.htm",method RequestMethod.POST)
        public 
    ModelAndView onSubmit() {
            
    System.out.println("<<<<<<<<<<<<<onSubmit POST WelcomeAnnotatedController>>>>>>>>>>>>>>>>>>>>>>>");
            
    String message secondBean.getFirstBean().foo();
            
    System.out.println("<<<<<<<<<<<<<Message>>>>>>>>>>>>>>>>>>>>>>>" message );
            
    ModelAndView mav = new ModelAndView("myview").addObject("message",message);
            return 
    mav;
        }

        @
    ModelAttribute("userList")
        public List<
    StringpopulateUserList() {
            
    ArrayList<String> list = new ArrayList<String>();
            list.
    add("Rahul");
            list.
    add("Joy");
            list.
    add("Anand");
            return list;
        }

        @
    RequestMapping(method RequestMethod.GET)
        public 
    String showForm(ModelMap map) {
            
    map.addAttribute("message""Hello,This is My First Jsp");
            
    System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<showUserForm GET WelcomeAnnotatedController>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            
    User user = new User();
            
    map.addAttribute("user"user);
            return 
    "myview";
        }

        @
    RequestMapping(value="/backUser.htm",method RequestMethod.POST)
        public 
    String backToWelcome() {
            
    System.out.println("going back to welocme page");
            return 
    "welcome";
        } 
    Whenever i am calling the RequestMapping method, its calling two times, here when i am trying to addUser, its tried to insert two times(Sometime).

    I am not able to figure out what is the problem. Whenever i am calling the same RequestMapping continously 3-4 times,suppose in this case @RequestMapping(value="/backUser.htm",method = RequestMethod.POST), then it will call two times.
    output of the RequestMapping as
    <<<<<<<<<<<<<<<<<<<<<<<<<showUserForm GET WelcomeAnnotatedController>>>>>>>>>>>>>>>>>>>>>>>> >>>
    going back to welocme page
    <<<<<<<<<<<<<<<<<<<<<<<<<showUserForm GET WelcomeAnnotatedController>>>>>>>>>>>>>>>>>>>>>>>> >>>
    going back to welocme page
    <<<<<<<<<<<<<<<<<<<<<<<<<showUserForm GET WelcomeAnnotatedController>>>>>>>>>>>>>>>>>>>>>>>> >>>
    going back to welocme page
    <<<<<<<<<<<<<<<<<<<<<<<<<showUserForm GET WelcomeAnnotatedController>>>>>>>>>>>>>>>>>>>>>>>> >>>
    going back to welocme page
    <<<<<<<<<<<<<<<<<<<<<<<<<showUserForm GET WelcomeAnnotatedController>>>>>>>>>>>>>>>>>>>>>>>> >>>
    going back to welocme page
    <<<<<<<<<<<<<<<<<<<<<<<<<showUserForm GET WelcomeAnnotatedController>>>>>>>>>>>>>>>>>>>>>>>> >>>
    going back to welocme page
    going back to welocme page


    Please help me , because , i am going to use SPRING MVC first time.

    Thanks
    Rahul
    Last edited by sunflower_rahul; Jan 23rd, 2010, 06:45 AM.

  • #2
    Can you post your web.xml and your web application config file?

    Comment


    • #3
      web.xml

      PHP Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <web-app id="WebApp_ID" version="2.4"
          xmlns="http://java.sun.com/xml/ns/j2ee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

          <display-name>SampleSpringApplication</display-name>

          <context-param>
              <param-name>log4jConfigLocation</param-name>
              <param-value>/WEB-INF/log4j.properties</param-value>
          </context-param>

          <context-param>
              <param-name>log4jRefreshInterval</param-name>
              <param-value>1000</param-value>
          </context-param>

          <listener>
              <listener-class>
                  org.springframework.web.util.Log4jConfigListener
              </listener-class>
          </listener>

          <listener>
              <listener-class>
                  org.springframework.web.context.ContextLoaderListener
              </listener-class>
          </listener>

          <!--<servlet>
              <servlet-name>firstSample</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
              </servlet>
              
              <servlet-mapping>
              <servlet-name>firstSample</servlet-name>
              <url-pattern>*.htm</url-pattern>
              </servlet-mapping>-->

          <servlet>
              <servlet-name>SimpleFormSubmit</servlet-name>
              <servlet-class>
                  org.springframework.web.servlet.DispatcherServlet
              </servlet-class>
              <load-on-startup>2</load-on-startup>
          </servlet>

          <servlet-mapping>
              <servlet-name>SimpleFormSubmit</servlet-name>
              <url-pattern>*.htm</url-pattern>
          </servlet-mapping>


          <welcome-file-list>
              <welcome-file>/WEB-INF/jsp/redirect.jsp</welcome-file>
              <!-- <welcome-file>/jsp/welcome.jsp</welcome-file> -->
          </welcome-file-list>



      </web-app>
      SimpleFormSubmit-servlet.xml

      PHP Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:p="http://www.springframework.org/schema/p"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

          <bean id="viewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="viewClass"
                  value="org.springframework.web.servlet.view.JstlView" />
              <property name="prefix" value="/WEB-INF/jsp/" />
              <property name="suffix" value=".jsp" />
              <property name="order" value="1" />
          </bean>

          <bean id="tilesConfigurer"
              class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"
              p:definitions="/WEB-INF/tiles-defs.xml" />

          <bean id="userValidator"
              class="com.rahul.samplespringex01.validator.UserValidator" />

          <bean id="userService"
              class="com.rahul.samplespringex01.service.UserServiceImpl" />

          <context:component-scan
              base-package="com.rahul.samplespringex01.controller" />

          <bean
              class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

          <bean
              class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

      </beans>
      applicationcontext.xml

      PHP Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

      <!--
        - A simple root application context.  See firstSample-servlet.xml for the 
        - Web context.
      -->
      <beans>
          <bean id="firstBean" class="com.rahul.samplespringex01.bean.FirstBean"/>
          <bean id="secondBean" class="com.rahul.samplespringex01.bean.SecondBean">
              <property name="firstBean" ref="firstBean"/>
          </bean>

          <bean id="dataSource" destroy-method="close"
              class="org.apache.commons.dbcp.BasicDataSource">
              <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
              <property name="url" value="jdbc:oracle:thin:@sys:2483/sys" />
              <property name="username" value="svil_team26" />
              <property name="password" value="99999" />
          </bean>
          
          <bean id="UserDao" class="com.rahul.samplespringex01.service.UserDaoImpl">
              <property name="dataSource" ref="dataSource"/>
          </bean>
           
      </beans>

      Comment


      • #4
        My Others Controller

        Hi,

        Flow is like that.... From loginController to WelcomeAnnotedController and then UserOperationController. I already pasted the WelcomeAnnotedController above .


        PHP Code:
        package com.rahul.samplespringex01.controller;

        import javax.servlet.http.HttpServletRequest;

        import org.apache.commons.logging.Log;
        import org.apache.commons.logging.LogFactory;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Controller;
        import org.springframework.ui.ModelMap;
        import org.springframework.validation.BindingResult;
        import org.springframework.web.bind.annotation.ModelAttribute;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RequestMethod;
        import org.springframework.web.bind.annotation.SessionAttributes;
        import org.springframework.web.bind.support.SessionStatus;

        import com.rahul.samplespringex01.bean.User;
        import com.rahul.samplespringex01.service.UserService;
        import com.rahul.samplespringex01.validator.UserValidator;

        @
        Controller
        @SessionAttributes("user")
        public class 
        LoginController {

            @
        Autowired
            
        private UserService userService;

            @
        Autowired
            
        private UserValidator userValidator

            
        Log log LogFactory.getLog(this.getClass());

            @
        RequestMapping(method RequestMethod.GET)
            public 
        String showUserForm(ModelMap model) {
                
        //putting into modelmap,for command object, which we can access in the jsp
                
        User user = new User();
                
        model.addAttribute(user);
                
        //log.debug("<<<<<<<<<<<<<<<<<<<<<<<<<showUserForm>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                
        System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<showUserForm GET LoginController>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                return 
        "Login";
            }

            @
        RequestMapping(value="/login.htm",method RequestMethod.POST)
            
        //ModelAttribute used to get the request attribute into one object,in this case we have user, 
            //that we already set in to GET method for initiate
            
        public String onSubmit(@ModelAttribute("user")User user,
                    
        BindingResult result,SessionStatus status,HttpServletRequest request) {
                
        System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<User Name>>>>>>>>>>>>>>>>>>> LoginController"user.getName());
                
        userValidator.validate(userresult);
                if (
        result.hasErrors()) {
                    return 
        "Login";
                } else {
                    
        userService.add(user);    
                    
        //We have commented because, it will nullify the session objects
                    //,in this case we have a user object
                    //status.setComplete();
                
        }
                return 
        "forward:welcome.htm";
            }

            @
        RequestMapping(value="/logout.htm",method RequestMethod.POST)
            public 
        String onLogout(SessionStatus status) {
                
        System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<Logout>>>>>>>>>>>>>>>>>>>LoginController");
                
        status.setComplete();
                return 
        "redirect:redirect.htm";
            }


        PHP Code:
        package com.rahul.samplespringex01.controller;

        import org.apache.commons.logging.Log;
        import org.apache.commons.logging.LogFactory;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Controller;
        import org.springframework.ui.ModelMap;
        import org.springframework.validation.BindingResult;
        import org.springframework.web.bind.annotation.ModelAttribute;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.support.SessionStatus;

        import com.rahul.samplespringex01.bean.User;
        import com.rahul.samplespringex01.service.UserDao;
        import com.rahul.samplespringex01.validator.UserValidator;

        @
        Controller
        public class UserOperationController {

            
        Log log LogFactory.getLog(this.getClass());

            @
        Autowired
            
        private UserDao dao;

            @
        Autowired
            
        private UserValidator userValidator

            @
        RequestMapping("/User.htm")
            public 
        String User(ModelMap map){
                
        User user = new User();
                
        map.addAttribute("addUser"user);
                
        System.out.println("Going to Add User Page" user);
                return 
        "AddUser";
            }
            
        //on submit the form , this method will call  
            
        @RequestMapping("/addUser.htm")
            public 
        String addUser(@ModelAttribute("addUser"User user,
            
        //public String addUser(User user,
                    
        BindingResult resultSessionStatus status,ModelMap map){
                
        System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<Userobecjt>>>>"user);
                
        System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<Userpassword>>>>>"user.getPassword());
                
        System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<Username>>>>>"user.getName());
                
        userValidator.validate(userresult);
                if (
        result.hasErrors()) {
                    
        System.out.println("validation error status >>>> "+status.isComplete());
                    return 
        "AddUser";
                } else {
                    
        dao.addUser(user);
                    
        System.out.println("validation sucees status >>>> "+status.isComplete());
                    
        map.addAttribute("userDetail"dao.selectUser());
                    return 
        "UserDetail";    
                }
            }

            
        /*@RequestMapping("/modifyUser.htm") 
            public String modifyUser(@ModelAttribute("user")User user,ModelMap map){
                log.debug("<<<<<<<<<<<<<<<<<<<<<modifyUser>>>>>>"+ user);
                dao.updateUser(user);
                map.addAttribute("Update", "Updated User");
                return "UserView";
            }

            @RequestMapping("/deleteUser.htm") 
            public String deleteUser(@ModelAttribute("user")User user,ModelMap map){
                log.debug("deleteUser>>>>>>"+ user);
                dao.updateUser(user);
                map.addAttribute("Update", "Updated User");
                return "UserView";
            }*/

            
        @RequestMapping("/selectUser.htm"
            public 
        String selectUser(ModelMap map){
                
        System.out.println("<<<<<<<<<<<<<<<<<<<get all the User>>>>>>");
                
        map.addAttribute("userDetail"dao.selectUser());
                return 
        "UserDetail";
            }

            
        /*@RequestMapping(method = RequestMethod.GET) 
            public String getUser(ModelMap map){
                System.out.println("commandUser>>>>>>>>");
                User user = new User(); 
                map.addAttribute("user", user);
                return "UserDetail";
            }*/

            /*@RequestMapping("/cancelUser.htm") 
            public String cancelUser(ModelMap map){
                map.addAttribute("userDetail", dao.selectUser());
                System.out.println("cancel user ");
                return "UserDetail";
            }*/

            
        @RequestMapping(value="/cancelViewUser.htm"
            public 
        String cancelViewUser(User user){
                
        System.out.println("cancel in the view page" user);
                return 
        "myview";
            }


        Please reply back, as i m stucking from last 2 days.....

        Thanks
        Rahul

        Comment


        • #5
          Request Mapping problem in the Annotated Controller

          Hi,

          is anyone can solve this problem? i am waiting for the reply

          thanks
          rahul

          Comment


          • #6
            Try removing the DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter from your servlet config. Those should be registered by default with the DispatcherServlet in your web.xml if you're running Java 5+. So if that's the case, those bean definitions are redundant and could be causing the double requests.

            Comment


            • #7
              Hi Pgrimard ,

              Thanks for replying back, Still i have the same problem.

              Please do the needful.

              Thanks
              Rahul

              Comment


              • #8
                According to your log output in your first post, it only seems to happen some of the time and not all the time. Are you able to determine what's different about the times it calls twice and the times it doesn't call twice?

                Comment


                • #9
                  Originally posted by pgrimard View Post
                  According to your log output in your first post, it only seems to happen some of the time and not all the time. Are you able to determine what's different about the times it calls twice and the times it doesn't call twice?

                  It can happen(calling twice) first time also, can happen after 2-3 times also...

                  Comment


                  • #10
                    This is the welcome.jsp

                    PHP Code:
                    <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
                    <%@ 
                    taglib prefix="c" uri="http://java.sun.com/jstl/core" %>    
                    <%@
                    page import="com.rahul.samplespringex01.bean.User"%>
                    <
                    html>
                    <
                    head>
                    <
                    title>Spring Sample</title>
                    </
                    head>
                    <
                    body>
                    <
                    form method="POST">
                    Welcome <%=((User)(session.getAttribute("user"))).getName()%>
                    <
                    table>
                        <
                    tr>
                            <
                    td><a href="logout.htm">Logout</a></td>
                        </
                    tr>
                    </
                    table>
                    <
                    hr>
                    <
                    table>
                        <
                    tr>
                            <
                    td><a href="actionName.htm">Click here for User CRUD</a></td>
                        </
                    tr>
                    </
                    table>
                    </
                    form>
                    </
                    body>
                    </
                    html
                    myview.jsp
                    PHP Code:
                    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
                    <%@ 
                    taglib prefix="c" uri="http://java.sun.com/jstl/core" %>    
                    <%@
                    page import="com.rahul.samplespringex01.bean.User"%>
                    <
                    html>
                    <
                    head>
                    <
                    meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
                    <
                    title>Welcome to Spring!</title>
                    </
                    head>
                    <
                    body>
                    Hi <%=((User)(session.getAttribute("user"))).getName()%>
                    <
                    table>
                        <
                    tr>
                            <
                    td><a href="logout.htm">Logout</a></td>
                        </
                    tr>
                    </
                    table>
                    <
                    hr>
                    <
                    form:form method="POST">
                    <
                    table>
                    <
                    tr><td><b><%=request.getAttribute("message")!=null?request.getAttribute("message"):""%> </b></td></tr>
                    <
                    tr><td>
                     <
                    input type="submit" value="Add User" onclick="this.form.action='User.htm';this.form.submit()">
                     <
                    input type="submit" value="View User" onclick="this.form.action='selectUser.htm';this.form.submit()">
                      <
                    input type="submit" value="Cancel" onclick="this.form.action='backUser.htm';this.form.submit()">
                    </
                    td></tr>
                     </
                    table>
                    </
                    form:form>
                    </
                    body>
                    </
                    html
                    Now, when i am submitting the myview.jsp. Cancel button, it will go to the welcome.jsp...

                    This is one of the flow, what i have given first post

                    please see it ... do reply me

                    Comment


                    • #11
                      Not sure why you need a <form method="POST"> in your welcome page. I don't think that's needed since all you have are anchor links on that page.

                      Comment


                      • #12
                        Hi, I have found the problem ,,,,, i think....

                        There two submit buttin , i am submiiting two time,one through javascript then via form submit too....

                        <input type="submit" value="Cancel" onclick="this.form.action='backUser.htm';this.form .submit()">

                        it should be like that ...

                        <input type="button" value="Cancel" onclick="this.form.action='backUser.htm';this.form .submit()">

                        I think,Am i correct?

                        Ok , i have one more doubt...

                        Hw i can have more than one submit button , how i can send the actionname... above i have used using javascript... is any another way....

                        Comment


                        • #13
                          The submit button might be part of the problem if you're also calling form.submit() in the onclick event. You could leave it as a type="submit" and just remove the form.submit() from the event. Not an expert on javascript thought. You could also create a javascript method that decides what action to use based on a form element.

                          Comment


                          • #14
                            Originally posted by pgrimard View Post
                            The submit button might be part of the problem if you're also calling form.submit() in the onclick event. You could leave it as a type="submit" and just remove the form.submit() from the event. Not an expert on javascript thought. You could also create a javascript method that decides what action to use based on a form element.
                            That's what i did..

                            onclick="this.form.action='backUser.htm';this.form .submit()"

                            How i can pass more than one action(action url name above like that "backUser.htm") without using the javascript....
                            ..
                            if you have any idea about that...

                            plz let me know....

                            Comment

                            Working...
                            X