Spring MVC DispatcherServlet Mapping

时间:2012-06-30 19:52:36

标签: java spring-mvc

我正在创建一个简单的spring mvc app。如何配置spring DispatcherServlet以接受如下所示的url模式。

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

目前我在启动应用时遇到警告。

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

spring-servlet.xml(我也添加了默认的servlet处理程序)

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

4 个答案:

答案 0 :(得分:1)

尝试使用以下方法之一:

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

或者:

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

答案 1 :(得分:1)

首先,您的web.xml应该包含DispatcherServlet映射(您可以提供dispatcher-servlet.xml文件位置或使用默认值):

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

将其添加到spring-servlet.xml文件中:

<mvc:annotation-driven/>

重要的一步,您应该创建将映射到您的路径/ 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");
     }
}

答案 2 :(得分:0)

您必须按如下方式将此网址映射到控制器中:

 @RequestMapping("/SpringMVC")
   public ModelAndView index(HttpServletRequest req,HttpServletResponse){

     // ur business logic

   return new ModelAndView("index");  
  }

答案 3 :(得分:0)

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>

并在您的控制器中:

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

然后你的url将是... localhost:port / SpringMVC / index