SSSP整合分页

简介

Spring、SpringMVC、SpringData、JPA的整合的一个例子
实现:

  1. SpringMVC、Spring、SpringData\JPA 整合完成 CRUD、翻页
  2. 基于Restful 风格
  3. 使用 JPA 二级缓存

项目截图

在这里插入图片描述

导入jar包

  • 导入Spring、SpringMVC、Hibernate、SpringData、MySql的jar包。

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>sssp</display-name>
	<!-- 1. 配置 Spring 的核心监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 配置spring的applicationContext.xml -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:config/spring.xml</param-value>
	</context-param>

	<!-- 2. 配置字符编码过滤器,必须在所有的filter最前面 -->
	<filter>
		<filter-name>CharacterEncodingFilter</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>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- 3. 配置可以把 POST 请求转为 DELETE 或 POST 请求 -->
	<filter>
		<filter-name>HiddenHttpMethodFilter</filter-name>
		<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>HiddenHttpMethodFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- 4. 配置 OpenEntityManagerInViewFilter. 可以解决懒加载异常的问题 -->
	<filter>
		<filter-name>OpenEntityManagerInViewFilter</filter-name>
		<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>OpenEntityManagerInViewFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- 5. 配置 SpringMVC 的DispatcherServlet -->
	<servlet>
		<servlet-name>DispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:config/spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>DispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

配置数据源

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/sssp
jdbc.username=root
jdbc.password=root

配置JPA的二级缓存配置文件ehcache.xml

<ehcache>

    <!-- Sets the path to the directory where cache .data files are created.

         If the path is a Java System Property it is replaced by
         its value in the running VM.

         The following properties are translated:
         user.home - User's home directory
         user.dir - User's current working directory
         java.io.tmpdir - Default temp file path -->
    <diskStore path="java.io.tmpdir"/>


    <!--Default Cache configuration. These will applied to caches programmatically created through
        the CacheManager.

        The following attributes are required for defaultCache:

        maxInMemory       - Sets the maximum number of objects that will be created in memory
        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
                            is never expired.
        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
                            if the element is not eternal. Idle time is now - last accessed time
        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
                            if the element is not eternal. TTL is now - creation time
        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
                            has reached the maxInMemory limit.

        -->
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        />

    <!--Predefined caches.  Add your cache configuration settings here.
        If you do not have a configuration for your cache a WARNING will be issued when the
        CacheManager starts

        The following attributes are required for defaultCache:

        name              - Sets the name of the cache. This is used to identify the cache. It must be unique.
        maxInMemory       - Sets the maximum number of objects that will be created in memory
        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
                            is never expired.
        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
                            if the element is not eternal. Idle time is now - last accessed time
        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
                            if the element is not eternal. TTL is now - creation time
        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
                            has reached the maxInMemory limit.

        -->

    <!-- Sample cache named sampleCache1
        This cache contains a maximum in memory of 10000 elements, and will expire
        an element if it is idle for more than 5 minutes and lives for more than
        10 minutes.

        If there are more than 10000 elements it will overflow to the
        disk cache, which in this configuration will go to wherever java.io.tmp is
        defined on your system. On a standard Linux system this will be /tmp"
        -->
    <cache name="sampleCache1"
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
        overflowToDisk="true"
        />

    <!-- Sample cache named sampleCache2
        This cache contains 1000 elements. Elements will always be held in memory.
        They are not expired. -->
    <cache name="sampleCache2"
        maxElementsInMemory="1000"
        eternal="true"
        timeToIdleSeconds="0"
        timeToLiveSeconds="0"
        overflowToDisk="false"
        /> -->
    <!-- Place configuration for your caches following -->
</ehcache>

Spring的配置文件spring.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:context="http://www.springframework.org/schema/context"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa" 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/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
	<!-- 1. 配置自动扫描的包 -->
	<context:component-scan base-package="com.znsd.sssp">
		<!-- 过滤SpringMVC的注解不扫描 -->
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
		<context:exclude-filter type="annotation"
			expression="org.springframework.web.bind.annotation.ControllerAdvice" />
	</context:component-scan>

	<!-- 2. 配置数据源 -->
	<context:property-placeholder location="classpath:config/db.properties" />
	<!-- C3P0连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="jdbcUrl" value="${jdbc.url}"></property>
	</bean>

	<!-- 3. 配置 JPA 的EntityManagerFactory -->
	<bean id="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- 3.1 配置 JPA 提供商的适配器. 可以通过内部 bean 的方式来配置 -->
		<property name="jpaVendorAdapter">
			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
		</property>
		<!-- 3.2 配置实体类所在的包 -->
		<property name="packagesToScan" value="com.znsd.sssp.entities" />
		<!-- 3.3 配置 JPA 的基本属性 -->
		<property name="jpaProperties">
			<props>
				<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
				<!-- Hibernate 基本属性 -->
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<!-- 二级缓存相关 -->
				<prop key="hibernate.cache.use_second_level_cache">true</prop>
				<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory
				</prop>
				<prop key="hibernate.cache.use_query_cache">true</prop>
			</props>
		</property>
		<!-- 设置二级缓存开关 -->
		<property name="sharedCacheMode" value="ENABLE_SELECTIVE"></property>
	</bean>

	<!-- 4. 配置事务管理 -->
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory"></property>
	</bean>

	<!-- 5. 配置支持注解的事务 -->
	<tx:annotation-driven transaction-manager="transactionManager" />

	<!-- 6. 配置 SpringData -->
	<!-- 加入 jpa 的命名空间 -->
	<!-- base-package: 扫描 Repository Bean 所在的 package -->
	<jpa:repositories base-package="com.znsd.sssp"
		entity-manager-factory-ref="entityManagerFactory">
	</jpa:repositories>
</beans>

SpringMVC配置文件spring-mvc.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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

	<!-- 1. 配置自动扫描的包 -->
	<context:component-scan base-package="com.znsd.sssp" use-default-filters="false">
		<!-- 这两个注解交给SpringMVC管理,其他的交给 IOC 容器 -->
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
		<context:include-filter type="annotation"
			expression="org.springframework.web.bind.annotation.ControllerAdvice" />
	</context:component-scan>

	<!-- 2. 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/" />
		<property name="suffix" value=".jsp" />
	</bean>

	<mvc:default-servlet-handler />
	<mvc:annotation-driven></mvc:annotation-driven>
</beans>

创建Entity

Student.java

package com.znsd.sssp.entities;

import java.util.Date;

import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.springframework.format.annotation.DateTimeFormat;

@Table(name = "tb_student")
@Entity
@Cacheable
public class Student {
	public Student() {
		this.addTime = new Date();
	}

	public Student(String stuName, String stuPass, String sex, Integer age, String email) {
		this.stuName = stuName;
		this.stuPass = stuPass;
		this.sex = sex;
		this.age = age;
		this.email = email;
	}

	public Student(String stuName, String stuPass, String sex, Integer age, String email, Date birth, Date addTime) {
		this.stuName = stuName;
		this.stuPass = stuPass;
		this.sex = sex;
		this.age = age;
		this.email = email;
		this.birth = birth;
		this.addTime = addTime;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer stuNo;

	@Column(name = "stu_name")
	private String stuName;

	@Column(name = "stu_pass")
	private String stuPass;
	
	@Transient
	private String realPass;

	private String sex;

	// @Transient
	private Integer age;

	private String email;

	@Temporal(TemporalType.DATE)
	@DateTimeFormat(pattern="yyyy-MM-dd")
	private Date birth;

	@Temporal(TemporalType.TIME)
	private Date addTime;

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "cls_id")
	private Clazz clazz;

	public Integer getStuNo() {
		return stuNo;
	}

	public void setStuNo(Integer stuNo) {
		this.stuNo = stuNo;
	}

	public String getStuName() {
		return stuName;
	}

	public void setStuName(String stuName) {
		this.stuName = stuName;
	}

	public String getStuPass() {
		return stuPass;
	}

	public void setStuPass(String stuPass) {
		this.stuPass = stuPass;
	}

	public String getRealPass() {
		return realPass;
	}

	public void setRealPass(String realPass) {
		this.realPass = realPass;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Date getBirth() {
		return birth;
	}

	public void setBirth(Date birth) {
		this.birth = birth;
	}

	public Date getAddTime() {
		return addTime;
	}

	public void setAddTime(Date addTime) {
		this.addTime = addTime;
	}

	public Clazz getClazz() {
		return clazz;
	}

	public void setClazz(Clazz clazz) {
		this.clazz = clazz;
	}

	@Override
	public String toString() {
		return "Student [stuNo=" + stuNo + ", stuName=" + stuName + ", stuPass=" + stuPass + ", sex=" + sex + ", age="
				+ age + ", email=" + email + ", birth=" + birth + ", addTime=" + addTime + "]";
	}
}

Clazz.java

package com.znsd.sssp.entities;

import java.util.Set;

import javax.persistence.Cacheable;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Table(name = "tb_class")
@Entity
@Cacheable
public class Clazz {
	public Clazz() {
	}

	public Clazz(String clsName) {
		this.clsName = clsName;
	}

