0

I'm beginner in web development and try to create a simple spring mvc application.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <display-name>Spring Web MVC Application</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

mvc-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8" ?>
<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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <mvc:annotation-driven/>

    <context:component-scan base-package="org.test.test.controller"/>
    <context:component-scan base-package="org.test.test.model"/>

    <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>

controller

@Controller
@Scope(value = "singleton")
public class T1Controller {

    @RequestMapping(value = "/hello.do")
    public ModelAndView helloWorld() {
        System.out.print("Hi");
        String message = "Hello World, Spring 3.0!";
        return new ModelAndView("hello", "message", message);
    }
}

I have written everything correct like my tutorial but still get 404 tomcat 8.5RC error. I thing controller method is not called.

I have add spring 4 libs and I thing everything was fine but I don't see Hi output in Intellij IDEA.

hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>one</title>
</head>
<body>
Hello World
${message}
</body>
</html>

I see my index.jsp page when tomcat is run ... but when try to go http://localhost:8000/hello.do getting 404 error :(

Farshadi
  • 143
  • 1
  • 12
  • Do you get any error or exception when you start your server? – Amit Mahajan Aug 15 '17 at 19:54
  • would you please show the path of `hello.jsp` ? – sunkuet02 Aug 15 '17 at 19:56
  • Please post a link to your tutorial... Also, in `web.xml`, I see that you used `*.do` as url pattern. This means that `dispatcher` is not your default servlet... Did you properly set up thing for that ? – Jason Krs Aug 15 '17 at 20:14
  • No , I don't get any exeption @amitmah . hello.jsp is very simple html code and write (Hello word ) in body tag – Farshadi Aug 15 '17 at 20:15
  • @JasonKrs my tutorial is video and not english (persian),and ablut *.do I just follow that tutorial. – Farshadi Aug 15 '17 at 20:18
  • @FarshadAsgharzade, I think you have a file `hello.jsp` but where is it ? Is it under `WEB-INF` ? Give me its path – sunkuet02 Aug 15 '17 at 20:23
  • @FarshadAsgharzade, I want to see the file path of `hello.jsp`. In many case, it is under `src/main/webapp/WEB-INF/hello.jsp`. – sunkuet02 Aug 15 '17 at 20:28
  • @sunkuet02 inside /web/hello.jsp , behind index.jsp https://ibb.co/jjJZGa also I upload my route directories – Farshadi Aug 15 '17 at 20:28
  • 1
    I thing controller not called , because I don't see `System.out.print("Hi");` in Intellij IDEA – Farshadi Aug 15 '17 at 20:36
  • When tomcat is run, What url is shown in the `url bar` ? – sunkuet02 Aug 15 '17 at 21:13
  • @sunkuet02 http://localhost:8000/ – Farshadi Aug 15 '17 at 21:18
  • would you please try http://localhost:8000/hello.jsp there ? – sunkuet02 Aug 15 '17 at 21:24
  • Farshad, based on the code above, it looks like perhaps you are using a very old tutorial (or else the person creating the tutorial is using some very old techniques e.g. ".do" extensions and spring XML config). Either way, I'd strongly suggest looking into a different more modern tutorial, perhaps one of the spring getting started guides. e.g. https://spring.io/guides/gs/spring-boot/ https://spring.io/guides/gs/serving-web-content/ – Ben M Aug 15 '17 at 21:27
  • @FarshadAsgharzade Please even if it's in persian post the link so we can see.... There are configuration elements in you dispatcher servlet config and and you web application context config that seems odd to me...For instance, your view resolver prefix, the url pattern, the context configuration in web.xml...etc...post your link and a screen shot of your project structure – Jason Krs Aug 15 '17 at 21:32
  • thanks for answer my question , i try all way and try other tutorial and get new exception ... I like java and Spring But can not make a simple app ... I try again .... tanks again :* – Farshadi Aug 24 '17 at 13:51

1 Answers1

0

First I would start by scanning only one base package instead of two. Remove the controller map from the path aswel Like this:

<context:component-scan base-package="org.test.test"/>

Get rid of the following because only one will suffice. You dont need two.

<context:component-scan base-package="org.test.test.model"/> 

And modify your property value as follows:

    <property name="prefix">
        <value>/WEB-INF/</value>
    </property>
AchillesVan
  • 4,156
  • 3
  • 35
  • 47