Use HashMap as Form Backing Bean Spring MVC + ThymeLeaf -



Use HashMap as Form Backing Bean Spring MVC + ThymeLeaf -

i new spring mvc (coming grails). possible utilize hashmap form backing bean?

in grails, 1 has access object called params controller action. params map contains values of fields included in posted data. have read far, have create form backing bean of forms.

is using maps backing object possible?

you don't need utilize form backing object this. if want access parameters passed in request (e.g. post, ...) need parameters map httpservletrequest#getparametermap method. @ illustration example prints parameters name , value console.

on other hand. if want utilize binding can wrap map object form backing bean.

controller

import java.util.arrays; import java.util.map; import java.util.map.entry; import javax.servlet.http.httpservletrequest; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; @controller public class parametermapcontroller { @requestmapping(value = "/", method = requestmethod.get) public string render() { homecoming "main.html"; } @requestmapping(value = "/", method = requestmethod.post) public string submit(httpservletrequest req) { map<string, string[]> parametermap = req.getparametermap(); (entry<string, string[]> entry : parametermap.entryset()) { system.out.println(entry.getkey() + " = " + arrays.tostring(entry.getvalue())); } homecoming "redirect:/"; } }

main.html

<!doctype html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8" /> </head> <body> <form th:action="@{/}" method="post"> <label for="value1">value 1</label> <input type="text" name="value1" /> <label for="value2">value 2</label> <input type="text" name="value2" /> <label for="value3">value 3</label> <input type="text" name="value3" /> <input type="submit" value="submit" /> </form> </body> </html>

spring-mvc thymeleaf

Comments

Popular posts from this blog

java Multi query from Mysql using netbeans -

c# - DotNetZip fails with "stream does not support seek operations" -

c++ - StartServiceCtrlDispatcher don't can access 1063 error -