	@Id
	@GeneratedValue
	private Integer clsId;
	private String clsName;

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "clazz")
	private Set<Student> students;

	public Integer getClsId() {
		return clsId;
	}

	public void setClsId(Integer clsId) {
		this.clsId = clsId;
	}

	public String getClsName() {
		return clsName;
	}

	public void setClsName(String clsName) {
		this.clsName = clsName;
	}

	public Set<Student> getStudents() {
		return students;
	}

	public void setStudents(Set<Student> students) {
		this.students = students;
	}

	@Override
	public String toString() {
		return "Clazz [clsId=" + clsId + ", clsName=" + clsName + "]";
	}
}

Repository配置

StudentResotitory

package com.znsd.sssp.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.znsd.sssp.entities.Student;

public interface StudentRepository extends JpaRepository<Student, Integer> {

}

ClazzRepository

package com.znsd.sssp.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.znsd.sssp.entities.Clazz;

public interface ClazzRepository extends JpaRepository<Clazz, Integer>{

}

Service

StudentService

package com.znsd.sssp.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.znsd.sssp.entities.Student;
import com.znsd.sssp.repository.StudentRepository;

@Service
public class StudentService {
	@Autowired
	private StudentRepository repository;
	
	public Page<Student> getStudentList(Pageable pager){
		return repository.findAll(pager);
	}
	
	@Transactional
	public Student save(Student student){
		return repository.save(student);
	}
	
	@Transactional
	public void delete(Integer stuNo){
		repository.delete(stuNo);
	}
	
	public Student getStudentByStuNo(Integer stuNo){
		return repository.findOne(stuNo);
	}
}

ClazzService

package com.znsd.sssp.service;

import java.util.List;

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

import com.znsd.sssp.entities.Clazz;
import com.znsd.sssp.repository.ClazzRepository;

@Service
public class ClazzService {
	@Autowired
	private ClazzRepository repository;
	
	public List<Clazz> getList(){
		return repository.findAll();
	}
}

Controller

StudentController

package com.znsd.sssp.controllor;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.znsd.sssp.entities.Clazz;
import com.znsd.sssp.entities.Student;
import com.znsd.sssp.service.ClazzService;
import com.znsd.sssp.service.StudentService;

@Controller
public class StudentControllor {

	@Autowired
	private StudentService studentService;

	@Autowired
	private ClazzService clazzService;

	private void initData(Map<String, Object> map) {
		// 初始化数据
		List<Clazz> clazzlist = clazzService.getList();
		System.out.println(clazzlist);
		map.put("clazzlist", clazzlist);

		List<String> sexlist = Arrays.asList("男", "女");
		map.put("sexlist", sexlist);
	}

	@ModelAttribute
	public void getStudentByStuNo(Integer stuNo, Map<String, Object> map) {
		if (stuNo != null) {
			Student stu = studentService.getStudentByStuNo(stuNo);
			map.put("student", stu);
		}
	}

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Integer pageindex, Integer pagesize, Map<String, Object> map) {
		if (pageindex == null) {
			pageindex = 1;
		}
		if (pagesize == null) {
			pagesize = 2;
		}
		Sort sort = new Sort(Direction.DESC, "stuNo");

		Pageable pager = new PageRequest(pageindex - 1, pagesize, sort);

		Page<Student> pagelist = studentService.getStudentList(pager);
		map.put("pagelist", pagelist);

		return "list";
	}

	@RequestMapping(value = "/student", method = RequestMethod.GET)
	public String addStudentGet(Map<String, Object> map) {
		initData(map);
		map.put("student", new Student());
		return "addstudent";
	}

	@RequestMapping(value = "/student", method = RequestMethod.POST)
	public String addStudentPost(Student student, Map<String, Object> map) {
		Student result = studentService.save(student);
		if (result != null) {
			return "redirect:list";
		} else {
			initData(map);
			return "addstudent";
		}
	}

	@RequestMapping(value = "/student/{stuNo}", method = RequestMethod.GET)
	public String editStudentGet(@PathVariable("stuNo") Integer stuNo, Map<String, Object> map) {
		// 初始化数据
		initData(map);

		Student student = studentService.getStudentByStuNo(stuNo);
		map.put("student", student);

		return "editstudent";
	}

	@RequestMapping(value = "/student", method = RequestMethod.PUT)
	public String editStudentPost(Student student, Map<String, Object> map) {
		System.out.println(student);
		Student result = studentService.save(student);
		if (result != null) {
			return "redirect:list";
		} else {
			initData(map);
			return "editstudent";
		}
	}
	
	@RequestMapping(value="/student/{stuNo}",method=RequestMethod.DELETE)
	public String deleteStudent(@PathVariable("stuNo") Integer stuNo){
		studentService.delete(stuNo);
		return "redirect:/list";
	}
}

JSP页面

list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!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>
<script type="text/javascript" src="scripts/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
	function deleteStudent(stuNo){
		if(confirm("是否删除?")){
			$("#deleteForm").attr("action","student/" + stuNo);
			$("#deleteForm")[0].submit();
		}
	}
</script>
</head>
<body>
	<a href="student">添加</a>
	<table border="1" width="800px">
		<tr>
			<th>编号</th>
			<th>班级</th>
			<th>姓名</th>
			<th>性别</th>
			<th>年龄</th>
			<th>邮箱</th>
			<th>出生日期</th>
			<th>操作</th>
		</tr>
		<c:forEach items="${pagelist.content}" var="s">
			<tr>
				<td>${s.stuNo}</td>
				<td>${s.clazz.clsName }</td>
				<td>${s.stuName }</td>
				<td>${s.sex }</td>
				<td>${s.age }</td>
				<td>${s.email }</td>
				<td>${s.birth }</td>
				<td>
					<a href="student/${ s.stuNo}">编辑</a>
					<a href="javascript:deleteStudent(${s.stuNo})">删除</a>
				</td>
			</tr>
		</c:forEach>
		<tr>
			<td colspan="8" align="center">
			<c:if test="${pagelist.number > 0 }">
				<a href="list?pageindex=1">首页</a>
				<a href="list?pageindex=${ pagelist.number }">上一页</a>
			</c:if>
			<c:if test="${pagelist.number < pagelist.totalPages - 1 }">
				<a href="list?pageindex=${ pagelist.number+2 }">下一页</a>
				<a href="list?pageindex=${pagelist.totalPages}">末页</a>
			</c:if>
				${ pagelist.number+1 }/${pagelist.totalPages}
				共${pagelist.totalElements }条数据,每页显示${pagelist.size}条
			</td>
		</tr>
	</table>

	<form:form id="deleteForm" method="delete"></form:form>
</body>
</html>

addstudent.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!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>
<body>
	<form:form action="student" metdod="post" commandName="student">
		<table>
			<tr>
				<td>姓名</td>
				<td>
					<form:input path="stuName"/>
				</td>
			</tr>
			<tr>
				<td>密码</td>
				<td>
					<form:password path="stuPass"/>
				</td>
			</tr>
			<tr>
				<td>确认密码</td>
				<td>
					<form:password path="realPass"/>
				</td>
			</tr>
			<tr>
				<td>班级</td>
				<td>
					<form:select path="clazz.clsId" items="${clazzlist}" 
						itemValue="clsId" itemLabel="clsName" />
				</td>
			</tr>
			<tr>
				<td>性别</td>
				<td>
					<form:select path="sex" items="${sexlist}" />
				</td>
			</tr>
			<tr>
				<td>邮箱</td>
				<td>
					<form:input path="email"/>
				</td>
			</tr>
			<tr>
				<td>年龄</td>
				<td>
					<form:input path="age"/>
				</td>
			</tr>
			<tr>
				<td>生日</td>
				<td>
					<form:input path="birth"/>
				</td>
			</tr>
			<tr>
				<td></td>
				<td>
					<input type="submit" value="添加" />
					<input type="reset" value="重置">
				</td>
			</tr>
		</table>
	</form:form>
</body>
</html>

editstudent.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!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>
<body>
	<form:form action="../student" method="put" commandName="student">
		<form:hidden path="stuNo"/>
		<table>
			<tr>
				<td>姓名</td>
				<td>
					<form:input path="stuName"/>
				</td>
			</tr>
			<tr>
				<td>班级</td>
				<td>
					<form:select path="clazz.clsId" items="${clazzlist}" 
						itemValue="clsId" itemLabel="clsName" />
				</td>
			</tr>
			<tr>
				<td>性别</td>
				<td>
					<form:select path="sex" items="${sexlist}" />
				</td>
			</tr>
			<tr>
				<td>邮箱</td>
				<td>
					<form:input path="email"/>
				</td>
			</tr>
			<tr>
				<td>年龄</td>
				<td>
					<form:input path="age"/>
				</td>
			</tr>
			<tr>
				<td>生日</td>
				<td>
					<form:input path="birth"/>
				</td>
			</tr>
			<tr>
				<td></td>
				<td>
					<input type="submit" value="修改" />
					<input type="reset" value="重置">
				</td>
			</tr>
		</table>
	</form:form>
</body>
</html>

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>入口</title>
</head>
<body>
	<a href="list">list</a>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/peter824/article/details/82989941