简体   繁体   English

RequestParam和会话Spring MVC

[英]RequestParam and sessions Spring MVC

i have one question i'm using 我有一个问题我正在使用

  @RequestMapping(value = "/user", method = RequestMethod.GET)
  public String getUser(@RequestParam("id"), Model model){

  }

this means that when i'll give the url/user?id=1 这意味着当我给url / user?id = 1时

that will give me back some data. 这会给我一些数据。 Now in the login i'm taking this id and i save it in a session. 现在在登录中我正在使用此ID并将其保存在会话中。

   session.setAttribute("userId", result.getBody().getId());

and i want when a user click at a tab automatically to be bind the id with the attribute stored in the session. 我希望当用户自动点击选项卡以将id与存储在会话中的属性绑定时。 ie lets say that the userId has as value 1. Then when i'll click on the tab to redirected automatically to:url/user?id=1 How can i do this? 即,假设userId具有值1.然后当我点击选项卡自动重定向到:url / user?id = 1我该怎么做?

For the tab i have this one: 对于标签我有这个:

<a href="<c:url value='/user'/>"

you can use: 您可以使用:

<a href="<c:url value='/user?id=${userId}'/>"> ...</a>

and for SpringMVC, I suggest you use the Restful url like /usr/1 , the action can be 对于SpringMVC,我建议你使用像/usr/1这样的Restful url,动作可以

@RequestMapping(value = "/user/{id}")
public String show(@PathVariable Long id, Model model) {
  ...
}

//link
<a href="<c:url value='/user/${userId}'/>"> ...</a>

Like @donnior asnwered @RequestMapping(value = "/user/{id}") is the best thing I have come accross. 就像@donnior所说@RequestMapping(value = "/user/{id}")是我遇到过的最好的事情。 It provides a very clean and secure way to navigate to a url in Spring. 它提供了一种非常干净和安全的方式来导航到Spring中的URL。

I recommend you to use that. 我建议你使用它。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM