springMVC REST风格 以及 利用ajax返回JSON数据 中文乱码 返回方式 SSM框架示例

springMVC

属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的SpringMVC框架或集成其他MVC开发框架,如Struts1(现在一般不用),Struts2(一般老项目使用)等。

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>

<name>SSM-school</name>
<groupId>com</groupId>
<artifactId>SSM-school</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<!--spring AOP就是用aspectj来实现的,是依赖关系-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.8</version>
</dependency>
<!-- spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<!--事务-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<!--初始化会话工厂-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<!--mybatis框架-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--mybatis与spring对接的jar包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!--mysql 数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<!--数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.2</version>
</dependency>
<!--JSP 标准标签库 (JSTL)-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--json转换工具包-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
</dependencies>
</project>

数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
DROP DATABASE IF EXISTS `school`;

CREATE DATABASE `school`;

USE `school`;

CREATE TABLE `classes`(
`id` INT PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(50)
)CHARSET=utf8;
CREATE TABLE `student`(
`id` INT PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
`age` INT NOT NULL,
`gender` VARCHAR(2) NOT NULL,
`telephone` VARCHAR(20) NOT NULL,
`email` VARCHAR(50) NOT NULL,
`classId` INT
)CHARSET=utf8;

INSERT INTO classes VALUES(DEFAULT,'one'),(DEFAULT,'two'),(DEFAULT,'three');
INSERT INTO student VALUES(DEFAULT,'张三',14,'男','10086','admin@icloud.com',1);
INSERT INTO student VALUES(DEFAULT,'李四',15,'女','10086','admin@icloud.com',2);
INSERT INTO student VALUES(DEFAULT,'王五',16,'男','10086','admin@icloud.com',3);

SELECT * FROM `classes`;
SELECT s.*,c.`id` 'classes.id',c.`name` 'classes.name' FROM `student` s,`classes` c WHERE s.`classId`=c.`id`;

database.properties 数据库配置文件

1
2
3
4
db_url=jdbc:mysql://localhost:3306/school
db_driver=com.mysql.jdbc.Driver
db_username=root
db_password=simon

实体类 pojo

Student 学生类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.pojo;

public class Student {

private Integer id;
private String name;
private Integer age;
private String gender;
private String telephone;
private String email;
private Classes classes;

//省略getter和setter
}

Classes 年级类

1
2
3
4
5
6
7
8
9
package com.pojo;

public class Classes {

private Integer id;
private String name;

//省略getter和setter
}

DAO层

StudentDao DAO层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.dao;

import com.pojo.Student;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface StudentDao {
List<Student> getStudents();

Student getStudent(@Param("id") Integer id);

Integer addStudent(Student student);
}

ClassesDao DAO层

1
2
3
4
5
6
7
8
9
package com.dao;

import com.pojo.Classes;

import java.util.List;

public interface ClassesDao {
List<Classes> getClasses();
}

service层

ClassesBiz

1
2
3
4
5
6
7
8
9
package com.biz;

import com.pojo.Classes;

import java.util.List;

public interface ClassesBiz {
List<Classes> getClasses();
}

StudentBiz

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.biz;

import com.pojo.Student;

import java.util.List;

public interface StudentBiz {
List<Student> getStudents();

Student getStudent(Integer id);

Integer addStudent(Student student);
}

serviceImpl实现层

ClassesBizImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.biz.impl;

import com.biz.ClassesBiz;
import com.dao.ClassesDao;
import com.pojo.Classes;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ClassesBizImpl implements ClassesBiz {
@Resource
private ClassesDao classesDao;

public List<Classes> getClasses() {
return classesDao.getClasses();
}
}

StudentBizImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.biz.impl;

import com.biz.StudentBiz;
import com.dao.StudentDao;
import com.pojo.Student;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StudentBizImpl implements StudentBiz {
@Resource
private StudentDao studentDao;

public List<Student> getStudents() {
return studentDao.getStudents();
}

public Student getStudent(Integer id) {
return studentDao.getStudent(id);
}

public Integer addStudent(Student student) {
return studentDao.addStudent(student);
}
}

servlet层

StudentController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.controller;

import com.alibaba.fastjson.JSON;
import com.biz.ClassesBiz;
import com.biz.StudentBiz;
import com.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@Controller
public class StudentController {
@Resource
private StudentBiz studentBiz;
@Resource
private ClassesBiz classesBiz;

@RequestMapping("/showStudents")
public String showStudents(Model model) {
model.addAttribute("students", studentBiz.getStudents());
return "list";
}

@RequestMapping("/toAdd")
public ModelAndView toAdd() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("classes", classesBiz.getClasses());
modelAndView.setViewName("add");
return modelAndView;
}

