2012-06-30 212 views
0

我正在創建一個簡單的spring mvc應用程序。我如何配置Spring DispatcherServlet來接受下面的url模式。Spring MVC DispatcherServlet映射

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

像什麼?不能說出你的問題是什麼。 – Affe

+1

也許[this](http://stackoverflow.com/q/7919840/1001027)可以幫助 –

+0

請加上spring context –

回答

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

好吧,我嘗試了第一種方式,它將我帶到應用程序的主頁(index.jsp),但該頁面上的鏈接無法正常工作。 'Say Hello Contact Manager' –

0

你必須映射這個網址在控制器如下:

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

    // ur business logic 

    return new ModelAndView("index"); 
    } 
0
在網頁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"; 
    } 

,然後你的網址是...本地主機:端口/用SpringMVC/index

0

首先,你的web.xml應該包含DispatcherServlet映射(你可以提供dispatcher-servlet.xm l文件位置或使用默認值):

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

添加到您的彈簧servlet.xml文件:

<mvc:annotation-driven/> 

而且重要的一步,你應該創建控制器將被映射到您的路徑/用SpringMVC 例如

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