种子软件项目管理系统实战

上篇博客的基础上进行项目的实战。

方法实现

src目录如下:

1、实体类

“com.xmx.oa.model.entity” --> “Person.java”,内容详见jsp连接mysql博客“jsp搭建”中第7点。

package com.xmx.oa.model.entity;

public class Person {
int id;
String name;
String sex;
int age;
String phone;
int currentpage;
int size;

public Person(String name, String sex, String phone) {
	super();
	this.name = name;
	this.sex = sex;
	this.phone = phone;
}
public int getId() {
	return id;
}
public void setId(int id) {
	this.id = id;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public String getSex() {
	return sex;
}
public void setSex(String sex) {
	this.sex = sex;
}
public int getAge() {
	return age;
}
public void setAge(int age) {
	this.age = age;
}
public String getPhone() {
	return phone;
}
public void setPhone(String phone) {
	this.phone = phone;
}
public Person(int id, String name, String sex, int age, String phone) {
	super();
	this.id = id;
	this.name = name;
	this.sex = sex;
	this.age = age;
	this.phone = phone;
}

public Person(int currentpage, int size) {
	super();
	this.currentpage = currentpage;
	this.size = size;
}
public Person() {
	super();
	// TODO Auto-generated constructor stub
}
@Override
public String toString() {
	return "Person [id=" + id + ", name=" + name + ", sex=" + sex + ", age="
			+ age + ", phone=" + phone + "]";
}

}

2、工具类

过滤器“com.xmx.oa.filter” --> “PersonFilter.java”内容详见小型客户管理系统的开发

package com.xmx.oa.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class PersonFilter implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		System.out.println("我是过滤器,我骄傲!");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		// 把请求传回过滤链
		chain.doFilter(request,response);

	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}

“com.xmx.oa.model.utils” --> “MybatisUtils.java”,内容详见Mybatis使用博客第4点。

