第十六节——spring练习之页面上角色列表的展示

一、步骤设计

  1. 点击角色管理菜单发送请求到服务器端(修改角色管理菜单的url地址)
  2. 创建RoleController和showList()方法
  3. 创建RoleService和showList()方法
  4. 创建RoleDao和findAll()方法
  5. 使用JdbcTemplate完成查询操作
  6. 将查询数据存储到Model中
  7. 转发到role-list.jsp页面进行展示

二、具体设计

  1. 查看index.jsp,发现语句让我们重定向到了pages/main.jsp
	<%
		response.sendRedirect(request.getContextPath()+"/pages/main.jsp");
	%>
  1. 查看main.jsp,引入了侧边栏aside.jsp
<jsp:include page="aside.jsp"></jsp:include>
  1. 查看侧边栏,角色列表引入了一个静态页面
href="${pageContext.request.contextPath}/pages/role-list.jsp">
  1. 修改角色列表的引入地址,为一个controller方法的地址role/list,之后去controller层实现role/list方法
href="${pageContext.request.contextPath}/role/list">
  1. 在controller目录,新建一个RoleController.class
@RequestMapping("/role")
@Controller
public class RoleController {
    
    

	@Autowired
    private RoleService roleService;

    @RequestMapping("/list")
    public ModelAndView list(){
    
    
        ModelAndView modelAndView = new ModelAndView();
        List<Role> roleList =roleService.list();
        modelAndView.addObject("roleList",roleList);
        modelAndView.setViewName("role-list");
        return modelAndView;
    }
}
  1. 在service目录下创建接口RoleService.class
public interface RoleService {
    
    
    List<Role> list();
}
  1. 在service目录下创建类RoleServiceImpl
public class RoleServiceImpl implements RoleService{
    
    
    private RoleDao roleDao;
    public void setRoleDao(RoleDao roleDao) {
    
    
        this.roleDao = roleDao;
    }

    @Override
    public List<Role> list() {
    
    
        List<Role> roleList=roleDao.finAll();
        return roleList;
    }
}
  1. 在dao目录下创建接口RoleDao.class
public interface RoleDao {
    
    
    List<Role> finAll();
}

  1. 在dao目录下创建类RoleDaoImpl.class
public class RoleDaoImpl implements RoleDao{
    
    
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    
    
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public List<Role> finAll() {
    
    
        List<Role> roleList = jdbcTemplate.query("select * from sys_role", new BeanPropertyRowMapper<Role>(Role.class));
        return roleList;
    }
}
  1. 修改applicationContext,将service和dao层进行配置文件的注入
<!--配置RoleService-->
<bean id="roleService" class="service.RoleServiceImpl">
    <property name="roleDao" ref="roleDao"/>
</bean>

<!--配置RoleDao-->
<bean id="roleDao" class="dao.RoleDaoImpl">
    <property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
  1. 修改role-list.jsp页面,引入<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>,修改table
<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>

在这里插入图片描述

  1. 启动tomcat访问http://localhost:8080/demo04_war/role/list,看到数据

猜你喜欢

转载自blog.csdn.net/qq_37589805/article/details/120610680