Uruchamianie odpowiednich metod w widoku Spring MVC

0

Koledzy i koleżanki potrzebuje pewnego wyjaśnienia działania @Controller w Spring MVC.
Ponizej przedstawiam kody mojego @Service , @Controller i strony jsp.
Podstawowe pytanie to w jaki sposób uruchomić odpowiednia metode z @Controlera w odpowiednim formularzu na stronie tj.
Chciałbym żeby w

<form:form commandName="user">

uruchamiała sie metoda

@RequestMapping(value="/user", method=RequestMethod.POST)
	public String saveUser(User user)
	{
		
		userService.addUser(user);
     

     return "redirect:/user";
	}

natomiast w

<form:form commandName="userCreated">

metoda

@RequestMapping(value="/user", method=RequestMethod.POST)
	public String deleteUser(int id)
	{
		
		userService.deleteUser(id);
     

     return "redirect:/user";
	}

Moje klasy:

@Service
public class UserService {

	private User user;

	public static List<User> listUser = new ArrayList<User>();

	
	
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> showAllUser() {
		/*listUser.add(new User(1, "Konrad", "Dawydiuk","m",
				"[email protected]","davyd", "password"));*/
		return listUser;
	}

	public void addUser(User user) {

		listUser.add(user);

	}

	public void deleteUser(int id)
	{
		User userRemove = listUser.get(id);
		listUser.remove(userRemove);
	}
	
}

@Controller
public class HomeController {
	
	@Autowired
	private UserService userService;

	@RequestMapping(value = "/user", method = RequestMethod.GET)
	public String home(Model model) {
		
	
		
		model.addAttribute("showAllUser", userService.showAllUser() );
		model.addAttribute("user",new User());
		model.addAttribute("userCreated", userService.getUser());
		
		return "user";
	
	}
	
	@RequestMapping(value="/user", method=RequestMethod.POST)
	public String saveUser(User user)
	{
		
		userService.addUser(user);
     

     return "redirect:/user";
	}
	
	@RequestMapping(value="/user", method=RequestMethod.POST)
	public String deleteUser(int id)
	{
		
		userService.deleteUser(id);
     

     return "redirect:/user";
	}
	
}


 
 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ page session="false"%>
<html>
<head>
<title>Home</title>
</head>
<body>
	<h1>USER</h1>

	<table>
		<c:forEach items="${showAllUser}" var="user">
			<tr>
				<td>"${user.id}""</td>
				<td>"${user.firstName}""</td>
				<td>"${user.login}""</td>
			</tr>
		</c:forEach>
	</table>

	<form:form commandName="user">
		<table>

			<tr>
				<td>id</td>
				<td><form:input path="id" /></td>
			</tr>

			<tr>
				<td>firstName</td>
				<td><form:input path="firstName" /></td>
			</tr>

			<tr>
				<td>lastName</td>
				<td><form:input path="lastName" /></td>
			</tr>

			<tr>
				<td>sex</td>
				<td><form:input path="sex" /></td>
			</tr>

			<tr>
				<td>email</td>
				<td><form:input path="email" /></td>
			</tr>

			<tr>
				<td>login</td>
				<td><form:input path="login" /></td>
			</tr>

			<tr>
				<td>password</td>
				<td><form:input path="password" /></td>
			</tr>

			<tr>
				<td colspan="2"><input type="submit" value="Dodaj" /></td>
			</tr>

			<tr>
				<td colspan="2"><input type="submit" value="Usun" /></td>
			</tr>

		</table>
	</form:form>

	<form:form commandName="userCreated">

		<tr>
			<td>id</td>
			<td><form:input path="id" /></td>
		</tr>

		<tr>
			<td colspan="2"><input type="submit" value="Usun" /></td>
		</tr>
	</form:form>



</body>
</html>


0

Hej, nie możesz mieć dwóch metod w kontrolerze mapowanych tym samym value oraz method, natomiast form:form powinno mniej więcej tak wyglądać:

<form:form method="post"
				action="${pageContext.request.contextPath}/user"
				commandName="user">

1 użytkowników online, w tym zalogowanych: 0, gości: 1