Попробуйте сопоставить ваш пример с моим рабочим решением ниже
web.xml по пути spring-tutorial-mvc \ WebContent \ WEB-INF
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>spring-tutorial-mvc</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- - Location of the XML file that defines the root application context.
- Applied by ContextLoaderListener. -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/app-ctx.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--
- Servlet that dispatches request to registered handlers (Controller implementations).
-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
app-ctx.xml, как показано ниже, путь: spring-tutorial-mvc \ WebContent \ WEB-INF
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<context:component-scan`enter code here` base-package="com.spring.tutorial" />
</beans>
mvc-config.xml по пути spring-tutorial-mvc \ WebContent \ WEB-INF
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.spring.tutorial"/>
<mvc:annotation-driven />
<!-- 2. HandlerMapping : Used default handler mapping internally -->
<!-- 3. ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
UserController.java по пути spring-tutorial-mvc \ src \ com \ spring \ tutorial \ controller
package com.spring.tutorial.controller;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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 org.springframework.web.servlet.ModelAndView;
import com.giggal.spring.domain.User;
import com.giggal.spring.service.UserService;
@Controller
@RequestMapping(value = "/user/")
public class UserController {
@Autowired
UserService userService; // Service which will do all data
// retrieval/manipulation work
// -------------------Retrieve All
// Users--------------------------------------------------------
@RequestMapping(value = "/getAllUsers/", method = RequestMethod.GET)
public ModelAndView listAllUsers() {
ModelAndView mav = new ModelAndView("user/user_list");
List<User> users = userService.findAllUsers();
mav.addObject("users", users);
return mav;
}
// -------------------Retrieve Single
// User--------------------------------------------------------
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
public User getUser(@PathVariable("id") long id) {
System.out.println("Fetching User with id " + id);
User user = userService.findById(id);
return user;
}
@RequestMapping(value = "/add/", method = RequestMethod.GET)
public ModelAndView createUserForm() {
System.out.println("Inside add...");
ModelAndView mav = new ModelAndView("user/user_form");
User user = new User();
mav.addObject("command", user);
mav.addObject("action", "Add Record");
return mav;
}
// //-------------------Create a
// User--------------------------------------------------------
//
@RequestMapping(value = "/create/", method = RequestMethod.POST)
public ModelAndView createUser(@ModelAttribute("command") User user, HttpSession session) {
System.out.println("Creating User " + user.getName());
ModelAndView mav = new ModelAndView("user/master/user_form");
try {
if (userService.isUserExist(user)) {
System.out.println("A User with name " + user.getName() + " already exist");
// return new ResponseEntity<Void>(HttpStatus.CONFLICT);
}
if (user.getId() == null) {
userService.saveUser(user);
mav.setViewName("redirect:../add/?successMsg=Add successfully");
} else {
User currentUser = userService.findById(user.getId());
currentUser.setName(user.getName());
currentUser.setAge(user.getAge());
currentUser.setSalary(user.getSalary());
userService.updateUser(currentUser);
mav.setViewName("redirect:../add/?successMsg=Update successfully");
}
} catch (Exception ex) {
ex.printStackTrace();
mav.addObject("command", user);
mav.addObject("errorMsg", "Failed to add / update record");
}
return mav;
}
// ------------------- Delete a User
// --------------------------------------------------------
@RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
public void deleteUser(@PathVariable("id") long id) {
System.out.println("Fetching & Deleting User with id " + id);
User user = userService.findById(id);
userService.deleteUserById(id);
}
// ------------------- Delete All User
// --------------------------------------------------------
@RequestMapping(value = "/user/", method = RequestMethod.DELETE)
public void deleteAllUsers() {
System.out.println("Deleting All Users");
userService.deleteAllUsers();
}
}
Здесь URL будет, например, http://localhost://user/getAllUsers
порт: порт вашего сервера
context-root - это контекстный путь вашего веб-приложения