package com.xmx.oa.model.utils;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MybatisUtils {
	//定义工厂
	public static SqlSessionFactory sqlSessionfactory;
	
	static{
		try {
			//与配置文件建立连接
			Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
			//生产工厂
			sqlSessionfactory = new SqlSessionFactoryBuilder().build(reader);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static SqlSession getSqlSession(){
		return sqlSessionfactory.openSession();
	}
	
	public void closeSqlSession(SqlSession sqlSession){
		if(sqlSession!=null){
			sqlSession.close();
		}
	}
}

“mybatis-config.xml”,内容详见Mybatis使用博客第2、3点。

<?xml version="1.0"?>
<!DOCTYPE configuration PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 默认与default的数据库建立连接 -->
<environments default="cs">
<!-- id为要连接的数据库,可同时配置多个 -->
<environment id="cs">
<!-- 事务管理,连接方式:JDBC/MANAGED -->
<transactionManager type="JDBC"></transactionManager>
<!-- 数据源:POOLED/UNPOOLED -->
<dataSource type="POOLED">
<!-- 在缓冲池中配置与数据库连接的参数 -->
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/oa" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<!-- 配置映射信息 -->
<mappers>
<mapper resource="com/xmx/oa/model/service/IPersonService-mapper.xml"></mapper>
</mappers>
</configuration>

3、控制层

“com.xmx.oa.controller” --> “PersonServlet.java”

package com.xmx.oa.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xmx.oa.model.entity.Person;
import com.xmx.oa.model.service.IPersonService;
import com.xmx.oa.service.impl.PersonServiceImpl;

public class PersonServlet extends HttpServlet {
	IPersonService service = new PersonServiceImpl();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//		System.out.println("1");
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//		System.out.println("进来了?");
		String method = request.getParameter("method");
		System.out.println(method);
		if(method.equals("add")){
			this.doAdd(request, response);
		}else if(method.equals("del")){
			this.doDel(request, response);
		}else if(method.equals("findid")){
			this.doFindId(request, response);
		}else if(method.equals("findname")){
			this.doFindName(request, response);
		}else if(method.equals("update")){
			this.doUpdate(request, response);
		}else if(method.equals("super")){
			this.doSuperFind(request, response);
		}else if(method.equals("delmul")){
			this.doDelMul(request, response);
		}else{
			this.doPageFind(request, response);
		}
	}
	
	private void doPageFind(HttpServletRequest request,HttpServletResponse response)
			 throws ServletException, IOException {
		int currentpage = 1;					//默认为第一页
		int size = 5;							//每页显示的记录数
		int count = service.findCountPage();	//页数总数
		int page = 0;
//		System.out.println("数据总数为"+count);
		//获取传过来的当前页面
		String cpage = request.getParameter("currentpage");
//		System.out.println("cpage="+cpage);
		if(cpage!=null && !cpage.equals("")){
			currentpage = Integer.parseInt(cpage);
		}
		//获取要转到的页面,放到currentpage里
		String turnTo = request.getParameter("turnTo");
		if(turnTo!=null && !turnTo.equals("")){
			currentpage = Integer.parseInt(turnTo);
		}
		//1.调用业务逻辑层的分页方法
		List<Person> list = service.findByPage((currentpage-1)*size, size);
		//2.保存到request中
		request.setAttribute("list", list);
		request.setAttribute("currentpage", currentpage);	//保存当前页码
		//保存总页数,要判断一下
		if(count%size==0){
			page = count/size;
		}else{
			page = count/size +1;
		}
		request.setAttribute("page", page);		
		//3.跳转到显示页面
		request.getRequestDispatcher("files/listPerson.jsp").forward(request, response);
		
	}

	public void doAdd(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		//1.提取参数
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		String sex = request.getParameter("sex");
		int age = Integer.parseInt(request.getParameter("age"));
		String phone = request.getParameter("phone");
//		System.out.println(id);
		//2.封装成对象
		Person p = new Person(id, name, sex, age, phone);
		//3.调用业务逻辑层(放到外面)
//		IPersonService service = new PersonServiceImpl();
		int i = service.addPerson(p);
		System.out.println("i="+i);
		//4.跳转到显示页面
		if(i>0){
			this.doFind(request, response);
		}else{
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}
	}
	
	public void doDel(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//1.取得参数
		int id = Integer.parseInt(request.getParameter("cid"));
		//2.调用业务逻辑层的删除方法
		int i = service.deletePerson(id);
		//3.跳转到显示页面
		if(i>0){
			this.doPageFind(request, response);
		}else{
			System.out.println("删除失败!");
		}
	}
	
	public void doDelMul(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//1.取得参数
		String ids[] = request.getParameterValues("id");
		
		//把字符串数组ids里的数都放到整形数组id中
		int t = 0;
		int id[] = new int[ids.length];
		for(String i:ids){
			id[t] = Integer.parseInt(i);
			t++;
		}
		//2.调用业务逻辑层的批量删除方法
		int i = service.deleteMulPerson(id);
		//3.跳转到显示页面
		if(i>0){
			this.doPageFind(request, response);
		}else{
			System.out.println("删除失败!");
		}
	}
	
	public void doFindId(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//1.取得参数
		int id = Integer.parseInt(request.getParameter("cid"));
		//2.调用业务逻辑层中的按id查询方法
		Person p = service.findById(id);
		//3.保存对象
		request.setAttribute("p1", p);
		//4.跳转到update.jsp
		request.getRequestDispatcher("files/editPerson.jsp").forward(request, response);
	}
	
	public void doFindName(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//1.取得参数
		String name = request.getParameter("username");
		//2.调用业务逻辑层中的按id查询方法
		List<Person> list = service.findVaguePerson(name);
		for(Person i:list){
			System.out.println(i.toString());
		}
		//3.保存对象
		request.setAttribute("list", list);
		//4.跳转到update.jsp
		request.getRequestDispatcher("files/listPerson.jsp").forward(request, response);
	}
	
	public void doUpdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//1.提取参数
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		String sex = request.getParameter("sex");
		int age = Integer.parseInt(request.getParameter("age"));
		String phone = request.getParameter("phone");
		//2.封装成对象
		Person p = new Person(id, name, sex, age, phone);
		//3.调用业务逻辑层(放到外面)
//		IPersonService service = new PersonServiceImpl();
		int i = service.updatePerson(p);
//		System.out.println(i);
		//4.跳转到显示页面
		if(i>0){
			System.out.println("更新成功!");
			this.doPageFind(request, response);
		}else{
			System.out.println("更新失败!");
		}
	}
	
	//查询所有客户信息
	public void doFind(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//1.调用业务逻辑层的查询方法
		List<Person> list = service.findAllPerson();
		//2.保存到request中
		request.setAttribute("list", list);
		//3.跳转到显示页面
		request.getRequestDispatcher("files/listPerson.jsp").forward(request, response);
	}
	
	public void doSuperFind(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//1.提取参数
		String name = request.getParameter("name");
		String sex = request.getParameter("sex");
		String phone = request.getParameter("phone");
		//2.封装成对象
		Person p = new Person(name, sex, phone);
		//3.调用业务逻辑层的高级搜索方法
		List<Person> list = service.superQuery(p);
		//4.保存到request中
		request.setAttribute("list", list);
		//5.跳转到显示页面
		request.getRequestDispatcher("files/listPerson.jsp").forward(request, response);
	}

	public void init() throws ServletException {
		// Put your code here
	}

}

