我现在正在使用JSP在Spring中编写Web应用程序,但是重定向存在问题。
当我在编辑个人资料网站中
http://localhost:8080/Kulcz/users/userName/edit
我提交了将我重定向到的表格
http://localhost:8080/Kulcz/users/userName/users/userName
如果我在editPassword网站上更改
action="users/${user.login} to action="/users/${user.login}
它重定向到
http://localhost:8080/users/userName
提交后,我想要将我重定向回用户个人资料视图:
http://localhost:8080/Kulcz/users/userName/
用户控制器代码:
@Controller
@RequestMapping(value ="users")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping (params ="dodaj")
public String createForm(Model model) {
model.addAttribute("user", new User());
return "user/dodaj";
}
@RequestMapping (method = RequestMethod.POST)
public String saveUser(@ModelAttribute("user") @Valid User user, BindingResult result) {
if(result.hasErrors()) {
return "user/dodaj";
}
userService.saveUser(user);
return "redirect:/users/"+ user.getLogin();
}
@RequestMapping (value = "/{login}")
public String getUserProfile(@PathVariable String login, Map<String,Object> model) {
User user = userService.findUser(login);
model.put("user", user);
return "user/view";
}
@RequestMapping (value = "/{login}/edit", method = RequestMethod.GET)
public String editUserPassword(@PathVariable("login") String login, Map<String,Object> model) {
User user = userService.findUser(login);
model.put("user", user);
return "user/editPassword";
}
@RequestMapping (value = "/{login}", method = RequestMethod.POST)
public String updateUs(@ModelAttribute("user") @Valid User user, BindingResult result) {
if(result.hasErrors()) {
return "user/dodaj";
}
userService.updateUser(user);
return "redirect:/users/" + user.getLogin();
}
editPassword.jsp
<form:form name="input" method="post" modelAttribute="user" action="/users/${user.login}">
<form:input type="hidden" path="id"/>
Login: <form:input type ="text" path="login" readonly="true"/> <br/>
<form:errors path="login"/>
Hasło: <form:input type ="text" path="password"/> <br/>
<form:errors path="password"/>
Flaga: <form:input type ="hidden" path="flaga"/> <br/>
<form:errors path="flaga" />
Saldo: <form:input type ="hidden" path="saldo"/> <br/>
<form:errors path="saldo"/>
<input type ="submit" value ="Zmień">
</form:form>