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>
相关推荐
从琳开始98934 分钟前
网络原理入门(二)------OSI七层模型?TCP/IP五层(四层)模型原来是这样的
java·网络·tcp/ip·java-ee·php
文丑颜不良啊1 小时前
LeetCode1861~1880题目解析
java·leetcode
Flynn Liang1 小时前
AgentScope Java 深度解析:ReActAgent、HarnessAgent 与多智能体编排——JVM 上的自治 Agent 底座
java·大模型·架构设计·ai agent·spring ai·langchain4j·agentscope
Flynn Liang1 小时前
上线后怎么排查 AI 调用?Spring AI Observability:token/延迟/成本埋点与全链路追踪
java·人工智能·spring·监控·可观测性·observability·springai
文丑颜不良啊1 小时前
LeetCode1921~1940题目解析
java·leetcode
Tt有了t2 小时前
设计模式速查
java·python·设计模式
Thomas.Sir2 小时前
第15课:SpringBoot连接池详解:HikariCP配置、调优、监控,4.x连接池新特性
java·spring boot
VimorphTech2 小时前
每批开 4 个线程,为什么限制不住 TTS 的总并发?
java·spring boot·spring
Pniubi2 小时前
Java为什么要分堆和栈
java·开发语言
杨凯凡2 小时前
Spring Boot 缓存一致性怎么保证?@Cacheable、@CacheEvict 与事务提交时机实战
java·spring boot·spring cache·后端开发·缓存一致性