![](/img/trans.png)
[英]How to get querystring passed value from the url into spring controller?
[英]How to save the passed parameter in the URL for use in different controller in Spring?
在一頁中,我有一個數據表。 當單擊用戶時,用戶ID作為URL值usersInfo?getid = 1傳遞
我有一個像這樣的控制器:
@RequestMapping(value = "users/userInfo", method = RequestMethod.GET, params = {"getId"})
@ResponseBody
public ModelAndView getClientStat(@RequestParam(value="getId", required = true) String getId) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User user = userService.findUserByEmail(auth.getName());
long clientId=0;
clientId=Long.parseLong(getId);
Client client=clientService.getClientById(clientId);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("userName", user.getName() + " " + user.getLastName());
modelAndView.addObject("userId", user.getId());
modelAndView.addObject("id", client.getName()+ " " + client.getLastName());
modelAndView.setViewName("users/userInfo");
return modelAndView;
}
然后,我使用傳遞的對象ID(具有名稱和姓氏)來顯示用戶名。
然后在打開的用戶/ userInfo頁面上,我有一個按鈕,該按鈕將發送到另一個頁面以填寫表格。 問題是:如何將這些表單的字段保存到數據庫中,但如何保存我在控制器中具有其ID的客戶端。
如何傳遞此值? 還是有更好的邏輯方法呢? 我對使用Spring還是很陌生,所以如果有不清楚的地方,我會提前致歉,但是我真的很感謝一些指導。 謝謝
您可以將@PathVariable與addAttribute一起使用
@RequestMapping(value = "users/edit/{userId}", method = RequestMethod.GET)
public ModelAndView getClientStat(@PathVariable(value="userId", required = true) String userId) {
//Use the userId to find user information and pass
modelAndView.addAttribute("id", userId)
modelAndView.setViewName("users/edit");
return modelAndView;
}
在您的視圖中,您可以將userId添加為隱藏字段<input type="hidden" name="userId" value={userId} />
,這取決於您使用的百里香或jsp視圖
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.