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";
}
}
感謝您的跟蹤。我嘗試過,但現在它帶有一個404錯誤。我的@RequestMapping有什麼不對嗎? – Gnostikoi