1.需要导入的jar包
commons-logging-
1.1
.3.
jar
spring
-aop-
4.0
.0.RELEASE.
jar
spring
-beans-
4.0
.0.RELEASE.
jar
spring
-context-
4.0
.0.RELEASE.
jar
spring
-core-
4.0
.0.RELEASE.
jar
spring
-expression-
4.0
.0.RELEASE.
jar
spring
-web-
4.0
.0.RELEASE.
jar
spring
-webmvc-
4.0
.0.
RELEASE
.
jar
jstl.
jar
standard
.
jar
2.web.xml中的配置
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
xsi:schemaLocation
=
"
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
;
id
=
"WebApp_ID"
version
=
"2.5"
>
<
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:springmvc.xml
</
param-value
>
</
init-param
>
<
load-on-startup
>
1
</
load-on-startup
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>
springDispatcherServlet
</
servlet-name
>
<
url-pattern
>
/
</
url-pattern
>
</
servlet-mapping
>
</
web-app
>
3.SpringMVC的配置文件
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
xsi:schemaLocation
=
"
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.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.0.xsd"
;
>
<!-- 设置自动扫描的包 -->
<
context:component-scan
base-package
=
"com.atguigu.springmvc"
></
context:component-scan
>
<!-- 配置视图解析器 -->
<
bean
id
=
"viewResolver"
class
=
"org.springframework.web.servlet.view.InternalResourceViewResolver"
>
<
property
name
=
"prefix"
value
=
"/WEB-INF/views/"
></
property
>
<
property
name
=
"suffix"
value
=
".jsp"
></
property
>
</
bean
>
<!-- 配置了mvc:default-
servlet
-handler标签之后,@RequestMapping就失效了,此时需要配置以下标签 -->
<
mvc:annotation-driven
></
mvc:annotation-driven
>
</
beans
>
- 1)配置处理静态资源
<!-- 配置处理静态资源 -->
<!-- 将在 SpringMVC 上下文中定义一个 DefaultServletHttpRequestHandler,
它会对进入 DispatcherServlet 的请求进行筛查,如果发现是没有经过映射的请求,
就将该请求交由 WEB 应用服务器默认的
Servlet
处理,如果不是静态资源的请求,才由 DispatcherServlet 继续处理
-->
<!-- default-
servlet
-name属性:如果WEB应用的服务器默认的
Servlet
的名字是default,该属性可以省略不写 -->
<
mvc:default-servlet-handler
/>
- 2)配置不经过Handler方法直接响应的页面
<!-- 设置发送请求时不经过Handler的方法 而直接响应的页面-->
<
mvc:view-controller
path
=
"/testI18n"
view-name
=
"success"
/>
- 不管配置了1)还是2),必须得配置以下标签
<!-- 配置了mvc:default-
servlet
-handler标签之后,@RequestMapping就失效了,此时需要配置以下标签 -->
<!-- 配置了mvc:view-controller标签之后,@RequestMapping注解就失效了,这时必须配置以下标签(开发时我们一般都配置以下标签) -->
<
mvc:annotation-driven
></
mvc:annotation-driven
>
4.具体实现
- Handler中的方法
@Controller
public
class
EmployeeHandler {
@Autowired
private
EmployeeDao
employeeDao
;
@Autowired
private
DepartmentDao
departmentDao
;
// 获取所有员工
@RequestMapping
(value =
"/emps"
, method = RequestMethod.
GET
)
public
String getEmployees(Map<String, Object>
map
) {
// 获取所有的员工
Collection<Employee>
emps
=
employeeDao
.getAll();
// 将所有的员工放到map中,最终会放到request域中
map
.put(
"emps"
,
emps
);
return
"list"
;
}
// 获取所有的部门信息并去添加员工的页面
@RequestMapping
(value =
"/emp"
, method = RequestMethod.
GET
)
public
String inputEmployee(Map<String, Object>
map
) {
// 获取所有的部门
Collection<Department>
depts
=
departmentDao
.getDepartments();
// 将所有的部门放到map中,最终会放到request域中
map
.put(
"depts"
,
depts
);
// map.put("employee", new Employee());
return
"input"
;
}
// 添加新员工
@RequestMapping
(value =
"/emp"
, method = RequestMethod.
POST
)
public
String addEmployee(Employee
employee
) {
// 保存用户
employeeDao
.save(
employee
);
return
"
redirect:/
emps"
;
}
// 删除员工
@RequestMapping
(value =
"/emp/{id}"
, method = RequestMethod.
DELETE
)
public
String deleteEmployee(
@PathVariable
(
"id"
) Integer
id
) {
// 删除员工
employeeDao
.delete(
id
);
return
"
redirect:/
emps"
;
}
// 获取员工
@RequestMapping
(value =
"/emp/{id}"
, method = RequestMethod.
GET
)
public
String getEmployee(
@PathVariable
(
"id"
) Integer
id
, Map<String, Object>
map
) {
// 根据员工的id获取要更新的员工
Employee
employee
=
employeeDao
.get(
id
);
// 将员工放到map中
map
.put(
"employee"
,
employee
);
// 获取所有的部门
Collection<Department>
depts
=
departmentDao
.getDepartments();
// 将所有的部门放到map中,最终会放到request域中
map
.put(
"depts"
,
depts
);
return
"input"
;
}
@ModelAttribute
public
Employee getEmployeeModel(
@RequestParam
(value =
"id"
, required =
false
) Integer
id
) {
Employee
employee
=
new
Employee();
if
(
id
!=
null
) {
// 从数据库中将该员工查询出来
employee
=
employeeDao
.get(
id
);
}
return
employee
;
}
// 更新员工
@RequestMapping
(value =
"/emp/{id}"
, method = RequestMethod.
PUT
)
public
String updateEmployee(Employee
employee
) {
// 更新员工
employeeDao
.save(
employee
);
return
"redirect:/emps"
;
}
}
- 显示所有员工的页面
<%@
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
>
<
script
type
=
"text/javascript"
src
=
"
${pageContext.request.contextPath }
/scripts/jquery-1.9.1.min.js"
></
script
>
<
script
type
=
"text/javascript"
>
$(
function
(){
//给所有删除员工的超链接绑定单击事件
$(
".empDelete"
).click(
function
(){
//1.获取超链接中的href属性值
var
url = $(
this
).attr(
"href"
);
//2.将url赋给form表单的action属性
$(
"#deleteForm"
).attr(
"action"
,url);
//3.手动提交表单
$(
"#deleteForm"
).submit();
//取消默认行为
return
false
;
});
});
</
script
>
</
head
>
<
body
>
<
form
action
=
""
method
=
"post"
id
=
"deleteForm"
>
<!-- 设置_method请求参数 -->
<
input
type
=
"hidden"
name
=
"_method"
value
=
"delete"
>
</
form
>
<
center
>
<
c:if
test
=
"
${
empty
requestScope.emps }
"
>
<
h1
>
没有任何员工
</
h1
>
</
c:if
>
<
c:if
test
=
"
${!
empty
requestScope.emps }
"
>
<
table
border
=
"1"
cellspacing
=
"0"
cellpadding
=
"10"
style
="
text-align
:
center
;"
>
<
tr
>
<
th
>
工号
</
th
>
<
th
>
姓名
</
th
>
<
th
>
邮箱
</
th
>
<
th
>
性别
</
th
>
<
th
>
部门
</
th
>
<
th
colspan
=
"2"
>
操作
</
th
>
</
tr
>
<
c:forEach
items
=
"
${requestScope.emps }
"
var
=
"emp"
>
<
tr
>
<
td
>
${emp.id }
</
td
>
<
td
>
${emp.lastName }
</
td
>
<
td
>
${emp.email }
</
td
>
<
td
>
<
c:if
test
=
"
${emp.gender == 1 }
"
>
男
</
c:if
>
<
c:if
test
=
"
${emp.gender == 0 }
"
>
女
</
c:if
>
</
td
>
<
td
>
${emp.department.departmentName }
</
td
>
<
td
><
a
href
=
"
${pageContext.request.contextPath }
/emp/
${emp.id}
"
>
编辑
</
a
></
td
>
<
td
><
a
class
=
"empDelete"
href
=
"
${pageContext.request.contextPath }
/emp/
${emp.id}
"
>
删除
</
a
></
td
>
</
tr
>
</
c:forEach
>
</
table
>
</
c:if
>
<
br
>
<
a
href
=
"
${pageContext.request.contextPath }
/emp"
>
添加新员工
</
a
>
</
center
>
</
body
>
</
html
>
- 添加和更新员工的页面
<%@
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
>
<!-- 使用SpringMVC提供的表单标签的好处:
1.便于快速的开发
2.便于表单的回显
-->
<!-- SpringMVC提供的表单默认表单中的数据是必须要回显的,默认情况下,SpringMVC会以command 作为key从request域中
查询模型数据,然后回显,找不到则会抛出异常。我们可以通过form表单的modelAttribute属性来指定在request域中放的模型数据的key
- 一般情况下,通过 GET 请求获取表单页面,而通过 POST 请求提交表单页面,因此获取表单页面和提交表单页面的 URL 是相同的。,
- 只要满足该最佳条件的契约,<form:form> 标签就无需通过 action 属性指定表单提交的 URL
- 可以通过 modelAttribute 属性指定绑定的模型属性,若没有指定该属性,则默认从 request 域对象中读取 command 的表单 bean,如果该属性值也不存在,则会发生错误。
-
- 只要满足该最佳条件的契约,<form:form> 标签就无需通过 action 属性指定表单提交的 URL
- SpringMVC 提供了多个表单组件标签,如 <form:input/>、<form:select/> 等,用以绑定表单字段的属性值,它们的共有属性如下:
- path:表单字段,对应 html 元素的 name 属性,支持级联属性
- htmlEscape:是否对表单值的 HTML 特殊字符进行转换,默认值为 true
- cssClass:表单组件对应的 CSS 样式类名
- cssErrorClass:表单组件的数据存在错误时,采取的 CSS 样式
- form:input、form:password、form:hidden、form:textarea:对应 HTML 表单的 text、password、hidden、textarea 标签
- form:radiobutton:单选框组件标签,当表单 bean 对应的属性值和 value 值相等时,单选框被选中
- form:radiobuttons:单选框组标签,用于构造多个单选框
- items:可以是一个 List、String[] 或 Map
- itemValue:指定 radio 的 value 值。可以是集合中 bean 的一个属性值
- itemLabel:指定 radio 的 label 值
- delimiter:多个单选框可以通过 delimiter 指定分隔符
- form:checkbox:复选框组件。用于构造单个复选框
- form:checkboxs:用于构造多个复选框。使用方式同 form:radiobuttons 标签
- form:select:用于构造下拉框组件。使用方式同 form:radiobuttons 标签
- form:option:下拉框选项组件标签。使用方式同 form:radiobuttons 标签
- form:errors:显示表单组件或数据校验所对应的错误
- <form:errors path= “*” /> :显示表单所有的错误
- <form:errors path= “user*” /> :显示所有以 user 为前缀的属性对应的错误
- <form:errors path= “username” /> :显示特定表单对象属性的错误
-->
<
form:form
modelAttribute
=
"employee"
>
<!-- path属性就相当于input中的name属性 -->
<!-- 当员工的id为null,即在添加新员工时再显示姓名 -->
<
c:if
test
=
"
${
empty
requestScope.employee.id }
"
>
姓名:
<
form:input
path
=
"lastName"
/><
br
>
</
c:if
>
<!-- 对应更新来说,需要将POST请求转换为PUT请求,所以需要传一个请求参数_method -->
<
c:if
test
=
"
${!
empty
requestScope.employee.id }
"
>
<
form:hidden
path
=
"id"
/>
<
input
type
=
"hidden"
name
=
"_method"
value
=
"put"
>
</
c:if
>
邮箱:
<
form:input
path
=
"email"
/><
br
>
性别:
<
form:radiobutton
path
=
"gender"
value
=
"1"
label
=
"男"
/>
<
form:radiobutton
path
=
"gender"
value
=
"0"
label
=
"女"
/><
br
>
部门:
<
form:select
path
=
"department.id"
items
=
"
${requestScope.depts }
"
itemValue
=
"id"
itemLabel
=
"departmentName"
></
form:select
>
<
br
>
<
input
type
=
"submit"
>
</
form:form
>
</
body
>
</
html
>