javaEE10(javabean课后题2,4)

java 复制代码
<%@ page  pageEncoding="UTF-8" import="java.util.*,entity.*,dao.*"%>

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

<html>

<head>

<style>

  body{

      text-align:center;

  }

  th,td{

  border:2px solid gray;

  text-align:center;

  padding:3px 10px;

  }

  table{

  border-collapse:collapse;

  margin:10px auto;

  }



  #form1{

  padding:20px;

  display:none;

  position:fixed;

  top:50%;

  left:50%;

  width:300px;

  height:200px;

  z-index:1000;

  margin-left:-200px;

  margin-top:-200px;

   background: #88f;

  }

 </style>

</head>

<body>

<%

StudentDao studentDao=new StudentDao();

request.setCharacterEncoding("UTF-8");

String op=request.getParameter("op");

if(op!=null){

if("add".equals(op)){

    String sno=request.getParameter("sno");

    String sname=request.getParameter("sname");

    String sex=request.getParameter("sex");

    Student student=new Student(sno,sname,sex);

    try{

    studentDao.add(student);

    }catch(Exception e){

        e.printStackTrace();

    }

}

else if("update".equals(op)){

    String sno=request.getParameter("sno");

    String sname=request.getParameter("sname");

    String sex=request.getParameter("sex");

    Student student=new Student(sno,sname,sex);

    try{

        studentDao.update(student);

        }catch(Exception e){

            e.printStackTrace();

        }

}

else if("delete".equals(op)){

    String sno=request.getParameter("sno");

    try{

        studentDao.delete(sno);

        }catch(Exception e){

            e.printStackTrace();

        }

}

}

List<Student> studentList=studentDao.query();

pageContext.setAttribute("studentList",studentList);

 %>

  <h3><a href="javascript:add()">添加记录</a></h3>

 <h2>学生信息</h2>

   <table>

   <tr><th>学号</th><th>姓名</th><th>性别</th><th>修改</th><th>删除</th></tr>

  <c:forEach items="${studentList}" var="student" varStatus="st">

  <tr>

   <td>${student.sno}</td>

   <td>${student.sname}</td>

   <td>${student.sex}</td>

   <td><a href="javascript:update(${student.id},'${student.sno}','${student.sname}','${student.sex}')">修改</a></td>

   <td><a href="yemian.jsp?op=delete&id=${student.id}" onclick="return confirm('确实要删除吗?')">删除</a></td>   

  </tr>

 </c:forEach>

 </table>

 <form method="post" id="form1">

学号<input type="text" name="sno" id="sno"/><br><br>

 姓名<input type="text" name="sname" id="sname"/><br><br>

性别<input type="radio" name="sex" value="男" checked/>男<input type="radio" name="sex" value="女" />女<br><br>

 <input type="hidden" name="op" value="" id="op"/>

 <input type="hidden" name="id" id="id"/>

 <input type="submit" value="" id="bt"/>

 <input type="reset" value="重置"/>

  <input type="button" value="关闭" onclick="document.getElementById('form1').style.display='none';"/>

 </form>

 <br>

 <script>

 function add(){

    document.getElementById("op").value="add";

    document.getElementById("bt").value="添加";

    document.getElementById('form1').style.display='block';

 }

 function update(id,sno,sname,sex){

     document.getElementById("op").value="update";

     document.getElementById("bt").value="修改";

     document.getElementById('form1').style.display='block';

     

 }

 </script>

</body>

</html>

java 复制代码
<%@ page   import="dao.CandidateDao,entity.*,java.util.*"  pageEncoding="UTF-8"%>

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

<!DOCTYPE html>

<html>

<script>

function f(){

    var cks=document.getElementsByName("ck");

    len=cks.length;

    sum=0;

    for(i=0;i<len;i++){

        if(cks[i].checked)

            sum++;

    }

    if(sum==5){

        document.getElementById("form1").submit();

    }

    else{

        alert("请选择5个候选人");

    }

}

</script>

<body>

<%

CandidateDao candidateDao=new CandidateDao();

String[] ids=request.getParameterValues("ck");

int[] ids1=null;

if(ids!=null){

    ids1=new int[ids.length];

    for(int i=0;i<ids.length;i++){

        ids1[i]=Integer.parseInt(ids[i]);

    }

    candidateDao.vote(ids1);

}



%>

<%

List<Candidate> cList=candidateDao.query();

pageContext.setAttribute("cList",cList);

%>

<form method="post" id="form1">

<c:forEach items="${cList}" var="c">

<input type="checkbox" name="ck" value="${c.id}">

<img src="image/${c.photo}"><br>

${c.name}<br>

${c.intro}<br>

${c.num}<hr>

</c:forEach>

<input type="button" value="投票" onclick="f()"/>

</form>

</body>

</html>
相关推荐
小毛驴85015 分钟前
Linux 后台启动java jar 程序 nohup java -jar
java·linux·jar
DKPT1 小时前
Java桥接模式实现方式与测试方法
java·笔记·学习·设计模式·桥接模式
好奇的菜鸟2 小时前
如何在IntelliJ IDEA中设置数据库连接全局共享
java·数据库·intellij-idea
DuelCode3 小时前
Windows VMWare Centos Docker部署Springboot 应用实现文件上传返回文件http链接
java·spring boot·mysql·nginx·docker·centos·mybatis
优创学社23 小时前
基于springboot的社区生鲜团购系统
java·spring boot·后端
幽络源小助理3 小时前
SpringBoot基于Mysql的商业辅助决策系统设计与实现
java·vue.js·spring boot·后端·mysql·spring
猴哥源码3 小时前
基于Java+springboot 的车险理赔信息管理系统
java·spring boot
YuTaoShao4 小时前
【LeetCode 热题 100】48. 旋转图像——转置+水平翻转
java·算法·leetcode·职场和发展
Dcs5 小时前
超强推理不止“大”——手把手教你部署 Mistral Small 3.2 24B 大模型
java
东阳马生架构5 小时前
订单初版—1.分布式订单系统的简要设计文档
java