简体   繁体   中英

Spring MVC DispatcherServlet Mapping

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>

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 eg

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");
     }
}

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>

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");  
  }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM