我的控制器:
@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.getUser());
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, Object> model = new HashMap<String, Object>();
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");
}
}
在 Jsp 页面 Welcome.jsp 中:
<p>Welcome: ${user_sess}</p>
//不知道如何使用会话变量,因为在jsp页面中没有收到值。这是一个使用数据库mysql的简单spring mvc 3 hibernate项目。请帮助我