0

I am working on Spring MVC sample. when i use the / it works well. but when it comes to /xxx, it return WARNING. WARNING: No mapping found for HTTP request with URI [/SimpleSpringMVC/index] in DispatcherServlet with name 'report'

I had searched many answers, but all of them don't work well.

And here are details of my sample.

web.xml

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:tool.xml</param-value>
 </context-param>

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

 <servlet>
  <servlet-name>report</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/report-servlet.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
  
   <servlet-mapping>
    <servlet-name>report</servlet-name>
    <url-pattern>/</url-pattern>
   </servlet-mapping>

report-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:utils="http://www.springframework.org/schema/util"
 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 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd 
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util.xsd">

 <mvc:annotation-driven />


 <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/views/" />
  <property name="suffix" value=".jsp" />
 </bean>

</beans>

SimpleController.java

package com.tian.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller("Report")
public class SimpleController {
 
 public SimpleController() {
  System.out.println("Initial ...");
 }
 
 @RequestMapping("/index")
 public String home() {
  return "home";
 }
 
 
 @RequestMapping(value = "/info", method = RequestMethod.GET)
 public @ResponseBody String getInfo() {
  return "hello world";
 }
 
}

tool.xml

<?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:context="http://www.springframework.org/schema/context"
 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.1.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
                    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

 <context:annotation-config />
 
 <context:component-scan base-package="com.tian.controller" />


</beans>
Juntian
  • 3
  • 1
  • 2
  • I would guess tool.xml is not loaded at all as it is not included in web.xml. You can add multiple values under contextConfigLocation param-values by separating them with newlines. This way you can have multiple xml files for configuration. – Kaur Kase Nov 28 '14 at 08:54

2 Answers2

0

Ok, i don' t know what is tool.xml for, but i would do something like this, put this line in report-servlet.xml <context:component-scan base-package="com.tian.controller" /> before <mvc:annotation-driven /> then in your web.xml put something like this :

  <servlet>
        <servlet-name>report</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

 <servlet-mapping>
   <servlet-name>report</servlet-name>
   <url-pattern>/</url-pattern>
 </servlet-mapping>
  <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

And take a look at this controller,

@Controller
@RequestMapping(value="/login")
public class LoginController {
    @RequestMapping(method=RequestMethod.POST)
    @ResponseBody
    public String receiveLoginRequest(@RequestParam String username,@RequestParam String password){
//DO WHAT YOU WANT
    }
}

And when you test a controller, localhost:8080/report/login/YOURMETHODMAP

Davide Quaglio
  • 751
  • 2
  • 11
  • 31
  • thank you for your answer, i just put into report-servlet.xml, and it works well. but i wanna to know that the reason why i have to do it? since ContextLoaderListener is the root context, all child container, such as report-servlet.xml, share it. – Juntian Nov 29 '14 at 04:30
  • all controllers must in xxx-servlet.xml.http://stackoverflow.com/a/3652125/4302844 – Juntian Nov 29 '14 at 04:35
0

You want to understand how to config your context my friend so instead of giving you a botched answer that doesn't really help you, here is a beautiful answer : ANSWER BY informatik01, giving you a nice and simple lecture

Long story short web application contexts are hierarchical and your mapping is ignored (more or less)

Hope this helps.

Community
  • 1
  • 1
Tahar Bakir
  • 716
  • 5
  • 14