想在Spring MVC 3中使用会话变量作为我的登录表单 [英] Want to use Session Variable for my login form in Spring MVC 3

查看:84
本文介绍了想在Spring MVC 3中使用会话变量作为我的登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制器:



@Controller

@RequestMapping(/ articles)

@SessionAttributes ({user_sess})

公共类ArticleController {

@Autowired

私人ArticleService文章服务;



@RequestMapping(value =/ save,method = RequestMethod.POST)

public ModelAndView saveArticle(@ModelAttribute(article)文章文章,

BindingResult结果){

ModelAndView modelAndView = new ModelAndView();



modelAndView.addObject(user_sess,article.getUser ());

System.out.println(article.getUser()+ article.get User());

articleService.addArticle(article); < br $>


// session.setAttribute(user_Sess,article.getUser());

//返回新的ModelAndView(redirect:/articles.html);

返回新的ModelAndView(欢迎);

}



@RequestMapping(method = RequestMethod.GET)

public ModelAndView listArticles(){

Map< string ,> model = new HashMap< string,>();

model.put(articles,articleService.listArticles());



返回新的ModelAndView(articlesList,model);

}



@RequestMapping(value =/ add,method = RequestMethod .GET)

public ModelAndView addArticle(@ModelAttribute(article)文章文章,

BindingResult结果){

返回新的ModelAndView( addArticle);

}

}



在Jsp页面中Welcome.jsp:



欢迎:$ {user_sess}

My Controller:

@Controller
@RequestMapping("/articles")
@SessionAttributes({"user_sess"})
public class ArticleController {
@Autowired
private ArticleService articleService;

@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView saveArticle(@ModelAttribute(" article ") Article article,
BindingResult result) {
ModelAndView modelAndView = new ModelAndView();

modelAndView.addObject("user_sess",article.getUser ());
System.out.println("article.getUser()"+article.get User());
articleService.addArticle( article);

// session.setAttribute("user_Sess", article.getUser());
// return new ModelAndView("redirect:/articles.html");
return new ModelAndView("Welcome");
}

@RequestMapping(method = RequestMethod.GET)
public ModelAndView listArticles() {
Map<string,> model = new HashMap<string,>();
model.put("articles", articleService.listArticles());

return new ModelAndView("articlesList", model);
}

@RequestMapping(value = "/add", method = RequestMethod.GET)
public ModelAndView addArticle(@ModelAttribute("article") Article article,
BindingResult result) {
return new ModelAndView("addArticle");
}
}

In Jsp page Welcome.jsp :

Welcome: ${user_sess}

推荐答案

{user_sess}
{user_sess}


这篇关于想在Spring MVC 3中使用会话变量作为我的登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