一、实验目的
通过编写Spring MVC框架程序,并结合Spring依赖注入和MyBatis技术实现简单Web应用,深入了解多层体系结构Web应用软件的构造方法,掌握SSM三大框架技术的整合方式。
二、实验内容
设计一个简单的学生信息管理系统,要求使用SSM框架技术整合实现,用户登录后能够通过Web页面添加、删除、修改和查询学生信息。数据库的名称为“db_你的名字首字母组合”(如“db_gbl”),建表的SQL语句为:
CREATE TABLE students (
Id int(11) NOT NULL AUTO_INCREMENT,
name varchar(20) NOT NULL,
age int(11) NOT NULL,
gender varchar(255) DEFAULT NULL,
number varchar(20) DEFAULT NULL,
address varchar(20) DEFAULT NULL,
status int(11) NOT NULL DEFAULT 1,
PRIMARY KEY (id) )
请自己编写相关的类、接口、配置文件和用户访问页面,要求在包路径名称中包含你的名字首字母,如“com.gbl.service”。要求使用Spring配置文件来管理数据连接和事务,使用拦截器来处理用户登录。
三、实验要求
参照课本及参考程序,快速熟悉相关内容,编写基于SSM技术的应用相关代码,掌握SSM三大框架的整合方法,具备简单Web应用的设计和开发能力。
四、实验环境
软件环境:Windows 11,MySQL5.5,Eclipse和MyEclipse;
硬件环境:酷睿i5 3.4G/1T/4G及更高配置的PC机。
五、实验步骤
1安装和配置Eclipse或MyEclipse集成开发环境
2导入相关的Jar包
3通过查阅课本、网络资料,了解相关知识
4对系统进行设计,规划出相应的包、接口、类和配置文件等软件开发元素
1)数据库常量配置文件db.properties4
2)Mabatis配置文件mybatis-config.xml
3)事物传播行为及切面配置applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="maxTotal" value="${jdbc.maxTotal}"></property>
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<property name="initialSize" value="${jdbc.initialSize}"></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="create*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config >
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.wuxt.core.service.*.*(..))"/>
</aop:config>
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wuxt.core.dao"></property>
</bean>
<context:component-scan base-package="com.wuxt.core.service"></context:component-scan>
</beans>
4)配置需要扫描的包,注解驱动、视图解析器配置springmvc-config.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:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="com.wuxt.core"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
<mvc:resources location="/fonts/" mapping="/fonts/**"></mvc:resources>
<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.wuxt.core.interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
5)配置监听器,编码过滤器、前端控制器web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>TEST3</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>crm</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>crm</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<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>
</web-app>
6)在com.wuxt.core.interceptor包中创建登录拦截器类LoginInterceptor
package com.wuxt.core.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.wuxt.core.po.User;
public class LoginInterceptor implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handler)throws Exception{
String url=request.getRequestURI();
if(url.indexOf("/login.action")>=0) {
return true;
}
HttpSession session =request.getSession();
User user=(User)session.getAttribute("user");
if(user!=null)return true;
request.setAttribute("msg", " 请先登录!谢谢合作!");
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
return false;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
throws Exception {
// TODO Auto-generated method stub
}
}
7)在com.wuxt.core.po包中创建用户持久化类User
package com.wuxt.core.po;
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
8)在com.wuxt.core.po包中创建用户持久化类Student
package com.wuxt.core.po;
public class Student {
private Integer id;
private String name;
private Integer age;
private String gender;
private String number;
private String address;
private Integer status;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String toString() {
return "Student [id="+id+",name="+name+",age="+age+",gender="+gender+",number="+number+",address="+address+",status="+status+"]";
}
}
9)在com.wuxt.core.dao包中创建学生接口StudentDao
package com.wuxt.core.dao;
import java.util.List;
import com.wuxt.core.po.Student;
public interface StudentDao {
public List<Student> selectStudentList(Student student);
public int addStudent(Student student);
public int updateStudent(Student student);
public int deleteStudent(Integer id);
public Student getStudent(Integer id);
}
10)在com.wuxt.core.dao包中创建映射文件StudentDao.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org//dtd//mybatis-3-mapper.dtd">
<mapper namespace="com.wuxt.core.dao.StudentDao">
<select id="getStudent" parameterType="Integer" resultType="Student">
select *from students where id=#{id}
</select>
<select id="findStudentByName" parameterType="String" resultType="Student">
select *from students where name like '%${value}%'
</select>
<select id="selectStudentList" parameterType="Student" resultType="Student">
select * from students where 1=1
<if test="id!=null">
and id =#{id}
</if>
<if test="name!=null and name!=''">
and name like concat('%',#{name},'%')
</if>
<if test="age!=null">
and age=#{age}
</if>
<if test="gender!=null and gender!=''">
and gender=#{gender}
</if>
<if test="address!=null and address!=''">
and address=#{address}
</if>
</select>
<select id="findStudentByNameAndAddress" parameterType="Student" resultType="Student">
select *from students where 1=1
<if test="name!=null and name!=''">
and name like concat('%',#{name},'%')
</if>
<if test="address!=null and address!=''">
and address=#{address}
</if>
</select>
<insert id="addStudent" parameterType="Student" keyProperty="id" useGeneratedKeys="true">
insert into students(name,age,gender,number,address,status) values(#{name},#{age},#{gender},#{number},#{address},#{status})
</insert>
<update id="updateStudent" parameterType="Student">
update students
<set>
<if test="name!=null and name!=''">
name=#{name},
</if>
<if test="age!=null">
age=#{age},
</if>
<if test="gender!=null and gender!=''">
gender=#{gender},
</if>
<if test="number!=null and number!=''">
number=#{number},
</if>
<if test="address!=null and address!=''">
address=#{address},
</if>
<if test="status!=null">
status=#{status},
</if>
</set>
where id=#{id}
</update>
<delete id="deleteStudent" parameterType="Integer">
delete from students where id=#{id}
</delete>
</mapper>
11)在com.wuxt.core.service包中创建学生Service接口StudentService
package com.wuxt.core.service;
import java.util.List;
import com.wuxt.core.po.Student;
public interface StudentService {
public List<Student> selectStudentList(Student student);
public int addStudent(Student student);
public int updateStudent(Student student);
public int deleteStudent(Integer id);
public Student getStudent(Integer id);
}
12)在com.wuxt.core.service包中创建学生Service接口的实现类StudentServiceImpl
package com.wuxt.core.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.wuxt.core.dao.StudentDao;
import com.wuxt.core.po.Student;
import com.wuxt.core.service.StudentService;
@Service
@Transactional
public class StudentServiceImpl implements StudentService{
@Autowired
private StudentDao studentDao;
public List<Student> selectStudentList(Student student){
return this.studentDao.selectStudentList(student);
}
public int addStudent(Student student) {
return this.studentDao.addStudent(student);
}
public int updateStudent(Student student) {
return this.studentDao.updateStudent(student);
}
public int deleteStudent(Integer id) {
return this.studentDao.deleteStudent(id);
}
public Student getStudent(Integer id) {
return this.studentDao.getStudent(id);
}
}
13)在com.wuxt.core.web.controller包中创建用户控制器类UserController
package com.wuxt.core.web.controller;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.wuxt.core.po.User;
@Controller
public class UserController {
@RequestMapping(value="/login.action",method=RequestMethod.POST)
public String login(User user,Model model,HttpSession session) {
System.out.println(user.getUsername());
System.out.println(user.getPassword());
if(user.getUsername().equals("login" )&& user.getPassword().equals("login")) {
session.setAttribute("user", user);
return "student";
}
else {
model.addAttribute("msg", "账号或密码错误!!!please重新输入!");
return "login";
}
}
@RequestMapping(value="/toStudent.action")
public String toStudent() {
return "student";
}
@RequestMapping(value="/logout.action")
public String logout(HttpSession session,Model model) {
session.invalidate();
model.addAttribute("msg", "成功退出welcome next欢迎下次再来 ");
return "login";
}
}
14)在com.wuxt.core.web.controller包中创建学生控制器类StudentController
package com.wuxt.core.web.controller;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
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 com.wuxt.core.po.Student;
import com.wuxt.core.service.StudentService;
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/List.action")
public String list(Integer id,String name,Integer age,String gender,String address,Model model,HttpServletRequest request) {
Student student=new Student();
student.setId(id);
if(name==null)name="";
student.setName(name);
student.setAge(age);
if(gender==null)gender="";
student.setGender(gender);
if(address==null)address="";
student.setAddress(address);
System.out.println(student);
List<Student> students=new ArrayList<Student>();
students=(List<Student>) studentService.selectStudentList(student);
Integer rows=0;
for(Student student1:students) {
System.out.println(student1);
rows++;
}
if(rows==0) {model.addAttribute("msg", "可惜,此学生不存在!!!");return "student";}
model.addAttribute("students", students);
return "findstudent";
}
@RequestMapping("/tocreate.action")
public String tocreate() {return "createstudent";}
@RequestMapping("/tostudent.action")
public String tostudent() {return "student";}
@RequestMapping("/create.action")
public String create(String name,Integer age,String gender,String number,String address,Integer status,Model model) {
Student student=new Student();
student.setName(name);
student.setAge(age);
if(gender==null)gender="";
student.setGender(gender);
student.setNumber(number);
if(address==null)address="";
student.setAddress(address);
if(status==null)status=1;
student.setStatus(status);
int rows=studentService.addStudent(student);
if(rows>0) {
model.addAttribute("msg","(>ω<)执行添加操作成功!!!");
}else {
model.addAttribute("msg"," T_T 执行添加操作失败!!!");
}
return "student";
}
@RequestMapping("/toupdate.action")
public String toupdate(Integer id,Model model) {
Student student=studentService.getStudent(id);
model.addAttribute("student", student);
return "updatestudent";}
@RequestMapping("/update.action")
public String update(Integer id,String name,Integer age,String gender,String number,String address,Integer status,Model model) {
Student student=new Student();
student.setId(id);
if(name==null)name="";
student.setName(name);
student.setAge(age);
if(gender==null)gender="";
student.setGender(gender);
if(number==null)number="";
student.setNumber(number);
if(address==null)address="";
student.setAddress(address);
student.setStatus(status);
System.out.println(student.toString());
int rows=studentService.updateStudent(student);
if(rows>0) {
model.addAttribute("msg","(>ω<)执行修改操作成功!!!");
}else {
model.addAttribute("msg"," T_T 执行修改操作失败!!!"); }
return "student";
}
@RequestMapping("/todelete.action")
public String delete(Integer id,Model model) {
int rows=studentService.deleteStudent(id);
if(rows>0) {
model.addAttribute("msg","(>ω<)执行删除操作成功!!!");
}else {
model.addAttribute("msg"," T_T 执行删除操作失败!!!") }
return "student";
}
}
15)实现页面功能
a) 默认页面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">
<jsp:forward page="/WEB-INF/jsp/login.jsp"></jsp:forward>
b) 登陆页面login.jsp
<meta http-equiv="Content-Type" charset="text/html;charset=UTF-8">
<title>登录页面</title>
<body>
<img src="C:\Users\Administrator\Desktop\xq.jpg">
<script src="js/jquery-1.7.2.min.js" type="text/javascript"></script>
<div class="logo_box">
<h3>欢迎你</h3>
<form name="form1" action="${pageContext.request.contextPath }/login.action" method="post" onsubmit="return CHECK()">
<div class="input_outer">
<span class="u_user"></span>
<input name="logname" class="text" onFocus=" if(this.value=='输入ID或用户名登录') this.value=''" onBlur="if(this.value=='') this.value='输入ID或用户名登录'" value="输入ID或用户名登录" style="color:black !important" type="text">
</div>
<div class="input_outer">
<span class="us_uer"></span>
<label class="l-login login_password" style="color: rgb(255, 255, 255);display: block;">输入密码</label>
<input name="logpass" class="text" style="color:black !important; position:absolute; z-index:100;" onFocus="$('.login_password').hide()" onBlur="if(this.value=='') $('.login_password').show()" value="" type="password">
</div>
<div class="mb2"><a class="act-but submit" href="javascript:;" style="color: black">登录</a></div>
<input name="savesid" value="0" id="check-box" class="checkbox" type="checkbox"><span>记住用户名</span>
</form>
<a href="#" class="login-fgetpwd" style="color: #FFFFFF">忘记密码?</a>
<div class="sas">
<a href="#">还未注册账号,请注册后登陆!</a>
</div>
<div class="sas">
<a href="#" style="color: red;">学生信息管理系统</a>
<a href="#" style="color: red;">作者——武鑫婷</a>
</div>
</div>
</body>
c) 主页面student.jsp
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>学生管理</title>
</head>
<body>
<h1 align="center">学生管理</h1>
<br/><br/><br/><br/>
<font color="red"><span id="message">${msg}</span></font>
<form name="form1" method="post" action="${pageContext.request.contextPath }/List.action">
<table border=1 align="center">
<tr>
<th>编号</th><th>姓名</th><th>年龄</th><th>性别</th><th>地址</th><th>操作</th>
</tr>
<tr>
<td><input type="text" name="id"/></td>
<td><input type="text" name="name"/></td>
<td><input type="text" name="age"/></td>
<td><input type="text" name="gender"/></td>
<td><input type="text" name="address"/></td>
<td><input type="submit" value="查询">
</tr>
</table>
</form>
<br/><br/><br/><br/>
<form name="form2" method="post" action="${pageContext.request.contextPath }/tocreate.action">
<input type="submit" value="新建">
<a href="${pageContext.request.contextPath }/logout.action">退出</a>
</form>
</body>
d) 查询信息页面findstudent.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>
<h1 align="center">学生信息</h1>
<br/><br/><br/><br/>
<form action="${pageContext.request.contextPath }/tostudent.action" >
<table border=1 align="center">
<tr>
<th>编号</th><th>姓名</th><th>年龄</th><th>性别</th><th>联系方式</th><th>地址</th><th>状态</th><th>操作</th>
</tr>
<c:forEach var="student" items="${students}" >
<tr>
<td>${student.id }</td>
<td>${student.name }</td>
<td>${student.age }</td>
<td>${student.gender }</td>
<td>${student.number }</td>
<td>${student.address }</td>
<td>${student.status }</td>
<td>
<a href="${pageContext.request.contextPath }/toupdate.action?id=${student.id} ">修改</a>
<a href="${pageContext.request.contextPath }/todelete.action?id=${student.id }">删除</a>
</td>
</tr>
</c:forEach>
<tr align="center" ><td ><input type="submit" value="返回"/></td></tr>
</table>
</form>
</body>
</html>
e) 添加学生页面createstudent.jsp
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加学生 </title>
<script type="text/javascript">
function CHECK(){
if(form1.name.value==null||form1.name.value==''){
document.getElementById("message").innerText="(~_~;) 姓名要填哦!";
form1.name.focus();
return false;
}
if(form1.age.value==null||form1.age.value==''){
document.getElementById("message").innerText="(~_~;) 年龄要填哦!";
form1.age.focus();
return false;
}
if(form1.number.value==null||form1.number.value==''){
document.getElementById("message").innerText="(~_~;) 联系方式要填哦!";
form1.number.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<font color="red"><span id="message">${msg}</span></font>
<h3 align="center">添加学生信息</h3>
<a href="${pageContext.request.contextPath }/tostudent.action">关闭</a>
<form name="form1" method="post" action="${pageContext.request.contextPath }/create.action" onsubmit="return CHECK()">
<table border=1 align="center">
<tr></tr>
<tr><th>姓名</th><td><input type="text" name="name" placeholder="必填*"/></td></tr>
<tr><th>年龄</th><td><input type="text" name="age" placeholder="必填*"/></td></tr>
<tr><th>性别</th><td><input type="text" name="gender"/></td></tr>
<tr><th>联系方式</th><td><input type="text" name="number" placeholder="必填*"/></td></tr>
<tr><th>地址</th><td><input type="text" name="address"/></td></tr>
<tr><th>状态</th><td><input type="text" name="status"/></td></tr>
<tr><td><input type="submit" value="确认"><td align="center"><input type="reset" value="重写"></tr>
</table>
</form>
</body>
f) 修改信息页面updatestudent.jsp
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>修改学生信息</title>
<script type="text/javascript">
function CHECK(){
if(form1.name.value==''&&form1.age.value==''&&form1.gender.value==''&&form1.number.value==''&&form1.address.value==''&&form1.status.value==''){
document.getElementById("message").innerText="(~_~;) 您没有对数据进行修改,可以点击“关闭”退出本页面!";
return false;
}
return true;
}
</script>
</head>
<body>
<font color="red"><span id="message">${msg}</span></font>
<h3 align="center">修改学生信息</h3>
<a href="${pageContext.request.contextPath }/tostudent.action" >关闭</a>
<form name="form1" method="post" action="${pageContext.request.contextPath }/update.action?id=${student.id}" onsubmit="return CHECK()">
<table border=1 align="center">
<tr><th>编号</th><td><input type="hidden" name="id">${student.id}</td></tr>
<tr><th>姓名</th><td><input type="text" placeholder="${student.name}" name="name"/></td></tr>
<tr><th>年龄</th><td><input type="text" placeholder="${student.age}" name="age"/></td></tr>
<tr><th>性别</th><td><input type="text" placeholder="${student.gender}" name="gender"/></td></tr>
<tr><th>联系方式</th><td><input type="text" placeholder="${student.number}" name="number"/></td></tr>
<tr><th>地址</th><td><input type="text" placeholder="${student.address}" name="address"/></td></tr>
<tr><th>状态</th><td><input type="text" placeholder="${student.status}" name="status"/></td></tr>
<tr><td><input type="submit" value="修改"><td align="center"><input type="reset" value="重写"></tr>
</table>
</form>
</body>
- 调试程序,输出结果,并进行记录,分析处理结果
结果如下:
图3-1登录界面
图3-2新建信息界面
图3-4学生信息操作结果
图3-5学生登录及学生信息数据库结构
图3-6学生信息列表
图3-7代码操作界面
本文来自博客园,作者:一路向北~~,转载请注明原文链接:https://www.cnblogs.com/ylxb2539989915/p/16329415.html