File:/WebContent/WEB-INF/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"
xmlns:web
=
"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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"
>
<
display-name
>Spring3MVC-Hashmap</
display-name
>
<
servlet
>
<
servlet-name
>spring</
servlet-name
>
<
servlet-class
>org.springframework.web.servlet.DispatcherServlet</
servlet-class
>
<
load-on-startup
>1</
load-on-startup
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>spring</
servlet-name
>
<
url-pattern
>*.html</
url-pattern
>
</
servlet-mapping
>
</
web-app
>
<?
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"
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">
<
context:annotation-config
/>
<
context:component-scan
base-package
=
"net.viralpatel.spring3.controller"
/>
<
bean
id
=
"jspViewResolver"
class
=
"org.springframework.web.servlet.view.InternalResourceViewResolver"
>
<
property
name
=
"viewClass"
value
=
"org.springframework.web.servlet.view.JstlView"
/>
<
property
name
=
"prefix"
value
=
"/WEB-INF/jsp/"
/>
<
property
name
=
"suffix"
value
=
".jsp"
/>
</
bean
>
</
beans
>
package
net.viralpatel.spring3.form;
import
java.util.HashMap;
import
java.util.Map;
public
class
ContactForm {
private
Map<String, String> contactMap =
new
HashMap<String, String>();
public
Map<String, String> getContactMap() {
return
contactMap;
}
public
void
setContactMap(Map<String, String> contactMap) {
this
.contactMap = contactMap;
}
}
package
net.viralpatel.spring3.controller;
import
java.util.HashMap;
import
java.util.Map;
import
net.viralpatel.spring3.form.ContactForm;
import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.ModelAttribute;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestMethod;
import
org.springframework.web.servlet.ModelAndView;
@Controller
public
class
ContactController {
private
static
Map<String, String> contactMap =
new
HashMap<String, String>();
static
{
contactMap.put(
"name"
,
"John"
);
contactMap.put(
"lastname"
,
"Lennon"
);
contactMap.put(
"genres"
,
"Rock, Pop"
);
}
@RequestMapping
(value =
"/show"
, method = RequestMethod.GET)
public
ModelAndView get() {
ContactForm contactForm =
new
ContactForm();
contactForm.setContactMap(contactMap);
return
new
ModelAndView(
"add_contact"
,
"contactForm"
, contactForm);
}
@RequestMapping
(value =
"/add"
, method = RequestMethod.POST)
public
ModelAndView save(
@ModelAttribute
(
"contactForm"
) ContactForm contactForm) {
return
new
ModelAndView(
"show_contact"
,
"contactForm"
, contactForm);
}
}
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<
html
>
<
head
>
<
title
>Spring 3 MVC HashMap Form - viralpatel.net</
title
>
</
head
>
<
body
>
<
h2
>Spring 3 MVC HashMap Form</
h2
>
<
form:form
method
=
"post"
action
=
"add.html"
modelAttribute
=
"contactForm"
>
<
table
>
<
tr
>
<
th
>Key</
th
>
<
th
>Value</
th
>
</
tr
>
<
c:forEach
items
=
"${contactForm.contactMap}"
var
=
"contactMap"
varStatus
=
"status"
>
<
tr
>
<
td
>${contactMap.key}</
td
>
<
td
><
input
name
=
"contactMap['${contactMap.key}']"
value
=
"${contactMap.value}"
/></
td
>
</
tr
>
</
c:forEach
>
</
table
>
<
br
/>
<
input
type
=
"submit"
value
=
"Save"
/>
</
form:form
>
</
body
>
</
html
>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<
html
>
<
head
>
<
title
>Spring 3 MVC HashMap Form - viralpatel.net</
title
>
</
head
>
<
body
>
<
h2
>Show Contact</
h2
>
<
table
>
<
tr
>
<
th
>Key</
th
>
<
th
>Value</
th
>
</
tr
>
<
c:forEach
items
=
"${contactForm.contactMap}"
var
=
"contactMap"
varStatus
=
"status"
>
<
tr
>
<
td
>${contactMap.key}</
td
>
<
td
>${contactMap.value}</
td
>
</
tr
>
</
c:forEach
>
</
table
>
<
br
/>
<
input
type
=
"button"
value
=
"Back"
onclick
=
"javascript:history.back()"
/>
</
body
>
</
html
>
<
jsp:forward
page
=
"show.html"
></
jsp:forward
>
程序运行ok!