52,797
社区成员
发帖
与我相关
我的任务
分享
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
</head>
<body>
<a href="stuInf.jsp">学生信息</a>
<a href="course.jsp">课程信息</a>
<a href="score.jsp">成绩信息</a>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<style type="text/css">
td
{
width:85px;
border-top:0px;
border-left:0px;
}
</style>
<script type="text/javascript" src="<%=path%>/jquery/jquery-1.8.0.js"></script>
<script type="text/javascript">
<!--
var m;
var pageSum;
var thisPage=1;
$(document).ready(function() {
findAll();
});
function findAll() {
$.ajax({
type:"post",
url:"main/selectStu",
dataType:"json",
success:callBack
});
}
function callBack(data){ // 回调
m=data.listStu;
fenYe(m);
}
function fenYe(m) { // 分页
pageSum=Math.ceil(m.length/5);
var divHtml="";
divHtml+="<form action='main/deleteStu' method='post' id='delete'>";
divHtml+="<table border='2px' align='center'><tr><td>选择</td><td>序号</td><td>学号</td><td>姓名</td><td colspan='2' style='border-right:0px;'>操作</td></tr>";
for(var i=(thisPage-1)*5;i<(thisPage==pageSum?m.length:thisPage*5);i++) {
var temp=m[i];
divHtml+="<tr>";
divHtml+="<td><input type='checkbox'value="+temp.s_id+" name='mapStu["+i+"]'/></td>";
divHtml+="<td>"+(i+1)+"</td>";
divHtml+="<td>"+temp.s_id+"</td>";
divHtml+="<td>"+temp.s_name+"</td>";
divHtml+="<td style='border-right:0px'><a href='main/oneStu?mapStu['s_id']="+temp.s_id+"'>修改</a></td>";
divHtml+="</tr>";
}
divHtml+="<tr><td colspan='5' style='border-right:0px; border-bottom:0px'>";
divHtml+=" <input type='submit' value='删除'>";
divHtml+=" <input type='button' value='首页' name='fristPage' onclick='changePage(this)'>";
divHtml+=" <input type='button' value='上页' name='shangPage' onclick='changePage(this)'>";
divHtml+=" <input type='button' value='下一页' name='nextPage' onclick='changePage(this)'>";
divHtml+=" <input type='button' value='末页' name='lastPage' onclick='changePage(this)'>";
divHtml+=" "+thisPage+"/"+pageSum;
divHtml+=" 共 "+m.length+" 条数据";
divHtml+="</td></tr>";
divHtml+="</table>";
divHtml+="</form>";
$("#d").html(divHtml);
}
function changePage(o) { // 改变页码
if(o.name=="fristPage") {
thisPage=1;
fenYe(m);
}
if(o.name=="shangPage") {
thisPage=(thisPage==1?1:thisPage-1);
fenYe(m);
}
if(o.name=="nextPage") {
thisPage=(thisPage==pageSum?pageSum:thisPage+1);
fenYe(m);
}
if(o.name=="lastPage") {
thisPage=pageSum;
fenYe(m);
}
}
</script>
</head>
<body>
<div id="d"></div>
<s:form action="main/addStu" method="post">
<s:textfield name="mapStu['s_id']" label="学号"></s:textfield>
<s:textfield name="mapStu['s_name']" label="姓名"></s:textfield>
<s:submit value="提交"></s:submit>
</s:form>
</body>
</html>
package action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.dao.StuDao;
import com.opensymphony.xwork2.ActionSupport;
public class StudentAction extends ActionSupport{
private List<Object> listStu;
private Map<String,Object> mapStu;
public Map<String, Object> getMapStu() {
return mapStu;
}
public void setMapStu(Map<String, Object> mapStu) {
this.mapStu = mapStu;
}
public List<Object> getListStu() {
return listStu;
}
public void setListStu(List<Object> listStu) {
this.listStu = listStu;
}
public String select() { // 查找学生信息
if(listStu== null) {
listStu= new ArrayList<Object>();// 注意绝对不能为: List<Object> listStu= new ArrayList<Object>();
}
new StuDao().select(listStu);
System.out.println(listStu.size());
return SUCCESS;
}
public String add() { // 增加学生信息
System.out.println("add");
if(mapStu== null) {
mapStu= new HashMap<String, Object>();
}
new StuDao().add(mapStu);
return SUCCESS;
}
public String getOne() { // 获取一个学生信息
System.out.println("getOne");
new StuDao().getOne(mapStu);
System.out.println(mapStu.get("s_name"));
return SUCCESS;
}
public String update() { // 修改
System.out.println("update");
if(mapStu==null){
mapStu= new HashMap<String, Object>();
}
new StuDao().update(mapStu);
select();
return SUCCESS;
}
public String delete() { // 删除
System.out.println("delete");
if(mapStu== null) {
mapStu= new HashMap<String, Object>();
}
new StuDao().delete(mapStu);
System.out.println(mapStu+"fffff");
return SUCCESS;
}
}
package com.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.jdbc.Jdbc;
public class StuDao {
public void select(List<Object> listStu) { // 查找
System.out.println("sqlrun....");
ResultSet rs= new Jdbc().getRs("select * from studentInf"); // ? 匿名要关闭吗?
try {
while(rs.next()) {
Map<String,Object> map= new HashMap<String,Object>();
map.put("s_id",rs.getString("s_id"));
map.put("s_name",rs.getString("s_name"));
listStu.add(map);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void add(Map<String,Object> mapStu) { // 增加
System.out.println("dfd");
String a[]= (String[]) mapStu.get("s_id"); // 注意 map。get()得到的是字符数组
String b[]= (String[]) mapStu.get("s_name");
String s_id= a[0];
String s_name= b[0];
new Jdbc().execSql("insert into studentInf values('"+s_id+"','"+s_name+"')");
System.out.println("insert into studentInf values('"+s_id+"','"+s_name+"')");
}
public void update(Map<String,Object> mapStu) { // 修改
String a[]= (String[]) mapStu.get("s_id");
String b[]= (String[]) mapStu.get("s_name");
String s_id= a[0];
String s_name= b[0];
new Jdbc().execSql("update studentInf set s_name='"+s_name+"' where s_id='"+s_id+"'");
}
public void delete(Map<String,Object> mapStu) { // 删除
Iterator i= mapStu.entrySet().iterator();
while(i.hasNext()) { // 迭代 map
Map.Entry e= (Entry) i.next();
String a[]= (String[])e.getValue();
new Jdbc().execSql("delete from studentInf where s_id='"+a[0]+"'");
System.out.println("delete from studentInf where s_id='"+a[0]+"'");
}
}
public void getOne(Map<String,Object> mapStu) { // 获取一个
String a[]= (String[])mapStu.get("s_id");
String s_id= a[0];
ResultSet rs= new Jdbc().getRs("select * from studentInf where s_id='"+s_id+"'");
try {
if(rs.next()) {
mapStu.put("s_id",rs.getString("s_id"));
mapStu.put("s_name",rs.getString("s_name"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<constant name="struts.devMode" value="false"/>
<constant name="struts.i18n.encoding" value="UTF-8"/>
<constant name="struts.serve.static.browserCache" value="false"/>
<package name="test" extends="struts-default,json-default" namespace="/main">
<action name="selectStu" class="action.StudentAction" method="select">
<result type="json"></result>
</action>
<action name="addStu" class="action.StudentAction" method="add">
<result name="success">/stuInf.jsp</result>
</action>
<action name="deleteStu" class="action.StudentAction" method="delete">
<result name="success">/stuInf.jsp</result>
</action>
<action name="oneStu" class="action.StudentAction" method="getOne">
<result name="success">/updateStu.jsp</result>
</action>
<action name="updateStu" class="action.StudentAction" method="update">
<result name="success">/stuInf.jsp</result>
</action>
</package>
</struts>
public String getOne() {
if(mapStu== null){ return null;}
new StuDao().getOne(mapStu);
System.out.println(mapStu.get("s_name"));
return SUCCESS;
}
<action name="selectStu" class="action.StudentAction" method="select">
<result type="json"><param name="root">listStu</param></result>
</action>
才可以