SSM框架中将数据遍历传到前端

您所在的位置:网站首页 ssm框架获取前台数据 SSM框架中将数据遍历传到前端

SSM框架中将数据遍历传到前端

2023-07-25 01:14| 来源: 网络整理| 查看: 265

实现

关键代码

select * from tb_user

mapper部分,负责在sql查询

List findAll();

dao部分,引用

List userlist=userDao.findAll(); //将查询的数据放入到ModelAndView对象中 //"itemList":为别名,在前端页面上使用 //items:执行sql语句查询到的数据 modelAndView.addObject("userlist", userlist);

controller部分,将数据传到前端

Title ${user.id } ${user.username } ${user.password }

jsp部分,在前端遍历显示 其实应该在service使用dao,然后在controller使用servise,但是findall部分不需要判断,只需要把数值拿来,加上任务较为简单,所以我略去了

整体代码 package com.zhongruan.service.impl; import com.zhongruan.bean.User; import com.zhongruan.dao.UserDao; import com.zhongruan.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; @Service public class UserService implements IUserService { @Autowired private UserDao userDao; @Override public boolean login(String username, String password) { User user = userDao.findUserByUserName(username); if(user!=null && user.getPassword().equals(password)){ return true; } return false; } } package com.zhongruan.dao; import com.zhongruan.bean.User; import java.util.List; public interface UserDao { User findUserByUserName(String username); List findAll(); } package com.zhongruan.controller; import com.zhongruan.bean.User; import com.zhongruan.dao.UserDao; import com.zhongruan.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import java.util.List; @Controller @RequestMapping("/user") public class UserController { @Autowired private IUserService userService; @Autowired private UserDao userDao; @RequestMapping("/login.do") public ModelAndView login(User user){ boolean flag = userService.login(user.getUsername(), user.getPassword()); ModelAndView modelAndView=new ModelAndView(); if(flag){ List userlist=userDao.findAll(); //将查询的数据放入到ModelAndView对象中 //"itemList":为别名,在前端页面上使用 //items:执行sql语句查询到的数据 modelAndView.addObject("userlist", userlist); modelAndView.setViewName("../ok"); }else { modelAndView.setViewName("../failure"); } return modelAndView; } }

和上课的登录判断写在一起了,登录成功则能看用户名单,失败就跳转到failure视图

效果图

在这里插入图片描述



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3