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>
相关推荐
梓色系5 分钟前
Spring AI 实战:从零搭建 MCP 客户端与服务端,让大模型拥有“手脚“
java·人工智能·spring
秦时星星13 分钟前
Spring AI + FastMCP 跨语言集成踩坑实录
java·人工智能·spring
见牛羊16 分钟前
docker理解
java·docker·容器
codingPower19 分钟前
JAVA后端安全进阶:基于HMAC-SHA256+Nonce+Timestamp的API防重放攻击方案
java·开发语言·spring boot·安全
寂夜了无痕25 分钟前
IntelliJ IDEA 高效配置:新建文件自动生成作者与时间注释
java·ide·intellij-idea
leonidZhao33 分钟前
Java 25新特性:模块导入申明
java
weixin_489690021 小时前
【IDEA 2025.2.4】 Maven 仅能手动 Reload All Maven Projects 问题解决
java·maven·intellij-idea
雨辰AI1 小时前
MySQL 迁移至达梦 DM9 完整改造指南|99% SQL 零改动
java·开发语言·数据库·sql·mysql·政务
golang学习记1 小时前
Intellij IDEA 2026重磅更新!开发体验大升级
java·ide·intellij-idea
弹简特1 小时前
【Java项目-轻聊】05-AI赋能设计接口文档
java·开发语言