2-角色

角色列表展示功能

步骤为:

①点击角色管理菜单发送请求到服务器端(修改角色管理菜单的url地址)

②创建RoleController和list()方法

③创建RoleService和list()方法

④创建RoleDao和findAll()方法

⑤使用JdbcTemplate完成查询操作

⑥将查询数据存储到modelAndView中

⑦转发到role-list.jsp页面进行展示

1.aside.jsp中修改角色管理菜单的url地址

<li><<li><a
   href="${pageContext.request.contextPath}/user/list"> <i
      class="fa fa-circle-o">i> 用户管理
a>li>

3.

public List<Role> getRoleListpublic List<Role> getRoleList() {
    return roleDao.getRoleList();
}

4.5.

public List<public List<Role> getRoleList() {
    if (jdbcTemplate != null){
        List<Role> list = jdbcTemplate.query("select * from sys_role", new BeanPropertyRowMapper<Role>(Role.class));
        return list;
    }
    return null;
}

2.6.

@RequestMapping(@RequestMapping("/role")
@Controller
public class RoleController {
    @Autowired
    private RoleService roleService;

    @RequestMapping("/list")
    public ModelAndView getList(){
        ModelAndView modelAndView = new ModelAndView();
        List roleList = roleService.getRoleList();
        modelAndView.addObject("roleList",roleList);
        System.out.println(roleList);
        modelAndView.setViewName("role-list");
        return modelAndView;
    }
}

7.role-list.jsp页面 修改

jsp配置

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

循环

<<tbody>
   <c:forEach items="${roleList}" var="role">
      <tr>
         <td><input name="ids" type="checkbox">td>
         <td>${role.id}td>
         <td>${role.roleName}td>
         <td>${role.roleDesc}td>
         <td class="text-center">
            <a href="javascript:void(0);" class="btn bg-olive btn-xs">删除a>
         td>
      tr>
   c:forEach>
tbody>
  • 有可能数据库时区没有配置,会出错You must configure either the server or JDBC driver (via the serverTimezone conf)

添加角色

操作步骤如下:

①点击列表页面新建按钮跳转到角色添加页面

②输入角色信息,点击保存按钮,表单数据提交服务器

③编写RoleController的save()方法

④编写RoleService的save()方法

⑤编写RoleDao的save()方法

⑥使用JdbcTemplate保存Role数据到sys_role

⑦跳转回角色列表页面

//daoimpl.java
public boolean save(Role role) {
    jdbcTemplate.update("insert into sys_role values (?, ?, ?)", null, role.getRoleName(), role.getRoleDesc());
    return true;
}
// controller.java    查完后重定向
@RequestMapping("/save")
public String save(Role role){
    roleService.save(role);
    return "redirect:/role/list";
}

乱码问题解决:添加过滤器。

因为是post方式.

展示用户列表

完成该功能的操作步骤:

①点击用户管理菜单发送请求到服务器端(修改用户管理菜单的url地址)

②创建UserController和list()方法

③创建UserService和list()方法

④创建UserDao和fifindAll()方法

⑤使用JdbcTemplate完成查询操作

⑥将查询数据存储到modelAndView中

⑦转发到user-list.jsp页面进行展示

controller

@RequestMapping(@RequestMapping("/list") 

public ModelAndView list(){ 

	List userList = userService.list(); 

	ModelAndView modelAndView = new ModelAndView(); 

	modelAndView.addObject("userList",userList); 

	modelAndView.setViewName("user-list"); 

	return modelAndView; 

}

service:

public List<User> listpublic List<User> list() {
       List userList = userDao.findAll();
       //封装userList中的每一个User的roles数据
       for (User user : userList) {
           //获得user的id
           Long id = user.getId();
           //将id作为参数 查询当前userId对应的Role集合数据
           List roles = roleDao.findRoleByUserId(id);
           user.setRoles(roles);
       }
       return userList;
   }

dao

public List<Role> findRoleByUserIdpublic List<Role> findRoleByUserId(Long id) {
    List roles = jdbcTemplate.query("select * from sys_user_role ur,sys_role r where ur.roleId=r.id and ur.userId=?", new BeanPropertyRowMapper(Role.class), id);
    return roles;
}

这里重点是多表查询,多次查询,dao层只做简单的单次sql, 多次操作数据库,放到业务层去实现,调用多次dao.


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!