0

I write site, and i have to use html page. When I write with jsp page I get good result, but I don't use HTML page because I get error:

INFO: Server startup in 4792 ms

WARNING: No mapping found for HTTP request with URI [/TestSpringMVC/] in DispatcherServlet with name 'HelloWeb'

WARNING: No mapping found for HTTP request with URI [/TestSpringMVC/index.html] in DispatcherServlet with name 'HelloWeb'

This is my welcome html page:

<!DOCTYPE html>
<html>
  <head>
   <meta charset="UTF-8">
   <title>Insert title here</title>
  </head>
  <body>
   <a href="home">click</a>
  </body>
</html>

This is my home.html page:

<body>
  <h1>this is home page</h1>
</body>

This is my web.xml

<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>Archetype Created Web Application</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
  </context-param>

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

   <servlet>
     <servlet-name>HelloWeb</servlet-name>
     <servlet-class>org.springframework.web.servlet.DispatcherServlet
     </servlet-class>
     <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
     <servlet-name>HelloWeb</servlet-name>
     <url-pattern>/</url-pattern>
   </servlet-mapping>  
</web-app>

HelloWeb-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-4.1.xsd
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-4.1.xsd
 http://www.springframework.org/schema/mvc    http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<context:component-scan base-package="main.test.spring" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/page/html/"/>
    <property name="suffix" value=".html"/>
</bean>

My controller:

    @Controller
    public class HelloController{
       @RequestMapping(value = "/home", method = RequestMethod.GET)
       public String printHellow() {
          return "page/html/home";
       }
    }

p.s. I use Tomcat 7

  • First try from browser `http://localhost:8080/TestSpringMVC/home` and see if it hits the controller. – Aniket Thakur Mar 30 '15 at 17:45
  • when i go to `http://localhost:8080/TestSpringMVC/home` I have `HTTP Status 404 description The requested resource is not available.` –  Mar 30 '15 at 17:52
  • Your tomcat server is running on port 8080 only ri8? Next change `return "page/html/home";` to `return "home"`; – Aniket Thakur Mar 30 '15 at 17:57

3 Answers3

0
 <url-pattern>/*.html</url-pattern>

 to add different type you need to add the type such as above code, and if you  
 were to add json then /*json should be added as well .Hope that solves your 
 problem
Moshiour
  • 633
  • 7
  • 20
  • if I add /*.html I have error `SEVERE: A child container failed during start java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/TestSpringMVC]]` –  Mar 30 '15 at 17:50
  • in your WEB-INF folder are are keeping your view File as .html or ,jsp ? – Moshiour Mar 30 '15 at 18:11
  • the error is rather generic isn't it? have you tried InternalResourceViewResolver instead of UrlBaseViewResolver? – Moshiour Mar 30 '15 at 18:15
  • in my WEB-INF i have .html files. No I have not tried InternalResourceViewResolver –  Mar 30 '15 at 18:23
0

Your servlet.xml has this: <property name="prefix" value="/WEB-INF/page/html/"/>

and you are returning "page/html/home" in your controller, so essentially that's the same as page/html/page/html/home.

You simply want to return "home"

Also you need a mapping for your webroot e.g.

@RequestMapping(value = "/", method = RequestMethod.GET)
public String home() {

    return "home";
}
0

There are couple of items you need to do. 1. Your HelloWeb-servlet.xml is missing <mvc:annotation-driven />. This is required for registering handler mapping and handler adapter.

    <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-4.1.xsd
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-4.1.xsd
     http://www.springframework.org/schema/mvc    http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
    <context:component-scan base-package="main.test.spring" />
<mvc:annotation-driven />
    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/page/html/"/>
        <property name="suffix" value=".html"/>
    </bean>`

2. Your view resolution needs to be corrected in the controller. Since you have defined /WEB-INF/page/html/ in your view resolver, you don't need that in your controller.

@Controller
    public class HelloController{
       @RequestMapping(value = "/home", method = RequestMethod.GET)
       public String printHellow() {
          return "home";
       }
    }

Assuming your tomcat listens at port 8080 and web context is TestSpringMVC, here is the URL you can try.

http://localhost:8080/TestSpringMVC/home
minion
  • 4,313
  • 2
  • 20
  • 35