@RequestMapping("/addStudent")
public String addStudent(Student student, Model model) {
studentBiz.addStudent(student);
return showStudents(model);
}

/**
* rest风格
* 原地址栏 /view?id=7
* rest风格 /view/7
*/
@RequestMapping("/view/{id}")
public ModelAndView view(@PathVariable("id") Integer id) {
ModelAndView modelAndView = new ModelAndView();
Student student = studentBiz.getStudent(id);
modelAndView.addObject("student", student);
modelAndView.setViewName("view");
return modelAndView;
}

//局部编码格式配置 produces = {"application/json;charset=UTF-8"}
//springMVC.xml JSON全局编码格式已设置
//@ResponseBody 返回JSON注解
@RequestMapping(value = "/stringJson/{id}")
@ResponseBody
public String stringJson(@PathVariable("id") Integer id) {
Student student = studentBiz.getStudent(id);
return JSON.toJSONString(student);
}

//局部编码格式配置 produces = {"application/json;charset=UTF-8"}
//springMVC.xml JSON全局编码格式已设置
//@ResponseBody 返回JSON注解
@RequestMapping(value = "/objectJson/{id}")
@ResponseBody
public Object objectJson(@PathVariable("id") Integer id) {
Student student = studentBiz.getStudent(id);
return student;
}
}

mybatis-config.xml mybatis配置文件

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 打印查询语句 -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!--达到 自动映射行为 : 全部FULL 局部的PARTIAL 禁止自动匹配NONE-->
<setting name="autoMappingBehavior" value="FULL"/>
</settings>
</configuration>

XXXMapper配置文件

ClassesMapper.xml

1
2
3
4
5
6
7
8
9
<?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.dao.ClassesDao">
<select id="getClasses" resultType="Classes">
select * from classes
</select>
</mapper>

StudentMapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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.dao.StudentDao">
<select id="getStudents" resultType="Student">
SELECT s.*,c.`id` 'classes.id',c.`name` 'classes.name'
FROM `student` s,`classes` c
WHERE s.`classId`=c.`id`
</select>
<insert id="addStudent">
INSERT INTO student
VALUES(DEFAULT,#{name},#{age},#{gender},#{telephone},#{email},#{classes.id})
</insert>
<select id="getStudent" resultType="Student">
SELECT s.*,c.`id` 'classes.id',c.`name` 'classes.name'
FROM `student` s,`classes` c
WHERE s.`classId`=c.`id` and s.id = #{id}
</select>
</mapper>

springMVC.xml springMVC配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"
default-autowire="byName">

<!--扫描注解的Bean-->
<context:component-scan base-package="com.controller,com.biz"/>

<!--begin 该事务声明在 application-context.xml 没有任何效果-->
<!--声明式事务-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" name="transactionManager"/>
<!--声明式事务管理-->
<!--已配置 default-autowire="byName" 根据名字自动注入 可不配置属性 transaction-manager="transactionManager"-->
<tx:advice id="interceptor" transaction-manager="transactionManager">
<!--配置拦截属性-->
<tx:attributes>
<!--REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。-->
<!--SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行。-->
<!--MANDATORY:支持当前事务,如果当前没有事务,就抛出异常。-->
<!--REQUIRES_NEW:新建事务,如果当前存在事务,把当前事务挂起。-->
<!--NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。-->
<!--NEVER:以非事务方式执行,如果当前存在事务,则抛出异常。-->
<!--NESTED:支持当前事务,如果当前事务存在,则执行一个嵌套事务,如果当前没有事务,就新建一个事务。-->
<tx:method name="add*"/>
<tx:method name="update*"/>
<tx:method name="del*"/>
<!--配置 get前缀的方法 只读并不支持-->
<tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>
</tx:attributes>
</tx:advice>
<!--配置面向切面-->
<aop:config>
<!--声明一个切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.biz..*.*(..))"/>
<!--定义切面-->
<aop:advisor advice-ref="interceptor" pointcut-ref="pointcut"/>
</aop:config>
<!--end 该事务声明在 application-context.xml 没有任何效果-->

<!--内部资源视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀解析-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀解析-->
<property name="suffix" value=".jsp"/>
</bean>

<!--使 @Controller 注解生效-->
<!--使 @ResponseBody 返回 object 时 利用 alibaba.fastjson 工具 转换-->
<mvc:annotation-driven>
<mvc:message-converters>
<!--解决springmvc返回的json中文问题 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="utf-8"/>
</bean>
<!--解决springMVC返回的json为object 时进行转换-->
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

