SpringMVC+Spring+HIbernate 简单增删改查实例


这里写图片描述


SpringMVC+Spring+HIbernate 简单增删改查实例

HIbernate配置mysql数据库的方式 和 Structs+spring+HIbernate 是一样的。
可以理解为SpringMVC 把 Structs2 替代了,SpringMVC 中的 Controller 对应 Structs2 中的 Action,相对来说更加的简单,毕竟少了一个配置文件struts.xml,这个实例中连xx.hbm.xml都用注解的方式代替了
对应页面回显数据,Struts2中,用值栈、session、Request等,SpringMVC中也可以使用Servlet API,session等都可以用,可以用 Map、Model等来回显数据
页面上可以使用JSTL,Struts的 s标签就没了

<s:iterator value="list" var="d">
<c:forEach var="p" items="${requestScope.personlist }">

删除和修改都是url中传入id的参数

http://localhost:8080/../doupdate?id=402881e958de29980158de2aa5440000

项目结构

这里写图片描述
这里写图片描述

除了spring和HIbernate基本包还需要加 jstl标签库的jar包
jstl.jar standard.jar

HIbernate c3p0 jar包,
c3p0-0.9.2.1.jar hibernate-c3p0-4.3.11.Final.jar mchange-commons-java-0.2.3.4.jar 3个
对应在 hibernate-release-4.3.11.Final\lib\optional\c3p0目录下

mysql数据库连接的jar包
mysql-connector-java-5.1.40-bin.jar

jar包在源码里有

没有使用各层的接口

这里写图片描述

版本信息

Eclipse版本 Neon.1a Release (4.6.1)
Spring 4.3.4
Hibernate 4.3.11
Tomcat 7.0
JDK 1.8
数据库 MySQL5.7.12

源码下载:http://download.csdn.net/detail/peng_hong_fu/9706477

部分源代码

业务层和Dao层没有使用接口(解耦等)

web.xml

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringMVC_Spring_Hibernate</display-name>


  <!-- 防止中文参数乱码 放在前面 -->  
      <filter>  
          <filter-name>SetCharacterEncoding</filter-name>  
          <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
          <init-param>  
               <param-name>encoding</param-name>  
               <param-value>UTF-8</param-value>  
          </init-param>  
          <init-param>  
               <param-name>forceEncoding</param-name>  
               <param-value>true</param-value>  
               <!-- 强制进行转码 -->  
          </init-param>  
     </filter>  
     <filter-mapping>  
          <filter-name>SetCharacterEncoding</filter-name>  
          <url-pattern>/*</url-pattern>  
     </filter-mapping>  

    <!-- spring 配置Listener-->

    <!-- needed for ContextLoaderListener -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/resources/beans.xml</param-value>
    </context-param>

    <!-- Bootstraps the root web application context before servlet initialization -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- springmvc配置dispatcherServlet -->

    <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:/resources/springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

实体类

Person.java

package com.jxust.svsh.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

/**
 * 个人信息实体类
 * 注解方式配置对应数据表
 * @author Peng
 * @Date2016年12月8日下午2:54:52
 */
@Entity
@Table(name = "person")
public class Person {

    private String id;// 主键id
    private String name;// 姓名
    private String idCard;// 身份证号
    private String phone;// 手机号
    private String address;// 地址

    public Person() {
        super();
    }

    public Person(String name, String idCard, String phone, String address) {
        super();
        this.name = name;
        this.idCard = idCard;
        this.phone = phone;
        this.address = address;
    }

    @Id
    @Column(name = "id", nullable = false, unique = true)
    @GenericGenerator(name = "generator", strategy = "uuid")
    @GeneratedValue(generator = "generator")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Column(name = "name", nullable = false, length = 32)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "idCard", nullable = false, length = 32)
    public String getIdCard() {
        return idCard;
    }

    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }

    @Column(name = "phone", nullable = false, length = 32)
    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Column(name = "address", nullable = false, length = 32)
    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + ", idCard=" + idCard + ", phone=" + phone + ", address="
                + address + "]";
    }
}

Service业务层

PersonService.java

package com.jxust.svsh.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jxust.svsh.dao.PersonDAO;
import com.jxust.svsh.entity.Person;

@Transactional
@Service
public class PersonService {

    @Autowired
    public PersonDAO personDAO;

    /**
     * 添加
     * @param person
     */
    public void addPerson(Person person) {
        personDAO.addPerson(person);
    }
    /**
     * 根据id查询
     * @param id
     * @return
     */
    public Person getPersonById(String id){
        return personDAO.getPersonById(id);
    }
    /**
     * 更新
     * @param person
     */
    public void updatePerson(Person person) {
        personDAO.updatePerson(person);
    }
    /**
     * 删除
     * @param id
     */
    public void deletePersonById(String  id) {
        personDAO.deletePersonById(id);
    }
    /**
     * 查询所有
     * @return
     */
    public List<Person> getPersons() {
        return personDAO.getPersons();
    }
}

Dao层

PersonDAO.java

package com.jxust.svsh.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.jxust.svsh.entity.Person;

@Repository
public class PersonDAO {

    @Resource
    private SessionFactory sessionFactory;

    private Session getSession() {
        return sessionFactory.getCurrentSession();
    }
    /**
     * 根据id查询
     * @param id
     * @return
     */
    public Person getPersonById(String id) {
        return (Person) this.getSession().createQuery("from Person where id=?").setParameter(0, id).uniqueResult();
    }
    /**
     * 添加
     * @param person
     */
    public void addPerson(Person person) {
        this.getSession().save(person);
    }
    /**
     * 更新
     * @param person
     */
    public void updatePerson(Person person) {
        this.getSession().update(person);
    }
    /**
     * 删除
     * @param id
     */
    public void deletePersonById(String id) {
        this.getSession().createQuery("delete Person where id=?").setParameter(0, id).executeUpdate();
    }
    /**
     * 查询所有
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<Person> getPersons() {
        return this.getSession().createCriteria(Person.class).list();
    }
}

Controller

PersonController.java

package com.jxust.svsh.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.jxust.svsh.entity.Person;
import com.jxust.svsh.service.PersonService;
/**
 * controller
 * @author Peng
 * @Date2016年12月9日上午11:25:40
 */
@SessionAttributes(value = "username")
@Controller
@RequestMapping(value = "/person")
public class PersonController {

    @Autowired
    public PersonService personService;

    /**
     * 登录请求,失败返回error.jsp
     * 
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/login")
    public String dologin(String username, String password, Map<String, Object> map) {
        if (username.equals("admin") && password.equals("admin")) {
            map.put("username", username);//存放在request请求域中
            /**
             * 类上加上@SessionAttributes({"username"}) 同时也会存放在 session域中
             * @SessionAttributes 除了可以通过属性名指定需要存放到会话中的属性外(使用的是value属性值)
             * 还可以通过模型属性的对象类型指定哪些模型属性需要放到会话中(实际上使用的是types属性值),
             */
            return "frame";
        }
        return "error";
    }

    /**
     * 保存添加的数据
     * 
     * @param person
     * @return
     */
    @RequestMapping(value = "/saveperson")
    public String saveperson(Person person) {
        personService.addPerson(person);
        return "redirect:main";
    }

    /**
     * 跳转到添加页面
     * savepage.jsp
     * @return
     */
    @RequestMapping(value = "/addperson")
    public String saveperson() {

        return "savepage";
    }

    /**
     * 删除一条数据
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/deletePersonById")
    public String deletePersonById(@RequestParam(value = "id") String id) {
        System.out.println("删除单个");
        personService.deletePersonById(id);
        return "redirect:main";
    }

    /**
     * 跳转到更新页面,回显数据
     * editpage.jsp
     * @param id
     * @param model 使用的Model保存回显数据
     * @return
     */
    @RequestMapping(value = "/doupdate")
    public String doupdate(@RequestParam(value = "id") String id, Model model) {
        model.addAttribute("person", personService.getPersonById(id));
        return "editpage";
    }

    /**
     * 更新数据
     * 
     * @param person
     * @return
     */
    @RequestMapping(value = "/updateperson")
    public String updateperson(Person person) {
        System.out.println(person.toString());
        personService.updatePerson(person);
        return "redirect:main";
    }

    /**
     * 查询所有人员信息
     * 
     * @param map 使用的是map保存回显数据
     * @return
     */
    @RequestMapping(value = "/main")
    public String mian(Map<String, Object> map) {
        map.put("personlist", personService.getPersons());
        /*遍历集合,查看查询到的数据
         * List<Person> lists = personService.getPersons(); 
         * Iterator<Person> it = lists.iterator(); 
         * while(it.hasNext()){ 
         *      Person p =it.next();
         *      System.out.println(p.toString());
         *  }
         */
        return "main";
    }
}

xml等配置文件

springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

    <!-- 配置自动扫描的包 -->
    <context:component-scan base-package="com.jxust">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    </context:component-scan>


    <!-- 配置视图解析器 如何把 handler 方法返回值解析为实际的物理视图 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--  静态资源交给默认的Servlet-->
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven></mvc:annotation-driven>
</beans>

jdbc.properties

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/ssh_person?characterEncoding=utf8&useSSL=true
jdbc.username=root
jdbc.password=root

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">

    <context:component-scan base-package="com.jxust">
        <context:exclude-filter type="annotation" 
        expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!-- 引入外部的属性文件 -->
    <context:property-placeholder location="classpath:/resources/jdbc.properties"/>

    <!-- 配置c3p0 连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        
        <property name="driverClass" value="${jdbc.driverClass}"/>        
        <property name="jdbcUrl" value="${jdbc.url}"/>        
        <property name="user" value="${jdbc.username}"/>        
        <property name="password" value="${jdbc.password}"/>        
    </bean>
     <!-- 配置hibernate 相关属性--> 
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!-- 注入连接池-->
        <property name="dataSource" ref="dataSource"></property>
        <!-- hibernate 的相关属性 -->
        <property name="hibernateProperties" >
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.format_sql">true </prop>  
                <!-- 
                            它包含4个属性: 
                * create : 会根据你的model类来生成表,但是每次运行都会删除上一次的表,重新生成表,哪怕2次没有任何改变 
                * create-drop : 根据model类生成表,但是sessionFactory一关闭,表就自动删除 
                * update : 最常用的属性,也根据model类生成表,即使表结构改变了,表中的行仍然存在,不会删除以前的行 
                * validate : 只会和数据库中的表进行比较,不会创建新表,但是会插入新值 
                 -->
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        <!-- hibernate 映射文件  设置为自动扫描包目录-->
         <property name="packagesToScan">
            <list>
                <value>com.jxust.svsh.entity</value>
            </list>
        </property>
    </bean> 

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- 开启事务注解 -->
    <tx:annotation-driven transaction-manager="transactionManager"  />
</beans>

笔记.txt

C:\Users\Peng>mysql -uroot -proot
..
..
mysql> use ssh_person;//要先创建数据库
Database changed
mysql> desc person;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id      | varchar(255) | NO   | PRI | NULL    |       |
| address | varchar(255) | YES  |     | NULL    |       |
| idCard  | varchar(32)  | NO   |     | NULL    |       |
| name    | varchar(32)  | NO   |     | NULL    |       |
| phone   | varchar(32)  | NO   |     | NULL    |       |
+---------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> select * from person;
+----------------------------------+----------+-----------+------+---------+
| id                               | address  | idCard    | name | phone   |
+----------------------------------+----------+-----------+------+---------+
| 402881e958de29980158de2aa5440000 | 江西南昌 | 20020020  | 天天 | 1008611 |
| 402881e958deb56b0158deb6a6200000 | 江西上饶 | 20020025  | 霍霍 | 1008614 |
| 402881e958dece620158decf2e3b0000 | 湖南长沙 | 230000032 | 胖子 | 1008677 |
+----------------------------------+----------+-----------+------+---------+
3 rows in set (0.00 sec)

jsp页面

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登陆</title>
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/style/reset.css">
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/style/main.css">
<!--[if IE 6]>
<script type="text/javascript" src="js/DD_belatedPNG_0.0.8a-min.js"></script>
<script type="text/javascript" src="js/ie6Fixpng.js"></script>
<![endif]-->
</head>

<body>
<div class="headerBar">
    <div class="logoBar login_logo">
        <div class="comWidth">
            <div class="logo fl">
                <a href="#"><img src="${pageContext.request.contextPath}/images/logo.jpg" alt="慕课网"></a>
            </div>
            <h3 class="welcome_title">欢迎登陆</h3>
        </div>
    </div>
</div>

<form action="person/login" method="post" >
<div class="loginBox">  

    <div class="login_cont">
        <ul class="login">
            <li class="l_tit">用户名</li>
            <li class="mb_10"><input type="text" name="username" class="login_input user_icon"></li>
            <li class="l_tit">密码</li>
            <li class="mb_10"><input type="password" name="password" class="login_input user_icon"></li>

            <li><input type="submit" value="" class="login_btn"></li>
        </ul>
        <div class="login_partners">
            <p class="l_tit">使用合作方账号登陆网站</p>
            <ul class="login_list clearfix">
                <li><a href="#">QQ</a></li>
                <li><span>|</span></li>
                <li><a href="#">网易</a></li>
                <li><span>|</span></li>
                <li><a href="#">新浪微博</a></li>
                <li><span>|</span></li>
                <li><a href="#">腾讯微薄</a></li>
                <li><span>|</span></li>
                <li><a href="#">新浪微博</a></li>
                <li><span>|</span></li>
                <li><a href="#">腾讯微薄</a></li>
            </ul>
        </div>
    </div>

</div>
</form>

<div class="hr_25"></div>
<div class="footer">
    <p><a href="#">慕课简介</a><i>|</i><a href="#">慕课公告</a><i>|</i> <a href="#">招纳贤士</a><i>|</i><a href="#">联系我们</a><i>|</i>客服热线:400-675-1234</p>
    <p>Copyright &copy; 2006 - 2014 慕课版权所有&nbsp;&nbsp;&nbsp;京ICP备09037834号&nbsp;&nbsp;&nbsp;京ICP证B1034-8373号&nbsp;&nbsp;&nbsp;某市公安局XX分局备案编号:123456789123</p>
    <p class="web"><a href="#"><img src="${pageContext.request.contextPath}/images/webLogo.jpg" alt="logo"></a><a href="#"><img src="${pageContext.request.contextPath}/images/webLogo.jpg" alt="logo"></a><a href="#"><img src="${pageContext.request.contextPath}/images/webLogo.jpg" alt="logo"></a><a href="#"><img src="${pageContext.request.contextPath}/images/webLogo.jpg" alt="logo"></a></p>
</div>
</body>
</html>

frame.jsp包含top.jsp 、right.jsp和left.jsp 3个页面

frame.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>员工管理系统</title>
</head>

<frameset rows="80,*">
    <frame name="top"
        src="${pageContext.request.contextPath}/frame/top.jsp">
    <frameset cols="150,*" id="main">
        <frame src="${pageContext.request.contextPath}/frame/left.jsp">
        <frame name="right"
            src="${pageContext.request.contextPath}/frame/right.jsp">
    </frameset>
</frameset>

</html>

top.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
.div1{ margin-top:50px; margin-left:600px; font-size:14px; color:white}
</style>
</head>
<body bgcolor="#0099FF">
<div class="div1">
欢迎您: ${sessionScope.username}
</div>
</body>
</html>

right.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body bgColor=#DDF0FB>
</body>
</html>

left.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="dtree.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="dtree.js"></script>
</head>

<body bgColor=#DDF0FB leftMargin=0 topMargin=0 marginwidth="0" marginheight="0">
<table width="90%" border="0" cellspacing="1" cellpadding="2" align="center">
<div class="dtree">
  <script type="text/javascript">
    d=new dTree('d');

    d.add('01','-1','信息管理系统');
    d.add('0101','01','人员管理');
    d.add('010101','0101','人员列表','${pageContext.request.contextPath}/person/main','','right');

    d.add('010102','0101','新增人员','${pageContext.request.contextPath}/person/addperson','','right');
    document.write(d);
  </script>
</div>
</table>

</body>
</html>

main.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<style type="text/css">
        .table1{
            border:1px solid #ddd;
            width:900px;

        }
        thead{
            background-color:lightblue;
        }

    </style>
