Announcement Announcement Module
Collapse
No announcement yet.
MVC controller calls itself three times? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • MVC controller calls itself three times?

    Hi,

    I am a new. I use spring mvc 3.0 on Tomcat 5.5. When I type url, the controller itself calls three times. I do not understand what is the problem?
    === this is controller class ==
    public class ShowEventCtr implements Controller {

    protected final Log logger = LogFactory.getLog(getClass());
    private ShowEventSvc showEventSvc;

    public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
    //call Event service to pull out events on the current month, next moth, and so on
    logger.info(" i call events");
    List<Event> events = showEventSvc.getEvents();

    //render view
    Map<String, List<Event>> myModel = new HashMap<String, List<Event>>();
    myModel.put("event", events);
    return new ModelAndView("showEvents", "model", myModel);
    }

    public void setShowEventSvc(ShowEventSvc cal){
    this.showEventSvc = cal;
    }

    }
    ===== this is web.xml ==
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app 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" >


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

    <listener>
    <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
    </listener>


    <servlet>
    <servlet-name>suites</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>


    <servlet-mapping>
    <servlet-name>suites</servlet-name>
    <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
    <welcome-file>
    index.jsp
    </welcome-file>
    </welcome-file-list>

    <!--
    <error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/jsp/page-not-found.jsp</location>
    </error-page>

    <error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/WEB-INF/jsp/uncaught-error.jsp</location>
    </error-page>
    -->
    </web-app>

  • #2
    1. Please use code tags around your code and configuration files so it is easier to read on the forum.
    2. Please post the applicationContext.xml so the bean configurations can be reviewed.
    3. I'm fairly certain that handleRequest will get called with every *.do request since you have no path mapping, which could explain the multiple calls.
    4. Why aren't you using annotations?

    Comment


    • #3
      For path mapping, I use servlet xml file. For the code simplicity, I just do not use annotations.

      This is my servlet.xml file
      <?xml version="1.0" encoding="UTF-8"?>

      <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


      <!-- show calendar events -->
      <bean name="/showCal.do" class="controller.ShowEventCtr">
      <property name="showEventSvc" ref="showEventSvc" />
      </bean>
      <bean id="showEventSvc" class="service.ShowEventSvcImpl">
      <property name="eventDao" ref="eventDao"/>
      </bean>
      <bean id="sessionSvc" class="service.SessionSvcImpl">
      <property name="sessionDao" ref="sessionDao"/>
      </bean>
      <bean id="sessionDao" class="dao.SessionDao">
      <property name="dataSource" ref="dataSource"/>
      </bean>
      <bean id="eventDao" class="dao.EventDao">
      <property name="dataSource" ref="dataSource"/>
      </bean>



      <!-- email setting up -->
      <bean id="sendEmailSvc" class="service.SendEmailSvcImpl">
      <property name="mailSender" ref="mailSender"/>
      <property name="templateMessage" ref="templateMessage"/>
      <property name="guestDao" ref="guestDao"/>
      </bean>
      <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailS enderImpl">
      </bean>
      <bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage" >
      <property name="from" value="[email protected]" />
      <property name="subject" value="Terptation Suites Ordering Confirmation" />
      </bean>

      <bean id="xmlViewResolver" class="org.springframework.web.servlet.view.XmlVie wResolver">
      <property name="order" value="1" />
      </bean>

      <bean id="messageSource" class="org.springframework.context.support.Resourc eBundleMessageSource">
      <property name="basename" value="messages"/>
      </bean>

      <bean id="viewResolver" class="org.springframework.web.servlet.view.Intern alResourceViewResolver">
      <property name="viewClass"><value>org.springframework.web.se rvlet.view.JstlView</value></property>
      <property name="prefix"><value>/WEB-INF/jsp/</value></property>
      <property name="suffix"><value>.jsp</value></property>
      </bean>

      </beans>

      This is my applicationContext xml file
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:jee="http://www.springframework.org/schema/jee"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/aop
      http://www.springframework.org/schem...ng-aop-2.5.xsd
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schem...-beans-2.5.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schem...ontext-2.5.xsd
      http://www.springframework.org/schema/jee
      http://www.springframework.org/schem...ng-jee-2.5.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

      <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
      <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/>
      <property name="url" value="jdbc:jtds:sqlserver://xxxxxxxxxxxxxxxx:1433;databaseName=LSM"/>
      <property name="username" value="xxxxxxxxxxx"/>
      <property name="password" value="xxxxxxxxxx"/>
      </bean>

      </beans>

      Comment

      Working...
      X