出于某种原因,当我对某个URI执行GET请求时,我需要在该方法中访问的变量丢失其内存或指向null。
我有一个表单,用户可以在其中更新他的个人信息。但当他输入副本时,它会将他重定向到一个让他知道的页面
我有:private static volatile User currentUser;
当用户登录并且服务器对我自己编程的REST API执行GET请求时,将设置此字段,并返回包含其信息的用户。这按预期工作,用户信息显示在他的主屏幕上。
以上代码:
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@ModelAttribute Credentials credentials,
RedirectAttributes redirect) {
RestTemplate restTemplate = new RestTemplate();
RoleInfo roleInfo = restTemplate.postForObject(
"http://localhost:9090/users/login", credentials,
RoleInfo.class);
if (roleInfo != null) {
if (roleInfo.isAdmin()) {
redirect.addFlashAttribute("credentials", credentials);
return "redirect:/adminHome";
} else {
redirect.addFlashAttribute("credentials", credentials);
return "redirect:/getBasicUser";
}
} else {
return "login_fail";
}
}
@RequestMapping(value = "/getBasicUser", method = RequestMethod.GET)
public <T> String getBasicUser(@ModelAttribute Credentials credentials,
Model model, RedirectAttributes redirect) {
RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:9090/users/getBasicUser?username="
+ credentials.getUsername();
ResponseEntity<User> responseEntity = restTemplate.exchange(
url,
HttpMethod.GET,
new HttpEntity<T>(createHeaders(credentials.getUsername(),
credentials.getPassword())), User.class);
User user;
user = responseEntity.getBody();
currentUser = user;
System.out.println("current user: " + currentUser.getUsername());
if (user != null) {
userName = credentials.getUsername();
passWord = credentials.getPassword();
redirect.addFlashAttribute("credentials", credentials);
redirect.addFlashAttribute("user", user);
return "redirect:/basicHome";
} else {
return "register_fail";
}
}
所以&#34; basicHome&#34;他可以查看他的信息。此页面上还有一个表单链接,可以编辑信息:
@RequestMapping(value = "/edit", method = RequestMethod.GET)
public String getEditProfilePage(Model model) {
model.addAttribute("currentUser", currentUser);
System.out.println("current use firstname: " + currentUser.getFirstname());
model.addAttribute("user", new User());
return "edit_profile";
}
如果编辑成功,则会返回其主页并附上更新的信息。
当他输入无效信息时出现问题。他应该被重定向回&#34; / edit&#34; URI和currentUser
字段仍应保留其信息,但实际上为空。
这是&#34; / edit&#34; PUT功能:
@RequestMapping(value = "/edit", method = RequestMethod.PUT)
public <T> String editProfile(@ModelAttribute("user") User user,
@ModelAttribute("credentials") Credentials credentials,
RedirectAttributes redirect) {
RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:9090/users/update?username=" + userName;
HttpHeaders headers = createHeaders(userName,
passWord);
@SuppressWarnings({ "unchecked", "rawtypes" })
HttpEntity<T> entity = new HttpEntity(user, headers);
ResponseEntity<User> responseEntity = restTemplate.exchange(url,
HttpMethod.PUT, entity, User.class);
User returnedUser = responseEntity.getBody();
currentUser = returnedUser;
if (returnedUser != null) {
redirect.addFlashAttribute("user", returnedUser);
redirect.addFlashAttribute("credentials", credentials);
return "redirect:/basicHome";
} else {
return "redirect:/editFail";
}
}
答案 0 :(得分:0)
我想出了我必须做的事情。我基本上将“user”作为会话对象:@SessionAttributes(“user”)