【问题标题】:Using session in spring mvc 3 [duplicate]在spring mvc 3中使用会话[重复]
【发布时间】:2013-11-21 18:03:06
【问题描述】:

我在 spring mvc3 中使用 session 时遇到问题。我不知道如何使用它。当我点击按钮提交时..它不起作用......我只是spring mvc的新手...... 这是我的示例代码:

登录控制器:

@Controller
@SessionAttributes({"account"})
public class LoginController {

@Autowired
private CatalogService catalogSerivce;
@Autowired
private AccountService accountSerivce;

@RequestMapping(value = "/login")
public String list(Model model) {
    List<Catalog> listCatalog = catalogSerivce.getListCatalog();
    model.addAttribute("catalogs", listCatalog);
    return "login";
}

@RequestMapping(value = "/login", method = RequestMethod.POST)
public ModelAndView login(@RequestParam(value = "tbusername") String username,
        @RequestParam(value = "tbpassword") String password,
        @ModelAttribute(value = "account") Account account) {
    ModelAndView modelAndView = new ModelAndView();
    account = accountSerivce.authenticate(username, password);
    modelAndView.addObject("account", account);
    return new ModelAndView("index");
}
}

索引控制器

@Controller
@SessionAttributes({"account"})
public class IndexController {

@Autowired
private CatalogService catalogSerivce;

@Autowired
private ProductService productService;

@RequestMapping(value = {"/index", ""})
public String list(Model model){
    List<Catalog> listCatalog = catalogSerivce.getListCatalog();
    List<Product> listProduct = productService.searchProductByCatalog("", "");
    model.addAttribute("catalogs", listCatalog);
    model.addAttribute("products", listProduct);
    return "index";
}
}

login.jsp

<form action='login' method='post'>
<label>Username</label> <input type='text' class='field'
    name='tbusername' /> <label>Password</label> <input
    type='password' class='field' name='tbpassword' />
<input type='submit' class='search-submit' name='login'
    value='Login' />
<p>
    <a href='register' class='bul'>Don't have an account</a><br />
</form>

【问题讨论】:

  • 这里有什么问题?
  • 我不知道什么时候点击按钮登录,它不起作用...它没有收到索引中的会话
  • 我尝试调试但它没有跳转到方法登录...

标签: java spring session spring-mvc


【解决方案1】:

SO discussion 的答案会让您了解何时使用@SessionAttributes。以下是从@SessionAttributes doc 复制的摘录:

注意:使用此注释指示的会话属性对应 到特定处理程序的模型属性,透明地存储 在会话会话中。这些属性将在 处理程序指示其会话会话的完成。所以, 将此功能用于此类会话属性 应该在会话过程中临时存储在会话中 特定处理程序的对话。

对于永久会话属性,例如一个用户认证对象, 改用传统的 session.setAttribute 方法。

因此,要使用永久会话属性,请将您的控制器方法更改为:

@Controller
public class LoginController {
...
@RequestMapping(value = "/login", method = RequestMethod.POST)
public ModelAndView login(@RequestParam(value = "tbusername") String username,
        @RequestParam(value = "tbpassword") String password,
        @ModelAttribute(value = "account") Account account,
        HttpSession session) {
    ModelAndView modelAndView = new ModelAndView();
    account = accountSerivce.authenticate(username, password);
    //modelAndView.addObject("account", account);
    session.setAttribute("account", account);
    return new ModelAndView("index");
}
}

【讨论】:

  • Tks.. 我试了一下,效果很好......
  • @ThanhDuyNgo 欢迎您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-29
  • 2016-07-28
  • 1970-01-01
  • 2011-11-12
  • 2011-07-07
  • 1970-01-01
相关资源
最近更新 更多