Binding Arrays with Spring - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

Binding Arrays with Spring

Description:

cmd.setShinyItems(new String[]{'sun glasses', 'more sun glasses'}); return cmd; ... taglib prefix='c' uri='http://java.sun.com/jstl/core' ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 7
Provided by: patriciaa4
Category:

less

Transcript and Presenter's Notes

Title: Binding Arrays with Spring


1
Binding Arrays with Spring
  • Presented by Thomas Fischer
  • Powered by the Love of Code
  • Sponsored by Nobody
  • Generously Supported by Almon Deomampo

2
Binding Goals
  • Goals
  • Have a composite command object that contains
    arrays (primitive arrays and collections).
  • Update them as well using Spring binding (,
    rather then by hand).

3
A Simple Form Controller
  • public class BindDemoController extends
    SimpleFormController
  • protected Object formBackingObject(HttpServlet
    Request httpServletRequest) throws Exception
  • System.out.println("formBackingObject
    called")
  • DemoCommand cmd new DemoCommand()
  • cmd.setName("Sky Captain")
  • cmd.getFuzzyItems().add("Rosy Cloud")
  • cmd.getFuzzyItems().add("Fluffy Cloud")
  • cmd.setShinyItems(new String"sun
    glasses", "more sun glasses")
  • return cmd
  • protected ModelAndView onSubmit(Object obj)
    throws Exception
  • System.out.println("onSubmit called")
  • return new ModelAndView(getFormView(),
    "cmd", obj)
  • public class DemoCommand
  • private String name
  • private ArrayList fuzzyItems new
    ArrayList()
  • private String shinyItems new String0
  • public ArrayList getFuzzyItems()
  • return fuzzyItems
  • public void setFuzzyItems(ArrayList
    fuzzyItems)
  • this.fuzzyItems fuzzyItems
  • public String getName()
  • return name
  • public void setName(String name)
  • this.name name

4
Binding Arrays for Display
  • lt_at_ page contentType"text/htmlcharsetUTF-8"
    language"java" gt
  • lt_at_ taglib prefix"c" uri"http//java.sun.com/jst
    l/core" gt
  • lt_at_ taglib prefix"fmt" uri"http//java.sun.com/j
    stl/fmt" gt
  • lthtmlgt
  • ltheadgtlttitlegtSpring Bind Demolt/titlegtlt/headgt
  • ltbodygt
  • lth1gtDemonstrate Spring Binding for
    Arrayslt/h1gt
  • ltform action"bind.htm" method"post"gt
  • lttable border"1"gtlttrgtlttdgtFuzzylt/tdgtlt/trgt
  • ltcforEach var"thing"
    items"cmd.fuzzyItems"gt
  • lttrgt
  • lttdgt
  • ltcout value"thing"/gt
  • lt/tdgt
  • lt/trgt
  • lt/cforEachgt
  • lt/tablegt

5
Binding Arrays for Update
  • lttable border"1"gtlttrgtlttdgtFuzzylt/tdgtlt/trgt
  • ltcforEach var"thing"
    items"cmd.fuzzyItems" varStatus"loopStatus"gt
  • lttrgt
  • lttdgt
  • ltspringbind
    path"cmd.fuzzyItemsloopStatus.index"gt
  • ltinput type"text"
    name"ltcout value"status.expression"/gt"
    value"ltcout value"status.value"/gt"/gt
  • lt/springbindgt
  • lt/tdgt
  • lt/trgt
  • lt/cforEachgt
  • lt/tablegt

6
Notes
  • Tricky Points
  • When using the Spring bind tag, use the full path
    of the command name. (Spring cant work with the
    loop item of the JSTL forEach.)
  • The command object itself can NOT be an array. If
    there is an array of command objects, they have
    to be placed inside a container class.
  • Issues
  • Using Spring binding gives a malicious user a lot
    of power to change any field and value of the
    command object.
  • The order of the arrays have to stay constant
    between presentation and submission of the form.
    Otherwise, data can be corrupted.
Write a Comment
User Comments (0)
About PowerShow.com