温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

利用java如何实现在删除信息后刷新页面功能

发布时间:2020-11-17 14:25:49 来源:亿速云 阅读:423 作者:Leah 栏目:开发技术

这篇文章给大家介绍利用java如何实现在删除信息后刷新页面功能,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

我就废话不多说了,大家还是直接看代码吧~

//执行的是删除信息的操作
 String a=request.getParameter("name");
  a = URLEncoder.encode(a, "ISO-8859-1");
  String name = URLDecoder.decode(a, "UTF-8");
 String num=request.getParameter("num");
 System.out.println("name:"+name+"num:"+num);
 String sql="delete from person_info where name=? and num=?";
 String sz[]={name,num};
 JdbcUtils.update(sql, sz);
 //刷新操作
 String sqls="select * from person_info";
 ResultSet rs=JdbcUtils.select(sqls, null);
 ArrayList<Person_info> list=new ArrayList<Person_info>();
 try {
  while(rs.next()){
  Person_info pi=new Person_info(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6));
  list.add(pi);
  }
  request.setAttribute("list", list);
  request.getRequestDispatcher("Personnel_definition.jsp").forward(request, response);
 } catch (SQLException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 
 
 }

利用java如何实现在删除信息后刷新页面功能

补充知识:关于分页时怎么实现当本页面最后一条记录被删除时,自动向上一个页面跳转的实现(java实现)

##问题详解

在做批量删除时,发现若批量删除整页时,会自动跳到第一页首页,而不是返回删除当前页的上一页,不符合产品要求且使界面交互不好,给用户带来糟糕体验。

##思路详解

在controller层传参时要考虑到不仅要传入需要删除的id集合,同时传入pageSize,pageNum以及总条数集合的查询条件(如:本示例会传入groupId(分组id)),在删除成功后初始化当前页,先根据查询条件查询出总条数数量,在pageSize不等于null或为0的情况下。算出余数[(pageSize*pageNum-count)%pageSize ].若余数为0,则当前页等于pageNum-1;若余数不为0,则当前页=pageNum.将结果当前页传给前台即可。

##后台代码实现

#controller层#

@Api(description = "分组下的学生",value = "分组下的学生")
@RestController
@RequestMapping("studentGroup")
public class StudentGroupController {
 @Autowired
 private RestStudentGroupService restStudentGroupService;


 @RequestMapping(value = "deleteGroupStudent",method = RequestMethod.POST)
 @ApiOperation(value = "删除分组中的学生",notes = "删除分组中的学生")
 public ResponseObj deleteGroupStudent(@RequestParam(value = "groupId",required = true)Long groupId,
     @RequestParam(value = "ids",required = true)String ids,
     @RequestParam(value = "pageSize",required = false)Integer pagesize,
     @RequestParam(value = "pageNum",required = false)Integer pageNum){

 return restStudentGroupService.deleteGroupStudent(groupId,ids,pagesize,pageNum);
 }
 }

#service层#

@FeignClient(value = ServiceName.VALUE)
public interface RestStudentGroupService {

 @RequestMapping(value = "/school/cloud/student/deleteGroupStudent",method = RequestMethod.POST)
 public ResponseObj deleteGroupStudent(@RequestParam(value = "groupId")Long groupId,
     @RequestParam(value = "ids")String ids,
     @RequestParam(value = "pageSize")Integer pagesize,
     @RequestParam(value = "pageNum")Integer pageNum);
     }

#serviceImpl层#

@Service
public class RestStudentGroupServiceImpl implements RestStudentGroupService {

 @Autowired
 private DubboStudentGroupService dubboStudentGroupService ;
 @Override
 public ResponseObj deleteGroupStudent(Long groupId,String ids,Integer pageSize,Integer pageNum) {

 List<Long> idList = TextUtils.split(ids);
 if(groupId == null || idList== null || idList.size() == 0){
  ResponseObj responseObj = ResponseObj.ERROR("参数错误");
  responseObj.setSuccess(true);
  return responseObj;
 }
 ServiceResult<Long> serviceResult = dubboStudentGroupService .deleteCorpGroup(idList, groupId);
 if(!serviceResult.getSuccess()){
  throw new RuntimeException("分组下学生查询失败");
 }
 //应前端要求加此dto,封装传给前台的当前页属性
 CurrenPageDto currenPageDto=new CurrenPageDto();
 //初始化当前页
 Integer currentPage = 1;
 //查出该分组id下的学生数量
 ServiceResult<Long> itemCountLongs = dubboStudentGroupService.getTotalCount(groupId);
 Long itemCountLong= itemCountLongs.getResult();
 Integer itemCount = itemCountLong!=null &#63; itemCountLong.intValue() : 0;
 //"查询到学生数量:{},pageSize:{}", itemCount,pageSize;
 if(pageSize != null && pageSize != 0){
  //算出余数
  Integer temp = (pageNum*pageSize-itemCount)%pageSize;
  if(temp == 0){
  //余数为0的话就pageNum-1
  currentPage = (pageNum - 1) == 0 &#63; 1 : (pageNum -1) ;
  }else {
  //余数不为0则等于pageNum
  currentPage = pageNum;
  }
  currenPageDto.setPresentPage(currentPage);
 }
 ResponseObj responseObj = ResponseObj.SUCCESS();
 responseObj.setData(currenPageDto);
 return responseObj;
 }
}

