SpringMVC HashMap Form Integration example

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 >
File: /WebContent/WEB-INF/spring-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: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 >
File: /src/net/viralpatel/spring3/form/ContactForm.java

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;
     }
 
}
File: /src/net/viralpatel/spring3/controller/ContactController.java

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);
     }
}
File: /WebContent/WEB-INF/jsp/add_contact.jsp

<%@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 >
File: /WebContent/WEB-INF/jsp/show_contact.jsp

<%@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 >
File: /WebContent/index.jsp

<jsp:forward page="show.html"></jsp:forward>

程序运行ok!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值