2015-11-24 139 views
2

我有警告:警告:沒有找到具有名稱爲'dispatcher'的DispatcherServlet中具有URI [/ Project/testAddUser]的HTTP請求的映射找不到映射名稱爲'dispatcher'的DispatcherServlet中的URI []的HTTP請求映射

我找不到錯在哪裏。我引用了許多教程,我認爲我的項目已正確配置。任何人都可以幫忙提前致謝!

這是我的web.xml文件。

<context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/applicationContext.xml /WEB-INF/hibernateContext.xml</param-value> 
    </context-param> 
    <servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value></param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 


    <servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
    </servlet-mapping> 

這是我的applicationContext.xml。 我試過<context:component-scan base-package="com.plainart.member.controller" />,但會發生其他錯誤。所以我把它作爲評論來擺脫這一點。

<context:annotation-config /> 
<!-- 
<context:component-scan base-package="com.plainart.member.controller" /> 
--> 
<bean id="jspViewResolver" 
    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> 

我控制器

package com.plainart.member.controller; 

import java.util.Map; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.validation.BindingResult; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

import com.plainart.member.model.User; 
import com.plainart.member.service.UserService; 

@Controller 
public class UserController { 

    @Autowired 
    private UserService userService; 

    @RequestMapping("/index") 
    public String listUsers(Map<String,Object> map){ 
     map.put("user",new User()); 
     map.put("userList",userService.listUser()); 
     return "index"; 
    } 

    @RequestMapping(value ="/") 
    public String index(){ 
     return "index"; 
    } 


    @RequestMapping("/testAddUser") 
    public String testAddUser(){ 
     User testUser = new User(); 
     testUser.setFirstName("John"); 
     testUser.setLastName("Smith"); 
     userService.addUser(testUser); 
     return "testUser"; 
    } 

    @RequestMapping(value = "/addUser", method = RequestMethod.POST) 
    public String addUser(@ModelAttribute("user")User user,BindingResult result){ 
     userService.addUser(user); 
     return "redirect:/index"; 
    } 

    @RequestMapping("/deleteUser") 
    public String deleteUser(@PathVariable("userId")Integer id){ 
     userService.removeUser(id); 
     return "redirect:/index"; 
    } 
} 

my directory tree

回答

0

您需要創建調度-servlet.xml文件,並把在那裏,你控制器封裝。

<context:annotation-config /> 
<context:component-scan base-package="com.plainart.member.controller" /> 
<mvc:annotation-driven /> 

然後在web.xml文件

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> 
</context-param> 

其映射我認爲你有inititalizing你的配置位置的問題。 以下列方式試用

<context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/hibernateContext.xml, /WEB-INF/dispatcher-servlet.xml</param-value> 
    </context-param> 
+0

感謝您的跟蹤。我嘗試過,但現在它帶有一個404錯誤。我的@RequestMapping有什麼不對嗎? – Gnostikoi

相關問題