0
votes

I am new to spring, and I just want going to the following url http://localhost:8080/DispatcherExample/dispatcher/welcome to take me to the index.jsp page. DispatcherExample is the name of the project, dispatcher is the url of the dispatcher servlet and welcome is the url mapped to the controller method. Here are my classes:

WelcomeController.java

package com.paymon;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class WelcomeController {

    @RequestMapping("/welcome")
    public ModelAndView welcome()
    {
        System.out.println("welcome entered");
        ModelAndView model = new ModelAndView();
        model.setViewName("index");
        return model;
    }
}

web.xml

<web-app>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/dispatcher/</url-pattern>
    </servlet-mapping>
</web-app>

dispatcher-servlet.xml

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

   <context:component-scan base-package="com.paymon" />
<mvc:annotation-driven />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix">
            <value></value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

</beans>
1
What's your question?Slava Semushin
How can I get my controller view to be displayed?Paymon Wang-Lotfi

1 Answers

1
votes

There are 2 things you have to check.

First, location of your jsp file. If it is in root folder then its fine otherwise put the location of jsp file in prefix.

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

Second, if your jsp file is in root folder then it can be accessed using following url.

http://localhost:8080/DispatcherExample/welcome

If you want your url to be the following

http://localhost:8080/DispatcherExample/dispatcher/welcome

Then add @RequestMapping("/dispatcher/welcome")

Note:

  • DispatcherExample is the name of the war/project
  • Don't get confused with servlet-name dispatcher, it has no contribution in url, it is just used to identify that servlet in web.xml

Advice:

  • If you are new, use spring boot and annotation based configuration, the xml configuration is a primitive thing now.