4、服务层

“com.xmx.oa.model.service” --> “IPersonService.java”,内容理解详见Mybatis使用博客。

package com.xmx.oa.model.service;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.xmx.oa.model.entity.Person;

public interface IPersonService {
	
	public int addPerson(Person p);
	public int deletePerson(int id);
	public int deleteMulPerson(@Param("id") int id[]);
	public int updatePerson(Person p);
	public List<Person> findAllPerson();
	public List<Person> superQuery(Person p);
	public Person findById(int id);
	public List<Person> findVaguePerson(String name);
	public int findCountPage();
	public List<Person> findByPage(int currentpage,int size);

}

“com.xmx.oa.model.service” --> “IPersonService-mapper.xml”,内容理解详见Mybatis使用博客。

<?xml version="1.0"?>
<!DOCTYPE mapper PUBLIC
 "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 
 <mapper namespace="com.xmx.oa.model.service.IPersonService">
 <insert id="addPerson" parameterType="com.xmx.oa.model.entity.Person">
 insert  into users values (#{id},#{name},#{sex},#{age},#{phone})
 </insert>
 <delete id="deletePerson" parameterType="int">
 delete from users where id=#{id}
 </delete>
 <delete id="deleteMulPerson">
 delete from users where id in
 <foreach collection="id" item="v" open="(" close=")" separator=",">
 #{v}
 </foreach>
 </delete>
 <update id="updatePerson" parameterType="com.xmx.oa.model.entity.Person">
 update users set name=#{name}, sex=#{sex}, age=#{age}, phone=#{phone} where id=#{id}
 </update>
 <select id="findAllPerson" resultType="com.xmx.oa.model.entity.Person">
 select * from users
 </select>
 <select id="findById"
 resultType="com.xmx.oa.model.entity.Person" parameterType="int">
 select * from users where id=#{id}
 </select>
 <select id="findVaguePerson" resultType="com.xmx.oa.model.entity.Person" 
 parameterType="String">
 select * from users where name like concat('%',#{name},'%')
 </select>
 <select id="findByPage" resultType="com.xmx.oa.model.entity.Person">
 select * from users limit #{0}, #{1}
 </select>
 <select id="findCountPage" resultType="int">
 select count(*) from users 
 </select>
 <select id="superQuery" resultType="com.xmx.oa.model.entity.Person"
 parameterType="com.xmx.oa.model.entity.Person">
 select * from users
 <where> 
    <if test="name!=null and name!=''">
         name like concat('%',#{name},'%')
    </if> 
    <if test="sex!=null and sex!=''">
        and sex like concat('%',#{sex},'%')
    </if>
    <if test="phone!=null and phone!=''">
        and phone like concat('%',#{name},'%')
    </if>
  </where>
 </select>
 </mapper>

其中,where 元素只会在至少有一个子元素的条件返回 SQL子句的情况下才去插入“WHERE”子句。

而且,若语句的开头为“AND”或“OR”,where 元素也会将它们去除。

5、接口层

“com.xmx.oa.service.impl” --> “PersonServiceImpl.java”

package com.xmx.oa.service.impl;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;

import com.xmx.oa.model.entity.Person;
import com.xmx.oa.model.service.IPersonService;
import com.xmx.oa.model.utils.MybatisUtils;

public class PersonServiceImpl implements IPersonService {
	//1.获取sqlSession对象
	SqlSession sqlSession = MybatisUtils.getSqlSession();
	//2.获取需要操作的接口实例
	//通过解析接口对应的映射文件,根据映射文件中的配置来获取接口实例
	IPersonService service = sqlSession.getMapper(IPersonService.class);
	@Override
	public int addPerson(Person p) {
		int i = service.addPerson(p);
		sqlSession.commit();
//		sqlSession.close();
		return i;
	}

	@Override
	public int deletePerson(int id) {
		int i = service.deletePerson(id);
		sqlSession.commit();
//		sqlSession.close();
		return i;
	}

	@Override
	public int deleteMulPerson(@Param("id")int[] id) {
		int i = service.deleteMulPerson(id);
		sqlSession.commit();
//		sqlSession.close();
		return i;
	}

	@Override
	public int updatePerson(Person p) {
		int i = service.updatePerson(p);
		sqlSession.commit();
//		sqlSession.close();
		return i;
	}

	@Override
	public List<Person> findAllPerson() {
		// TODO Auto-generated method stub
		return service.findAllPerson();
	}

	@Override
	public Person findById(int id) {
		// TODO Auto-generated method stub
		return service.findById(id);
	}

	@Override
	public List<Person> findVaguePerson(String name) {
		// TODO Auto-generated method stub
		return service.findVaguePerson(name);
	}

	@Override
	public List<Person> findByPage(int currentpage, int size) {
		// TODO Auto-generated method stub
		return service.findByPage(currentpage, size);
	}

	@Override
	public int findCountPage() {
		// TODO Auto-generated method stub
		return service.findCountPage();
	}

	@Override
	public List<Person> superQuery(Person p) {
		// TODO Auto-generated method stub
		return service.superQuery(p);
	}

}

其作用为封装服务层的方法,类比上篇博客,在测试方法中可以体会到其简单的特性。

6、测试层

“com.xmx.oa.test” --> “TestOa.java”

package com.xmx.oa.test;

import java.util.List;

import org.junit.Test;

import com.xmx.oa.model.entity.Person;
import com.xmx.oa.model.service.IPersonService;
import com.xmx.oa.service.impl.PersonServiceImpl;

public class TestOa {
	IPersonService service = new PersonServiceImpl();
	
	@Test
	//添加
	public void test1(){
		Person p = new Person(9,"梁山伯","男",20,"1231413");
		int i = service.addPerson(p);
		System.out.println(i);
	}
	
	@Test
	//删除
	public void test2(){
		int i = service.deletePerson(2);
		System.out.println(i);
	}
	
	@Test
	//批量删除
	public void testDel(){
		int id[] = {2,3};
		int i = service.deleteMulPerson(id);
		System.out.println(i);
	}
	
	@Test
	//修改
	public void test3(){
		Person p = new Person(7,"哈哈","男",20,"1231413");
		int i = service.updatePerson(p);
		System.out.println(i);
		
	}
	
	@Test
	//查询所有
	public void test4(){
		List<Person>list = service.findAllPerson();
		for(Person p:list){
			System.out.println(p.toString());
		}
		
	}
	
	@Test
	//按照id查询
	public void test5(){
		Person p = new Person();
		p = service.findById(4);
		System.out.println(p);
	}
	
	@Test
	//按名字模糊查询
	public void test6(){
		List<Person>list = service.findVaguePerson("李");
		for(Person p:list){
			System.out.println(p.toString());
		}
	}
	
	@Test
	//高级搜索
	public void testSuperFind(){
		Person p = new Person("山","男","");
		List<Person>list = service.superQuery(p);
		for(Person p1:list){
			System.out.println(p1.toString());
		}
	}
	
	@Test
	//分页查询
	public void test7(){
		List<Person>list = service.findByPage(2, 3);
		for(Person p:list){
			System.out.println(p.toString());
		}
		
	}
	
	@Test
	//计算总条数
	public void test8(){
		int i = service.findCountPage();
		System.out.println(i);
	}
}

前端页面交互

注:以下仅显示设计交互的部分代码。

其中,index.jsp在WebRoot目录下,其余页面均在与之同级的files目录下。

1、index.jsp

<frameset rows="59,*" cols="*" frameborder="no" border="0" framespacing="0">
  <frame src="files/top.html" name="topFrame" scrolling="No" noresize="noresize" 
  id="topFrame" title="topFrame" />
  <frameset cols="213,*" frameborder="no" border="0" framespacing="0">
    <frame src="files/left.jsp" name="leftFrame" scrolling="No" noresize="noresize" 
    id="leftFrame" title="leftFrame" />
    <frame src="files/mainfra.html" name="mainFrame" id="mainFrame" title="mainFrame" />
  </frameset>
</frameset>
<noframes>

该html代码在<head></head>与<body></body>之间。

2、left.jsp

<tr>
  <td width="9%" height="20" >
    <img id="xiaotu20" src="../images/ico06.gif" width="8" height="12" /></td>
  <td width="91%"><a href="addPerson.jsp" target="mainFrame"
  class="left-font03" onClick="tupian('20');">添加人员信息</a></td>
</tr>
<tr>
  <td width="9%" height="21" >
    <img id="xiaotu21" src="../images/ico06.gif" width="8" height="12" /></td>
  <td width="91%"><a href="/test0821/PersonServlet?method=a"
  method="get" target="mainFrame" class="left-font03" >人员信息查看</a></td>
</tr>

3、addPerson.jsp

<body class="ContentBody">
  <form action="/test0821/PersonServlet" method="post" name="fom" id="fom">
  <input type="hidden" name="method" value="add" />
	<div class="MainDiv">
	  <table width="99%" border="0" cellpadding="0" cellspacing="0" class="CContent">
  	  <tr><th class="tablestyle_title" >人员添加页面</th></tr>
  	  <tr><td class="CPanel">
		<table border="0" cellpadding="0" cellspacing="0" style="width:100%">
		  <tr><td align="left">
			<input type="button" name="Submit" value="保存" class="button"
            οnclick="alert('保存成功!');"/> 
			<input type="button" name="Submit2" value="返回" class="button"     
            οnclick="window.history.go(-1);"/>
		  </td></tr>
		  <tr><td width="100%">
			<fieldset style="height:100%;">
			  <legend>添加人员</legend>
				<table border="0" cellpadding="2" cellspacing="1" style="width:100%">
			      <tr><td nowrap align="right" width="13%">编号:</td>
					<td width="41%"><input type="text" name="id" class="text" 
                    style="width:250px"  size="40" />
				    <span class="red"> *</span></td>
					<td align="right" width="19%">姓名:</td>
					<td width="27%"><input name="name" id="Input22" class="text" 
                    style="width:154px" /></td>
				  </tr>
				  <tr><td nowrap align="right">性别:</td>
					<td><input type="radio" name="sex" value="男" />男
                        <input type="radio" name="sex" value="女" />女</td>
					<td align="right">年龄:</td>
				    <td><input name="age" id="Input" class="text" style="width:154px" />
                  </td></tr>
				  <tr><td nowrap align="right">电话:</td>
					  <td><input name="phone" id="Input" class="text"
                      style="width:154px" /></td>
				  </tr>
				</table>
			 	<br />
			  </fieldset></td></tr></table></td></tr>
		<tr>
			<td colspan="2" align="center" height="50px">
			<input type="button" name="Submit" value="保存" class="button" 
            οnclick="link();"/> 
			<input type="button" name="Submit2" value="返回" class="button" 
            οnclick="window.history.go(-1);"/></TD>
		</tr>
	  </table>

	</div>
  </form>
</body>

4、listPerson.jsp

<SCRIPT language=JavaScript>
function selectAll(){
	var obj = document.fom.elements;
	for (var i=0;i<obj.length;i++){
		if (obj[i].name == "delid"){
			obj[i].checked = true;
		}
	}
}

function unselectAll(){
	var obj = document.fom.elements;
	for (var i=0;i<obj.length;i++){
		if (obj[i].name == "delid"){
			if (obj[i].checked==true) obj[i].checked = false;
			else obj[i].checked = true;
		}
	}
}

function link(){
    document.getElementById("fom").action="addrenwu.htm";
   document.getElementById("fom").submit();
}

function deleteMul(){
	var v = document.getElementsByName("delid");
	var url="<%=path %>/PersonServlet?method=delmul";
	/* alert(url); */
	for(var i=0; i<v.length; i++){
		var ck_person = v[i];
		if(ck_person.checked==true){
			/* alert(ck_person.value); */
			url = url + "&id=" + ck_person.value;
		}
	}
	window.location.href = url;
}

function go(){
	var v = document.getElementById("turnTo");
	var url="<%=path %>/PersonServlet?method=a";
	/* alert(url+"&turnTo="+v.value); */
	window.location.href = url+"&turnTo="+v.value;
}

</SCRIPT>

<body>
  <form name="fom" id="fom" method="post" action="/test0821/PersonServlet">
  	<input type="hidden" name="method" value="findname" />
	<table width="100%" border="0" cellspacing="0" cellpadding="0">
  
      <tr>
    	<td height="30">      
    	  <table width="100%" border="0" cellspacing="0" cellpadding="0">
        	<tr>
          	  <td height="62" background="../images/nav04.gif">

		        <table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
		    	  <tr>
				    <td width="21"><img src="../images/ico07.gif" width="20" height="18" /></td>
				    <td width="538">查看人员:按姓名:
					  <input name="username" type="text" size="12" />
					  <input name="Submit4" type="submit" class="right-button02" value="查 询" /></td>
			   		<td width="144" align="left">
			   		  <a href="files/superQuery.jsp">
			     	    <input name="Submit3" type="button" class="right-button07" value="高级搜索" />
			     	  </a>
			   	    </td>	
		    	  </tr>
          		</table>
          	  </td>
        	</tr>
         </table></td></tr>
     <tr>
       <td><table id="subtree1" style="DISPLAY: " width="100%" border="0" cellspacing="0" cellpadding="0">
         <tr>
           <td><table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
          	 <tr>
               <td height="20"><span class="newfont07">选择:
                 <a href="#" class="right-font08" οnclick="selectAll();">全选</a>-
                 <a href="#" class="right-font08" οnclick="unselectAll();">反选</a></span>
	             <input name="Submit" type="button" class="right-button08" value="删除所选人员" οnclick="deleteMul()" />
	             <input name="Submit2" type="button" class="right-button08" value="添加人员" οnclick="link();"/></td>
          	 </tr>
             <tr>
               <td height="40" class="font42">
			   <table width="100%" border="0" cellpadding="4" cellspacing="1" bgcolor="#464646" class="newfont03">
				 <tr class="CTitle" >
                   <td height="22" colspan="7" align="center" style="font-size:16px">人员信息详情</td>
                 </tr>
                 <tr bgcolor="#EEEEEE">
                   <td width="4%">选择</td>
                   <td width="10%">人员编号</td>
				   <td width="10%">姓名</td>
                   <td width="10%">性别</td>
				   <td width="10%">年龄</td>
				   <td width="5%">电话</td>
				   <td width="12%">操作</td>
                 </tr>
                 <c:forEach items="${list }" var="v">
	               <tr bgcolor="#FFFFFF">
	                 <td><input type="checkbox" name="delid" value="${v.id }" /></td>
	                 <td>${v.id }</td>
					 <td>${v.name }</td>
	                 <td>${v.sex }</td>
	                 <td>${v.age }</td>
	                 <td>${v.phone }</td>
	                 <td><a href="/test0821/PersonServlet?method=findid&cid=${v.id }">编辑|</a>
	                 <a href="files/listrenwumingxi.htm">查看|</a>
				     <a href="/test0821/PersonServlet?method=del&cid=${v.id }">删除</a></td>
	               </tr>
	             </c:forEach>
                 </table></td>
               </tr>
             </table>
             <table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
        	   <tr>
          		 <td height="6"><img src="../images/spacer.gif" width="1" height="1" /></td>
        	   </tr>
        	   <tr>
          		 <td height="33"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="right-font08">
               	   <tr>
                	 <td width="50%">共 <span class="right-text09">${page }</span> 页 | 第 <span class="right-text09">${currentpage }</span> 页</td>
                	 <td width="49%" align="right">[<a href="/test0821/PersonServlet?method=a" class="right-font08">首页</a> | 
                	   <a href="PersonServlet?method=a&currentpage=${currentpage-1==0?1: currentpage-1}" class="right-font08">上一页</a> | 
                	   <a href="/test0821/PersonServlet?method=a&currentpage=${currentpage+1==page?page: currentpage+1}" class="right-font08" method="get">下一页</a> | 
                	   <a href="/test0821/PersonServlet?method=a&currentpage=${page}" class="right-font08">末页</a>] 转至:</td>
                     <td width="1%"><table width="20" border="0" cellspacing="0" cellpadding="0">
                       <tr>
                         <td width="1%"><input id="turnTo" name="turnTo" type="text" class="right-textfield03" size="1" /></td>
                         <td width="87%"><input type="button" class="right-button06" οnclick="go()" value="Go"/></td>
                       </tr>
                     </table></td>
                   </tr>
                 </table></td>
               </tr>
             </table></td>
           </tr>
		 </table></td>
	   </tr></table>
	 </form>
  </body>

(1)判断方法

<a href="PersonServlet?method=a&currentpage=${currentpage-1==0?1: currentpage-1}" class="right-font08">上一页</a>

该行代码中,用到了<判断条件>?<结果1>:<结果2>来传递值。

(2)使用js来传值和跳转

页面跳转使用function go()方法来实现,姓名模糊查询使用表单提交来实现。

这是因为js的url传递中不能传递汉字,而姓名查询显然拥有汉字。

 5、editPerson.jsp

<body class="ContentBody">
  <form action="/test0821/PersonServlet" method="post" name="fom" id="fom">
  <input type="hidden" name="method" value="update" />
	<div class="MainDiv">
	  <table width="99%" border="0" cellpadding="0" cellspacing="0" class="CContent">
  		<tr>
      	  <th class="tablestyle_title" >人员修改页面</th>
  		</tr>
  		<tr>
    	  <td class="CPanel">
			<table border="0" cellpadding="0" cellspacing="0" style="width:100%">
			  <tr><td align="left">
				<input type="button" name="Submit" value="保存" class="button" οnclick="alert('保存成功!');"/> 
				<input type="button" name="Submit2" value="返回" class="button" οnclick="window.history.go(-1);"/>
			  </td></tr>
			  <tr>
				<td width="100%">
				  <fieldset style="height:100%;">
				    <legend>修改人员</legend>
					<table border="0" cellpadding="2" cellspacing="1" style="width:100%">
					  <tr>
					    <td nowrap align="right" width="13%">编号:</td>
					    <td><input name="id" id="Input21" class="text" style="width:154px" value="${p1.id }" /></td>
					    <td align="right" width="19%">姓名:</td>
					    <td width="27%">
					      <input name="name" id="Input22" class="text" style="width:154px" value="${p1.name }" />
					    </td>
					  </tr>
					  <tr>
					    <td nowrap align="right">性别:</td>
					    <td>
                            <input type="radio" name="sex" value="男" <c:if test="${p1.sex=='男' }">checked</c:if>/>男
                            <input type="radio" name="sex" value="女" <c:if test="${p1.sex=='女' }">checked</c:if>/>女
                         </td>
					    <td align="right">年龄:</td>
					    <td><input name="age" id="Input" class="text" style="width:154px" value="${p1.age }" /></td>
					  </tr>
					  <tr>
					    <td nowrap align="right">电话:</td>
					    <td><input name="phone" id="Input" class="text" style="width:154px" value="${p1.phone }" /></td>
					  </tr>
					</table>
			 		<br />
				  </fieldset>			
				</td>
			  </tr>
			</table>
	      </td>
        </tr>
  
		<tr>
			<td colspan="2" align="center" height="50px">
			<input type="button" name="Submit" value="保存" class="button" οnclick="link();"/> 
			<input type="button" name="Submit2" value="返回" class="button" οnclick="window.history.go(-1);"/></TD>
		</tr>
	  </table>

	</div>
  </form>
</body>

6、superQuery.jsp

<body class="ContentBody">
  <form action="<%=path %>/PersonServlet" method="post" name="form">
  	<input type="hidden" name="method" value="super" />
	<div class="MainDiv">
	  <table width="99%" border="0" cellpadding="0" cellspacing="0" class="CContent">
  		<tr><th class="tablestyle_title" >高级搜索</th></tr>
  		<tr><td class="CPanel">
		  <table border="0" cellpadding="0" cellspacing="0" style="width:100%">
			<tr id="zdjh">
			  <td width="100%">
				<fieldset style="height:100%;">
				  <table border="0" cellpadding="2" cellspacing="1" style="width:100%">
				    <tr>
					  <td nowrap align="right" width="15%">用户姓名:</td>
					  <td width="35%"><input name="name" type="text" class="text" style="width:154px" />
					</tr>
					<tr>
					  <td align="right">性别:</td>
					  <td><select name="sex" >
                        <option value='' selected="selected">==请选择==</option>
                        <option value="男">男</option>
                        <option value="女">女</option>
                      </select></td>
					</tr>
					<tr>
					  <td nowrap align="right" width="15%">电话:</td>
					  <td width="35%"><input name="phone" type="text" class="text" style="width:154px"/>
					</tr>
				  </table>
			      <br />
				</fieldset>
			  </td>
		    </tr>
		  </table>
	    </td></tr>
		<tr>
			<td colspan="2" align="center" height="50px">
			  <input type="submit" name="Submit" value="搜索" class="button" /> 
			  <input type="button" name="Submit2" value="返回" class="button" οnclick="window.history.go(-1);"/>
			</td>
		</tr>
      </table>
	</div>
  </form>
</body>

发布了73 篇原创文章 · 获赞 55 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_35756383/article/details/81914285
今日推荐