</head>
<body>
<table border="0" width="900px">
<tr>
<td align="center" style="font-size:24px; color:#666"> 人员管理</td>
</tr>
<tr>
<td align="right" > <a href="${pageContext.request.contextPath}/person/addperson">添加</a></td>
</tr>
</table>
<br/>
<table cellspacing="0" border="1" class="table1">
<thead>
   <tr>
        <th width="300">姓名</th>
        <th width="300">身份证号</th>
        <th width="300">电话</th>
        <th width="300">地址</th>
        <th  width="300">编辑</th>
        <th  width="300">删除</th>
   </tr>
</thead>
<tbody>

<c:forEach var="p" items="${requestScope.personlist }">
    <tr>
        <td align="center">${p.name }</td>
        <td align="center">${p.idCard }</td>
        <td align="center">${p.phone }</td>
        <td align="center">${p.address }</td>
        <td align="center">
            <a href="${pageContext.request.contextPath}/person/doupdate?id=${p.id}"><img src="<%=basePath %>images/编辑.png"></a>
        </td>
        <td align="center">
            <a href="${pageContext.request.contextPath}/person/deletePersonById?id=${p.id}" onclick='return confirm("确认要删除吗?")'><img src="<%=basePath %>images/trash.gif"></a>
        </td>
    </tr>
</c:forEach>
</tbody>
</table>
<br/>
</body>
</html>

editpage.jsp

<%@ page language="java" import="java.util.*"
    contentType="text/html; charset=UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<html>
<head></head>
<body>

    <h3>员工编辑</h3>

    <br />
    <br />
    <!-- action对应一个action标签,id对应提交时的对应关系 -->
    <form id="saveForm" action="${pageContext.request.contextPath}/person/updateperson" method="post">
        <input type="hidden" name="id" value="${person.id }" />
        <table style="font-size: :16px">
            <tr>
                <td>姓名:</td>
                <td><input type="text" value="${person.name }" name="name" /></td>
            </tr>
            <tr>
                <td>身份证号码:</td>
                <td><input type="text" value="${person.idCard }" name="idCard" /></td>
            </tr>
            <tr>
            <tr>
                <td>手机号:</td>
                <td><input type="text" value="${person.phone }" name="phone" /></td>
            </tr>
            <tr>
                <td>地址:</td>
                <td><input type="text" value="${person.address }" name="address" /></td>
            </tr>
            <tr>

                <td align="right">
                <input type="submit" value="更新" /> &nbsp;&nbsp;<a href="javascript:history.go(-1)">退回 </a>
            </tr>
        </table>
    </form>
            <!-- <tr>

                <td align="right"><a
                    href="javascript:document.getElementById('saveForm').submit()">保存</a>
                    &nbsp;&nbsp; <a href="javascript:history.go(-1)">退回 </a></td>
            </tr>
             -->
</body>
</html>

savepage.jsp

<%@ page language="java" import="java.util.*"
    contentType="text/html; charset=UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<html>
<head></head>
<body>

    <h3>人员添加</h3>

    <br />
    <br />
    <form id="saveForm" action="${pageContext.request.contextPath}/person/saveperson" method="post">
        <table style="font-size: :16px">
            <tr>
                <td>姓名:</td>
                <td><input type="text" value="${person.name }" name="name" /></td>
            </tr>
            <tr>
                <td>身份证号码:</td>
                <td><input type="text" value="${person.idCard }" name="idCard" /></td>
            </tr>
            <tr>
            <tr>
                <td>手机号:</td>
                <td><input type="text" value="${person.phone }" name="phone" /></td>
            </tr>
            <tr>
                <td>地址:</td>
                <td><input type="text" value="${person.address }" name="address" /></td>
            </tr>
            <tr>

                <td align="right">
                <input type="submit" value="添加" /> &nbsp;&nbsp;<a href="javascript:history.go(-1)">退回 </a>
            </tr>
        </table>
    </form>
</body>
</html>

error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h3>登录失败</h3>
    <a href="${pageContext.request.contextPath}/index.jsp">重新登录</a>
</body>
</html>

源码下载

源码下载:http://download.csdn.net/detail/peng_hong_fu/9706477

  • 12
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 26
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 26
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值