@RequestMapping(value=”/user/login”,method=RequestMethod.POST)
public String login(String name,String password)throws Exception{
ApplicationContext ctx = new ClassPathXmlApplicationContext(“spring-hibernate.xml”);
menuService = (IMenuService) ctx.getBean(“menuService”);
User u = userService.login(name, password);
if (u!=null) {
//通过用户查询出全部的menu
List<Menu> menus = menuService.queryMenuByUserId(String.valueOf(u.getId()));
//不知道该怎么样修改,求指导!
//在jsp就可以迭代相应的list
return “success”;
}
return “user/login”;
}
//顺便看下jsp页面怎么迭代本人的菜单信息
<%@ page language=”java” contentType=”text/html; charset=UTF-8″
pageEncoding=”UTF-8″%>
<%@ taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Insert title here</title>
</head>
<body>
<div >
<c:forEach var=”url” items=”${menus }” >
${url.title }
</c:forEach>
</div>
</body>
</html>
public String login(String name,String password)throws Exception{
ApplicationContext ctx = new ClassPathXmlApplicationContext(“spring-hibernate.xml”);
menuService = (IMenuService) ctx.getBean(“menuService”);
User u = userService.login(name, password);
if (u!=null) {
//通过用户查询出全部的menu
List<Menu> menus = menuService.queryMenuByUserId(String.valueOf(u.getId()));
//不知道该怎么样修改,求指导!
//在jsp就可以迭代相应的list
return “success”;
}
return “user/login”;
}
//顺便看下jsp页面怎么迭代本人的菜单信息
<%@ page language=”java” contentType=”text/html; charset=UTF-8″
pageEncoding=”UTF-8″%>
<%@ taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Insert title here</title>
</head>
<body>
<div >
<c:forEach var=”url” items=”${menus }” >
${url.title }
</c:forEach>
</div>
</body>
</html>
解决方案
20
@RequestMapping(value=”/user/login”,method=RequestMethod.POST)
public String login(HttpServletRequest request,String name,String password)throws Exception{
ApplicationContext ctx = new ClassPathXmlApplicationContext(“spring-hibernate.xml”);
menuService = (IMenuService) ctx.getBean(“menuService”);
User u = userService.login(name, password);
if (u!=null) {
//通过用户查询出全部的menu
List<Menu> menus = menuService.queryMenuByUserId(String.valueOf(u.getId()));
request.setAttribute(“menus”,menus)
return “success”;
}
return “user/login”;
}
public String login(HttpServletRequest request,String name,String password)throws Exception{
ApplicationContext ctx = new ClassPathXmlApplicationContext(“spring-hibernate.xml”);
menuService = (IMenuService) ctx.getBean(“menuService”);
User u = userService.login(name, password);
if (u!=null) {
//通过用户查询出全部的menu
List<Menu> menus = menuService.queryMenuByUserId(String.valueOf(u.getId()));
request.setAttribute(“menus”,menus)
return “success”;
}
return “user/login”;
}
20
public String login(String name,String password)throws Exception这里添加个Model model
public String login(String name,String password,Model model)throws Exception
然后menus 下面添加model.addAttribute(“menus”,menus );
这么写就可以了,或把方法类型改为ModelAndView
ModelAndView mv = new ModelAndView();
mv.addObject(“menus “, menus );
mv.setViewName(“success”);
return mv;
当然你也可以想楼上说的那样放request
public String login(String name,String password,Model model)throws Exception
然后menus 下面添加model.addAttribute(“menus”,menus );
这么写就可以了,或把方法类型改为ModelAndView
ModelAndView mv = new ModelAndView();
mv.addObject(“menus “, menus );
mv.setViewName(“success”);
return mv;
当然你也可以想楼上说的那样放request