<!--配置静态文件资源的访问-->
<!--否则 WEB-INF 下的 css 等资源文件访问不到-->
<mvc:resources mapping="/css/**" location="/WEB-INF/css/"/>
</beans>

application-context.xml spring 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName">
<!--default-autowire 配置默认自动注入-->

<!--加载数据库配置文件 database.properties-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:database.properties"/>
</bean>

<!--数据源配置-->
<!--scope 作用域、生存空间 singleton 单例-->
<bean class="com.alibaba.druid.pool.DruidDataSource" name="dataSource" scope="singleton">
<property name="url" value="${db_url}"/>
<property name="driverClassName" value="${db_driver}"/>
<property name="username" value="${db_username}"/>
<property name="password" value="${db_password}"/>
<!--配置最大连接池数量-->
<!--<property name="maxActive" value="20"/>-->
<!--配置获取连接等待超时的时间 单位/毫秒-->
<!--<property name="maxWait" value="60000"/>-->
</bean>

<!--配置会话工厂-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" name="sessionFactory">
<!--已配置 default-autowire="byName" 通过名字自动匹配 可以忽略-->
<property name="dataSource" ref="dataSource"/>
<!--指定类型别名包 XXXMapper.xml 使用-->
<property name="typeAliasesPackage" value="com.pojo"/>
<!--配置映射器位置-->
<property name="mapperLocations" value="classpath:mybatis/mapping/xml/*.xml"/>
<!--指定外部配置文件位置 (控制台显示SQL语句)-->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
</bean>

<!--Mapper扫描器配置-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描所有的dao接口-->
<property name="basePackage" value="com.dao"/>
<!--会话工厂Bean名 默认找键值 sqlSessionFactory 的map -->
<!--所以不用 ref 用value 且会话工厂名字定义为 sessionFactory 因此要多此配置-->
<!--会话工厂配置 name 为 sqlSessionFactory 则不需要配置 sqlSessionFactoryBeanName-->
<property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
</bean>
</beans>

web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<!--加载spring配置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</context-param>

<!--配置监听 (加载spring配置没效果)-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!--配置spring 作用域可以使用 request 和 session 值-->
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

<!--加载springMVC-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置加载的springMVC.xml 默认为 servlet-name 的值 XXX-servlet.xml-->
<!--DispatcherServlet-servlet.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<!--配置springMVC加载顺序-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<!--防止中文乱码-->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

jsp页面

index.jsp

1
2
3
4
5
6
7
8
9
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<meta http-equiv="refresh" content="0; url=showStudents"/>
</body>
</html>

list.jsp 学生列表页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
<link rel="stylesheet" type="text/css" href="/css/style.css">
</head>
<body>
<a href="toAdd">添加学员</a>
<table border="1" width="80%">
<thead>
<tr>
<td>编号</td>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
<td>电话</td>
<td>email</td>
<td>班级</td>
<td>JSON</td>
</tr>
</thead>
<tbody>
<c:forEach items="${students}" var="student">
<tr>
<td>${student.id}</td>
<td><a href="/view/${student.id}">${student.name}</a></td>
<td>${student.gender}</td>
<td>${student.age}</td>
<td>${student.telephone}</td>
<td>${student.email}</td>
<td>${student.classes.name}</td>
<td>
<a href="/stringJson/${student.id}">stringJson</a>
<br>
<a href="/objectJson/${student.id}">objectJson</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>

add.jsp 新增学生页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
<link rel="stylesheet" type="text/css" href="/css/style.css">
</head>
<body>
<form action="addStudent" method="post">
<table border="1" width="50%">
<tr>
<td>姓名</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>性别</td>
<td><input type="text" name="gender"/></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age"/></td>
</tr>
<tr>
<td>电话</td>
<td><input type="text" name="telephone"/></td>
</tr>
<tr>
<td>email</td>
<td><input type="text" name="email"/></td>
</tr>
<tr>
<td>班级</td>
<td>
<select name="classes.id">
<c:forEach items="${classes}" var="clazz">
<option value="${clazz.id}">${clazz.name}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="增加"/>
</td>
</tr>
</table>
</form>
</body>
</html>

view.jsp 学生详情页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<link rel="stylesheet" type="text/css" href="/css/style.css">
</head>
<body>
<table border="1" width="80%">
<thead>
<tr>
<td>编号</td>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
<td>电话</td>
<td>email</td>
<td>班级</td>
</tr>
</thead>
<tbody>
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.gender}</td>
<td>${student.age}</td>
<td>${student.telephone}</td>
<td>${student.email}</td>
<td>${student.classes.name}</td>
</tr>
</tbody>
</table>
</body>
</html>

示例下载 密码: sfgt