#dubbo接口的service层#

①://删除分组下的学生
ServiceResult<Long> deleteCorpGroup(List<Long> idList,Long groupId);
②://根据条件查询对应的条目总数
ServiceResult<Long> getTotalCount(Long groupId);

#dubbo接口的serviceImpl层#

①://删除分组下的学生
 @Override
 public ServiceResult<Long> deleteCorpGroup(List<Long> idList, Long groupId) {
 ServiceResult<Long> result = new ServiceResult<>();

 try {
  studentGroupDao.deleteCorpGroup(idList, groupId);
 } catch (Exception e) {
  log.error("调用{}方法 异常", "[RestStudentGroupServiceImpl .deleteCorpGroup]");
  log.error("方法使用参数:[idList:{},groupId:{}]", idList, groupId);
  log.error("异常信息:{}", e);
  result.setErrMessage("调用deleteCorpGroup方法异常,异常信息:" + e.getMessage());
 }

 return result;
 }
②://根据条件查询对应的条目总数
 @Override
 public ServiceResult<Long> getTotalCount(Long groupId) {
 ServiceResult<Long> result = new ServiceResult<>();

 try {
  long count = studentGroupDao.getFindCorpGroupDirectoryCount(groupId);
  result.setResult(count);
 } catch (Exception e) {
  log.error("调用{}方法 异常", "[RestStudentGroupServiceImpl .getTotalCount]");
  log.error("方法使用参数:[groupId:{}]", groupId);
  log.error("异常信息:{}", e);
  result.setErrMessage("调用getTotalCount方法异常,异常信息:" + e.getMessage());
 }
 return result;
 }

#dubbo接口的dao层#

①://删除分组下的学生
 Long deleteCorpGroup(@Param(value = "idList") List<Long> idList,@Param(value = "groupId") Long groupId);
②://根据条件查询对应的条目总数
Long getFindCorpGroupDirectoryCount(@Param(value = "groupId") Long groupId);

#dubbo接口的sql#

①://删除分组下的学生
 <delete id="deleteCorpGroup">
 delete from student_group where group_id = #{groupId} and id in
 <foreach collection="idList" index="index" separator="," item="id"
   open="(" close=")">
  #{id}
 </foreach>
 </delete>
②://根据条件查询对应的条目总数
 <select id="getFindCorpGroupDirectoryCount" resultType="long">
 SELECT COUNT(1)
 FROM student_group 
 where group_id = #{groupId}
 </select>

#Entity类(学生分组类)#(get,set函数省略)

public class StudentGroup implements java.io.Serializable {
 
 /**
 * 
 */
 private static final long serialVersionUID = 1L;
 /**
 * @描述: 
 * @字段:id BIGINT(19) 
 */ 
 private Long StudentGroupId;

 /**
 * @描述: 
 * @字段:group_id BIGINT(19) 
 */ 
 private Long groupId;

 /**
 * @描述: 
 * @字段:id BIGINT(19) 
 * 此id为学生表id
 */ 
 private Long id;

 /**
 * @描述:创建时间 
 * @字段:create_time DATETIME(19) 
 */ 
 private java.util.Date createTime;

 * @描述:创建人用户名 
 * @字段:create_user_name VARCHAR(30) 
 */ 
 private String createUserName;

 /**
 * @描述:创建人用户ID 
 * @字段:create_user_id BIGINT(19) 
 */ 
 private Long createUserId;

 /**
 * @描述:更新时间 
 * @字段:update_time DATETIME(19) 
 */ 
 private java.util.Date updateTime;

 * @描述:更新人用户名 
 * @字段:update_user_name VARCHAR(30) 
 */ 
 private String updateUserName;

 /**
 * @描述:更新人用户ID 
 * @字段:update_user_id BIGINT(19) 
 */ 
 private Long updateUserId;
 }

#Entity类(学生类)#(get,set函数省略)

public class Student implements java.io.Serializable {
 /**
 * 
 */
 private static final long serialVersionUID = 1L;

 private Long id;
 private String name ;
 private Integer age;
 }

关于利用java如何实现在删除信息后刷新页面功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI