Spring MVC实现C语言标准化考试系统
Spring MVC实现C语言标准化考试系统
📝 前言
在Web开发学习中,实现一个在线考试系统是非常经典的实战项目。本文将带你使用Spring MVC框架,从零开始构建一个完整的C语言标准化考试系统,涵盖前端页面设计、后端控制器开发、答案判分等核心功能。
🎯 项目概述
本系统实现了一个C语言基础知识单选题考试系统,具备以下特点:
- 美观的UI界面:渐变流动背景、悬浮阴影效果
- 完整的考试流程:试卷展示 → 用户答题 → 自动判分 → 结果显示
- MVC架构设计:清晰的代码分层,符合企业开发规范
🛠️ 技术栈
| 技术 | 说明 |
|---|---|
| Spring MVC | 控制层框架,处理请求分发 |
| JSP | 视图层,展示页面 |
| Servlet | 底层请求处理 |
| CSS3 | 页面样式与动画效果 |
📁 项目结构
springmvc-exam/
├── src/
│ └── com/jmu/controller/
│ └── ExamController.java # 考试控制器
├── webapp/
│ ├── view/
│ │ ├── test.jsp # 考试页面
│ │ └── score.jsp # 成绩页面
│ ├── css/
│ │ └── style.css # 样式文件
│ └── WEB-INF/
│ ├── web.xml # Web部署描述文件
│ └── myspringmvc-servlet.xml # Spring MVC配置文件
🎨 前端页面设计
1. CSS样式设计 —— 让页面更美观
完整样式代码 (style.css)
/* 全局样式重置:清除所有元素默认的内外边距,统一盒模型和字体 */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Microsoft YaHei", sans-serif;
}
/* 设置页面高度和渐变背景,实现动态流动效果 */
html, body {
height: 100%;
background: linear-gradient(135deg, #74c0f8, #5b3cb8);
background-size: 400% 400%;
animation: gradientFlow 14s ease-in-out infinite;
}
/* 定义背景渐变流动动画:让背景位置缓慢移动 */
@keyframes gradientFlow {
0% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
100% { background-position: 0% 50%; }
}
/* 页面主体布局:居中显示、内容过长可滚动 */
body {
display: flex;
justify-content: center;
align-items: flex-start;
overflow-y: auto;
padding: 30px 0;
}
/* 考试表单外层容器样式 */
.exam-box {
width: 700px;
padding: 45px;
background: rgba(255, 255, 255, 0.85);
border-radius: 20px;
border: 1px solid rgba(120, 150, 255, 0.3);
box-shadow: 0 0 30px rgba(90, 110, 220, 0.25), inset 0 0 15px rgba(90, 110, 220, 0.1);
z-index: 10;
transition: all 0.4s ease;
margin: auto 0;
}
/* 鼠标悬浮在考试容器上的效果:上浮 + 阴影加深 */
.exam-box:hover {
transform: translateY(-6px);
box-shadow: 0 0 40px rgba(90, 110, 220, 0.35), inset 0 0 20px rgba(90, 110, 220, 0.15);
}
/* 考试标题样式 */
.exam-box h2 {
text-align: center;
margin-bottom: 35px;
color: #4a3296;
font-weight: 600;
letter-spacing: 3px;
}
/* 题目模块样式 */
.question {
margin-bottom: 22px;
font-size: 16px;
color: #3f2e7c;
}
/* 题目题干样式 */
.question p {
margin-bottom: 10px;
font-weight: 500;
}
/* 选项标签样式 */
.question label {
display: block;
padding: 8px 5px;
color: #554096;
cursor: pointer;
}
/* 提交按钮样式 */
.btn {
width: 100%;
height: 50px;
background: linear-gradient(135deg, #6fb6f5, #6744c9);
color: #ffffff;
border: none;
border-radius: 10px;
font-size: 17px;
cursor: pointer;
letter-spacing: 2px;
transition: all 0.3s ease;
margin-top: 20px;
}
/* 按钮鼠标悬浮效果 */
.btn:hover {
background: linear-gradient(135deg, #58a3e8, #5332b8);
transform: scale(1.02);
box-shadow: 0 0 15px rgba(100,75,200,0.4);
}
/* 按钮点击按下效果 */
.btn:active {
transform: scale(0.98);
}
/* 分数结果展示框样式 */
.score-box {
width: 500px;
padding: 60px 45px;
background: rgba(255, 255, 255, 0.9);
border-radius: 20px;
text-align: center;
font-size: 24px;
color: #4a3296;
font-weight: 600;
box-shadow: 0 0 40px rgba(90, 110, 220, 0.3);
margin: auto;
}
💡 设计亮点:通过
background-size放大背景,配合keyframes动画实现色彩流动效果,让页面更有活力。
2. 考试页面 (test.jsp)
考试页面包含5道C语言单选题,使用表单收集用户答案:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<link rel="stylesheet" href="css/style.css">
<html>
<head>
<meta charset="UTF-8">
<title>C语言标准化考试</title>
</head>
<body>
<div class="exam-box">
<h2>C语言标准化测试(单选题)</h2>
<form method="post" action="exam.do">
<div class="question">
<p>1. C语言程序是由___构成的。</p>
<label><input type="radio" name="q1" value="A"> A. 一些可执行语言</label>
<label><input type="radio" name="q1" value="B"> B. main函数</label>
<label><input type="radio" name="q1" value="C"> C. 函数</label>
<label><input type="radio" name="q1" value="D"> D. 包含文件中的第一个函数</label>
</div>
<div class="question">
<p>2. ___是构成C语言程序的基本单位。</p>
<label><input type="radio" name="q2" value="A"> A. 函数</label>
<label><input type="radio" name="q2" value="B"> B. 过程</label>
<label><input type="radio" name="q2" value="C"> C. 子程序</label>
<label><input type="radio" name="q2" value="D"> D. 子例程</label>
</div>
<div class="question">
<p>3. C语言可执行程序从___开始执行。</p>
<label><input type="radio" name="q3" value="A"> A. 第一条可执行语句</label>
<label><input type="radio" name="q3" value="B"> B. 第一个函数</label>
<label><input type="radio" name="q3" value="C"> C. main函数</label>
<label><input type="radio" name="q3" value="D"> D. 包含文件中的第一个函数</label>
</div>
<div class="question">
<p>4. C语言程序从main()开始执行,所以main要写在___。</p>
<label><input type="radio" name="q4" value="A"> A. 程序文件的开始</label>
<label><input type="radio" name="q4" value="B"> B. 程序文件的最后</label>
<label><input type="radio" name="q4" value="C"> C. 它所调用的函数前面</label>
<label><input type="radio" name="q4" value="D"> D. 程序文件的任何位置</label>
</div>
<div class="question">
<p>5. 以下说法正确的是___。</p>
<label><input type="radio" name="q5" value="A"> A. 从第一个定义的函数开始执行</label>
<label><input type="radio" name="q5" value="B"> B. 调用的函数必须在main中定义</label>
<label><input type="radio" name="q5" value="C"> C. 总是从main()开始执行</label>
<label><input type="radio" name="q5" value="D"> D. main()必须放在程序开始</label>
</div>
<input type="submit" class="btn" value="提交试卷">
</form>
</div>
</body>
</html>
📋 考试题目(5道单选题)
| 题号 | 题目 | 正确答案 |
|---|---|---|
| 1 | C语言程序是由___构成的 | C |
| 2 | ___是构成C语言程序的基本单位 | A |
| 3 | C语言可执行程序从___开始执行 | C |
| 4 | C语言程序从main()开始执行,所以main要写在___ | D |
| 5 | 以下说法正确的是___ | C |
3. 成绩页面 (score.jsp)
简洁的成绩展示页面,通过EL表达式接收后端传递的分数:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" isELIgnored="false"%>
<link rel="stylesheet" href="css/style.css">
<html>
<head>
<title>考试成绩</title>
</head>
<body>
<div class="score-box">
你的考试成绩:${score} 分
</div>
</body>
</html>
⚙️ Spring MVC配置
1. 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>springmvc001</display-name>
<welcome-file-list>
<welcome-file>test.jsp</welcome-file>
</welcome-file-list>
<!-- 配置DispatcherServlet,作为Spring MVC的前端控制器 -->
<servlet>
<servlet-name>myspringmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myspringmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
2. Spring MVC配置文件 (myspringmvc-servlet.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:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:util="http://www.springframework.org/schema/util"
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.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- HandlerMapping:请求映射,相当于导诊台 -->
<bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"></bean>
<!-- Controller:业务控制器,相当于各个科室 -->
<bean name="/exam.do" class="com.jmu.controller.ExamController"/>
<!-- ViewResolver:视图解析器,配置前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
📌 配置说明:
prefix:视图文件存放路径前缀suffix:视图文件后缀名- 这样返回
"score"时会自动映射到/view/score.jsp
🔧 核心控制器实现
ExamController.java —— 考试判分控制器
package com.jmu.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
/**
* 考试控制器:接收前端提交的考试答案,自动判分并返回结果
* 继承SpringMVC的AbstractController,实现请求处理
*/
public class ExamController extends AbstractController {
/**
* 核心请求处理方法(SpringMVC自动调用)
* @param request 请求对象,用于获取前端提交的答案参数
* @param response 响应对象
* @return ModelAndView 模型与视图对象,携带分数跳转到结果页面
*/
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// 定义考试标准答案数组,共5道题,每题20分
String[] ans = {"C", "A", "C", "D", "C"};
// 初始化用户得分,默认0分
int score = 0;
// 循环遍历5道题目,逐题判断用户答案是否正确
for (int i = 1; i <= 5; i++) {
// 获取前端提交的答案参数,参数名:q1、q2、q3、q4、q5
String user = request.getParameter("q" + i);
// 判断:用户答案不为空 且 与标准答案一致,则加20分
if (user != null && user.equals(ans[i - 1])) {
score += 20;
}
}
// 创建ModelAndView对象,指定跳转的视图名称为 score(对应score.jsp页面)
ModelAndView mav = new ModelAndView("score");
// 将最终得分存入模型,前端页面可通过${score}获取分数
mav.addObject("score", score);
// 返回ModelAndView,SpringMVC会自动跳转到结果页面并展示分数
return mav;
}
}
代码核心逻辑解析
| 步骤 | 说明 |
|---|---|
| ① | 定义标准答案数组 {"C", "A", "C", "D", "C"} |
| ② | 循环遍历5道题目,获取用户提交的答案 |
| ③ | 比对用户答案与标准答案,正确则加20分 |
| ④ | 将分数存入Model,返回视图名称"score" |
🚀 运行效果展示
1. 考试页面
页面采用半透明白色卡片设计,5道单选题清晰排列,提交按钮采用渐变风格,鼠标悬浮有上浮效果。

2. 提交试卷
提交后自动跳转到成绩页面,清晰展示最终得分。

3. 成绩展示
成绩页面简洁大方,大号字体突出显示分数。

📊 Spring MVC请求处理流程
Spring MVC架构图

上图展示了Spring MVC的核心组件协作流程:
- 用户请求被
DispatcherServlet拦截 HandlerMapping根据配置找到对应的ControllerController执行业务逻辑,返回ModelAndViewViewResolver解析视图名称,定位到具体的 JSP 页面View渲染数据,返回响应给客户端
💡 技术要点总结
1. Spring MVC核心组件
| 组件 | 作用 | 本项目中对应 |
|---|---|---|
| DispatcherServlet | 前端控制器,统一请求入口 | web.xml中配置 |
| HandlerMapping | 请求映射,找到对应Controller | ControllerBeanNameHandlerMapping |
| Controller | 处理业务逻辑 | ExamController |
| ModelAndView | 封装数据和视图 | 返回结果页面 |
| ViewResolver | 解析视图路径 | InternalResourceViewResolver |
2. CSS设计亮点
- 渐变流动背景:
linear-gradient+keyframes动画 - 玻璃态卡片:
rgba半透明背景实现毛玻璃效果 - 悬浮交互:
transform: translateY()实现上浮效果 - 按压反馈:
:active伪类实现按钮按压缩放
📚 扩展思考
本系统作为基础版本,还可以进行以下扩展:
- ✨ 增加题库管理:从数据库读取题目和答案
- ✨ 增加用户登录:记录每个用户的考试成绩
- ✨ 增加计时功能:限制考试时间
- ✨ 增加错题解析:提供答案解析
- ✨ 增加成绩排行榜:展示高分用户
- ✨ 增加多种题型:判断题、多选题等
🎯 总结
通过本项目,我们完整实践了Spring MVC框架的以下核心能力:
✅ 掌握DispatcherServlet前端控制器配置
✅ 理解HandlerMapping请求映射机制
✅ 学会编写AbstractController子类处理业务
✅ 掌握ModelAndView数据传递与视图跳转
✅ 实践JSP+CSS3构建美观前端页面
这是一个非常适合Spring MVC入门的实战项目,代码简洁但涵盖了框架的核心要点,希望对你有所帮助!

浙公网安备 33010602011771号