0

I am creating a simple spring mvc app. How can I configure the spring DispatcherServlet to accept url pattern like below.

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SpringMVC</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app> 

Currently I am getting below warning on starting the app.

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

spring-servlet.xml (I have added the default servlet handler as well)

<?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:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" 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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.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.springapp" /> <mvc:default-servlet-handler/> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="classpath:resources/messages" /> <property name="defaultEncoding" value="UTF-8" /> </bean> <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/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans> 
6
  • Like what? cannot tell what your question is. Commented Jun 30, 2012 at 19:54
  • 1
    maybe this could help Commented Jun 30, 2012 at 19:56
  • please add the spring context Commented Jun 30, 2012 at 19:56
  • It is accepting the URL pattern. The Spring dispatcher servlet is invoked, and then tries to find a request handler in its set of controllers which is configured to handle requests with the URI /SpringMVC/, but doesn't find any. The log you're seeing comes from the DispatcherServlet. It doesn't come from the web container. Commented Jun 30, 2012 at 21:32
  • I've updated the question with spring context. Commented Jul 2, 2012 at 8:23

4 Answers 4

2

First of all, your web.xml should contain DispatcherServlet mapping (you can provide dispatcher-servlet.xml file location or use default):

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

add this to your spring-servlet.xml file:

<mvc:annotation-driven/> 

And major step,you should create controller which will be mapped to your path /SpringMVC e.g.

package com.springapp.controllers; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/SpringMVC") public class MyController{ @RequestMapping(method= RequestMethod.GET) public ModelAndView springMvcTest(ModelMap modelMap){ return new ModelAndView("test"); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

<mvc:annotation-driven/> helped me. Thank you very much!
1

Try to use one of the following:

<servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/SpringMVC/*</url-pattern> </servlet-mapping> 

Or:

<servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.form</url-pattern> </servlet-mapping> 

1 Comment

Ok I tried the first way and it is taking me to the app's home page (index.jsp), but links on that page are not working. <a href="hello">Say Hello</a> <a href="contacts">Contact Manager</a>
0

You have to map this url in controller as follows:

 @RequestMapping("/SpringMVC") public ModelAndView index(HttpServletRequest req,HttpServletResponse){ // ur business logic return new ModelAndView("index"); } 

Comments

0

in web xml

 <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>/</url-pattern> </servlet-mapping> 

and in your controller:

@Controller @RequestMapping("SpringMVC") public class MyController { Logger logger = LoggerFactory.getLogger(MyController.class); @Autowired private ContentService contentService; @RequestMapping(value = "/index", method = RequestMethod.GET) public String createForm(@RequestParam("user") String user, @RequestParam("requestId") String requestId, Model uiModel) { //TODO return "index"; } 

and then your url would be ... localhost:port/SpringMVC/index

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.