Skip to content

Commit 0e80528

Browse files
committed
springmvc中的参数绑定
1 parent 46482a6 commit 0e80528

File tree

16 files changed

+380
-313
lines changed

16 files changed

+380
-313
lines changed
Binary file not shown.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<%@ page language="java" contentType="text/html; charset=UTF-8"
2+
pageEncoding="UTF-8"%>
3+
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
4+
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
5+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
6+
<html>
7+
<head>
8+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
9+
<title>修改商品信息</title>
10+
11+
</head>
12+
<body>
13+
14+
<form id="itemForm" action="${pageContext.request.contextPath }/editItemsSubmit.action" method="post" >
15+
<input type="hidden" name="id" value="${itemsCustom.id }"/>
16+
修改商品信息:
17+
<table width="100%" border=1>
18+
<tr>
19+
<td>商品名称</td>
20+
<td><input type="text" name="name" value="${itemsCustom.name }"/></td>
21+
</tr>
22+
<tr>
23+
<td>商品价格</td>
24+
<td><input type="text" name="price" value="${itemsCustom.price }"/></td>
25+
</tr>
26+
<tr>
27+
<td>商品生产日期</td>
28+
<td><input type="text" name="createtime" value="<fmt:formatDate value="${itemsCustom.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td>
29+
</tr>
30+
<%-- <tr>
31+
<td>商品图片</td>
32+
<td>
33+
<c:if test="${item.pic !=null}">
34+
<img src="/pic/${item.pic}" width=100 height=100/>
35+
<br/>
36+
</c:if>
37+
<input type="file" name="pictureFile"/>
38+
</td>
39+
</tr> --%>
40+
<tr>
41+
<td>商品简介</td>
42+
<td>
43+
<textarea rows="3" cols="30" name="detail">${itemsCustom.detail }</textarea>
44+
</td>
45+
</tr>
46+
<tr>
47+
<td colspan="2" align="center"><input type="submit" value="提交"/>
48+
</td>
49+
</tr>
50+
</table>
51+
52+
</form>
53+
</body>
54+
55+
</html>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<%@ page language="java" contentType="text/html; charset=UTF-8"
2+
pageEncoding="UTF-8"%>
3+
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
4+
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
5+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
6+
<html>
7+
<head>
8+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
9+
<title>查询商品列表</title>
10+
<script type="text/javascript">
11+
function editItemsAllSubmit(){
12+
//提交form
13+
document.itemsForm.action="${pageContext.request.contextPath }/editItemsQueryResult.action";
14+
document.itemsForm.submit();
15+
}
16+
function queryItems(){
17+
//提交form
18+
document.itemsForm.action="${pageContext.request.contextPath }/queryItems.action";
19+
document.itemsForm.submit();
20+
}
21+
</script>
22+
</head>
23+
<body>
24+
<form name="itemsForm" action="${pageContext.request.contextPath }/queryItems.action" method="post">
25+
查询条件:
26+
<table width="100%" border=1>
27+
<tr>
28+
<td>
29+
商品名称:<input name="itemsCustom.name" />
30+
</td>
31+
<td><input type="button" value="查询" onclick="queryItems()"/>
32+
<input type="button" value="批量修改提交" onclick="editItemsAllSubmit()"/>
33+
</td>
34+
</tr>
35+
</table>
36+
商品列表:
37+
<table width="100%" border=1>
38+
<tr>
39+
<td>商品名称</td>
40+
<td>商品价格</td>
41+
<td>生产日期</td>
42+
<td>商品描述</td>
43+
<td>操作</td>
44+
</tr>
45+
<c:forEach items="${itemsList }" var="item" varStatus="status">
46+
<tr>
47+
48+
<td><input name="itemsList[${status.index }].name" value="${item.name }"/></td>
49+
<td><input name="itemsList[${status.index }].price" value="${item.price }"/></td>
50+
<td><input name="itemsList[${status.index }].createtime" value="<fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td>
51+
<td><input name="itemsList[${status.index }].detail" value="${item.detail }"/></td>
52+
53+
54+
</tr>
55+
</c:forEach>
56+
57+
</table>
58+
</form>
59+
</body>
60+
61+
</html>

WebRoot/WEB-INF/jsp/items/itemsList.jsp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,33 @@
77
<head>
88
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
99
<title>查询商品列表</title>
10+
<script type="text/javascript">
11+
function queryItems() {
12+
document.itemsForm.action="${pageContext.request.contextPath }/editItemsAllSubmit.action";
13+
document.itemsForm.submit();
14+
}
15+
function deleteItems() {
16+
document.itemsForm.action="${pageContext.request.contextPath }/deleteItems.action";
17+
document.itemsForm.submit();
18+
}
19+
</script>
1020
</head>
1121
<body>
12-
<form
13-
action="${pageContext.request.contextPath }/item/queryItem.action"
22+
<form name="itemsForm"
23+
action="${pageContext.request.contextPath }/editItemsAllSubmit.action"
1424
method="post">
1525
查询条件:
1626
<table width="100%" border=1>
1727
<tr>
18-
<td><input type="submit" value="查询" /></td>
28+
<td>商品名称:<input name="itemsCustom.name" /></td>
29+
<td><input type="button" value="查询" onclick="queryItems()"/></td>
30+
<td><input type="button" value="批量删除" onclick="deleteItems()"/></td>
1931
</tr>
2032
</table>
2133
商品列表:
2234
<table width="100%" border=1>
2335
<tr>
36+
<td>选择</td>
2437
<td>商品名称</td>
2538
<td>商品价格</td>
2639
<td>生产日期</td>
@@ -29,14 +42,15 @@
2942
</tr>
3043
<c:forEach items="${itemsList }" var="item">
3144
<tr>
45+
<td><input type="checkbox" name="items_id" value="${item.id }"/></td>
3246
<td>${item.name }</td>
3347
<td>${item.price }</td>
3448
<td><fmt:formatDate value="${item.createtime}"
3549
pattern="yyyy-MM-dd HH:mm:ss" /></td>
3650
<td>${item.detail }</td>
3751

3852
<td><a
39-
href="${pageContext.request.contextPath }/item/editItem.action?id=${item.id}">修改</a></td>
53+
href="${pageContext.request.contextPath }/editItems.action?id=${item.id}">修改</a></td>
4054

4155
</tr>
4256
</c:forEach>

WebRoot/WEB-INF/jsp/success.jsp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<%@ page language="java" contentType="text/html; charset=UTF-8"
2+
pageEncoding="UTF-8"%>
3+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4+
<html>
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7+
<title>成功提示</title>
8+
</head>
9+
<body>
10+
操作成功!
11+
</body>
12+
</html>

WebRoot/WEB-INF/web.xml

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,56 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<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">
3-
<display-name>SpringMVC_Study</display-name>
4-
5-
<!-- 加载spring容器 -->
6-
<context-param>
7-
<param-name>contextConfigLocation</param-name>
8-
<param-value>classpath:spring/applicationContext-*.xml</param-value>
9-
</context-param>
10-
<listener>
11-
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
12-
</listener>
13-
14-
<welcome-file-list>
15-
<welcome-file>index.jsp</welcome-file>
16-
</welcome-file-list>
17-
18-
<!-- 配置springmvc的前端控制器DispatcherServlet(相当于一个servlet) -->
19-
<servlet>
20-
<servlet-name>springmvc</servlet-name>
21-
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
22-
23-
<!-- contextConfigLocation是配置springmvc加载的配置文件(配置处理器映射器、适配器等)
24-
如果不配置contextConfigLocation,则默认加载的是/WEB-INF/springmvc-servlet.xml
25-
-->
26-
<init-param>
27-
<param-name>contextConfigLocation</param-name>
28-
<param-value>classpath:springmvc.xml</param-value>
29-
</init-param>
30-
</servlet>
31-
32-
<servlet-mapping>
33-
<servlet-name>springmvc</servlet-name>
34-
<!--
35-
第一种:*.action,可以访问以.action结尾的地址,由DispatcherServlet进行解析
36-
第二种:/,所有访问的地址都由DispatcherServlet进行解析,对应静态文件的解析需要配置不让DispatcherServlet进行解析
37-
第三种:/*,这样配置是不对的,使用这种配置,最重要转发到一个jsp页面时,仍然会由DispatcherServlet解析jsp地址,
38-
不能根据jsp页面找打Handler,会报错
39-
-->
40-
<url-pattern>*.action</url-pattern>
41-
</servlet-mapping>
42-
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://java.sun.com/xml/ns/javaee"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
5+
id="WebApp_ID" version="3.0">
6+
<display-name>SpringMVC_Study</display-name>
7+
8+
<welcome-file-list>
9+
<welcome-file>index.jsp</welcome-file>
10+
</welcome-file-list>
11+
12+
<!-- 加载spring容器 -->
13+
<context-param>
14+
<param-name>contextConfigLocation</param-name>
15+
<param-value>classpath:spring/applicationContext-*.xml</param-value>
16+
</context-param>
17+
<listener>
18+
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
19+
</listener>
20+
21+
<!-- 过滤器解决post乱码问题 -->
22+
<filter>
23+
<filter-name>CharacterEncodingFilter</filter-name>
24+
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
25+
<init-param>
26+
<param-name>encoding</param-name>
27+
<param-value>utf-8</param-value>
28+
</init-param>
29+
</filter>
30+
<filter-mapping>
31+
<filter-name>CharacterEncodingFilter</filter-name>
32+
<url-pattern>/*</url-pattern>
33+
</filter-mapping>
34+
35+
36+
37+
<!-- 配置springmvc的前端控制器DispatcherServlet(相当于一个servlet) -->
38+
<servlet>
39+
<servlet-name>springmvc</servlet-name>
40+
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
41+
42+
<!-- contextConfigLocation是配置springmvc加载的配置文件(配置处理器映射器、适配器等) 如果不配置contextConfigLocation,则默认加载的是/WEB-INF/springmvc-servlet.xml -->
43+
<init-param>
44+
<param-name>contextConfigLocation</param-name>
45+
<param-value>classpath:spring/springmvc.xml</param-value>
46+
</init-param>
47+
</servlet>
48+
49+
<servlet-mapping>
50+
<servlet-name>springmvc</servlet-name>
51+
<!-- 第一种:*.action,可以访问以.action结尾的地址,由DispatcherServlet进行解析 第二种:/,所有访问的地址都由DispatcherServlet进行解析,对应静态文件的解析需要配置不让DispatcherServlet进行解析
52+
第三种:/*,这样配置是不对的,使用这种配置,最重要转发到一个jsp页面时,仍然会由DispatcherServlet解析jsp地址, 不能根据jsp页面找打Handler,会报错 -->
53+
<url-pattern>*.action</url-pattern>
54+
</servlet-mapping>
55+
4356
</web-app>

WebRoot/WEB-INF/classes/springmvc.xml renamed to config/spring/springmvc.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,23 @@
6868
如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
6969
实际开发时使用mvc:annotation-driven
7070
-->
71-
<mvc:annotation-driven></mvc:annotation-driven>
71+
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
7272
<!-- 也不用配置具体的Controller了,直接使用这个进行扫描包即可,也可以扫描service -->
7373
<context:component-scan base-package="ssm.controller"></context:component-scan>
7474

7575
<!-- 配置视图解析器
7676
进行jsp解析,默认使用jstl标签,classpath下得有jstl的包
7777
-->
7878
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
79+
80+
<!-- 自定义参数绑定 -->
81+
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
82+
<!-- 转换器 -->
83+
<property name="converters">
84+
<list>
85+
<!-- 日期类型转换 -->
86+
<bean class="ssm.controller.converter.CustomDateConverter"/>
87+
</list>
88+
</property>
89+
</bean>
7990
</beans>

config/springmvc.xml

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)