Java 在SpringMVC3中使用会话
我在SpringMVC3中使用会话时遇到了一个问题。我不知道如何使用它。当我点击按钮提交。。它不起作用。。。我只是春季mvc的新手。。。 这是我的示例代码: 登录控制器:Java 在SpringMVC3中使用会话,java,spring,session,spring-mvc,Java,Spring,Session,Spring Mvc,我在SpringMVC3中使用会话时遇到了一个问题。我不知道如何使用它。当我点击按钮提交。。它不起作用。。。我只是春季mvc的新手。。。 这是我的示例代码: 登录控制器: @Controller @SessionAttributes({"account"}) public class LoginController { @Autowired private CatalogService catalogSerivce; @Autowired private AccountService acco
@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");
}
}
@控制器
@会期贡献({“账户”})
公共类登录控制器{
@自动连线
私人目录服务目录服务;
@自动连线
私人帐户服务帐户服务;
@请求映射(value=“/login”)
公共字符串列表(模型){
List listCatalog=catalogSerivce.getListCatalog();
model.addAttribute(“目录”,listCatalog);
返回“登录”;
}
@RequestMapping(value=“/login”,method=RequestMethod.POST)
公共模型和视图登录(@RequestParam(value=“tbusername”)字符串用户名,
@RequestParam(value=“tbpassword”)字符串密码,
@ModelAttribute(value=“account”)账户{
ModelAndView ModelAndView=新建ModelAndView();
account=accountSerivce.authenticate(用户名、密码);
modelAndView.addObject(“account”,account);
返回新的ModelAndView(“索引”);
}
}
索引控制器
@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";
}
}
@控制器
@会期贡献({“账户”})
公共类索引控制器{
@自动连线
私人目录服务目录服务;
@自动连线
私人产品服务;
@RequestMapping(值={“/”索引“,”})
公共字符串列表(模型){
List listCatalog=catalogSerivce.getListCatalog();
List listProduct=productService.searchProductByCatalog(“,”);
model.addAttribute(“目录”,listCatalog);
model.addAttribute(“产品”,列表产品);
返回“索引”;
}
}
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>
用户名密码
这个问题的答案将告诉您何时使用@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");
}
}
什么不工作?我不知道当我点击按钮登录,它不工作。。。它没有在indexI尝试调试中接收会话,但它没有跳转到方法login…Tks。。我试过了,但效果很好…@ThanhDuyNgo,不客气。