Binding Entities and Their Children to Java JSP Form Elements in Spring Framework MVC
2013-04-08 10:22
555 查看
Binding objects to JSP form elements is actually quite easy in Spring Framework using the “modelAttribute” and “path” tags. Our scenario was the use of two Hibernate POJOs comprised of a parent entity “Parent” and a single “Child” entity which needed to be
displayed on screen with appropriate form elements (“input” and “select”). The entities were annotated for use as part of a larger application but the basic structure was:
To display these objects on screen as part of a form in Spring Framework MVC we used the following “ParentController” controller with a “showparent” action. The action uses a “parentService.getParent(id)” method (not shown) to retrieve a single parent entity
based on “id” and attach it to the form using a Map “model” and a returned “ModelAndView”. A “ch
14ff2
ildService” is used to get the list of every possible child entity (also not shown) and this is also added to the “ModelAndView”:
The form itself is very simple and uses Spring binding to automatically create and populate input fields:
Spring Framework uses the “modelAttribute” and “path” tags to determine what is set on the form. In this case the forms “modelAttribute” is “parent” which means that the elements in the form are properties of the “parent” entity. The “description” is shown
as the contents of a simple textbox input using “form:input”. The interesting part is the “form:select” which automatically populates the select with option objects based on the set of “child” objects in “children” and selects the correct “child” option for
the “parent” based on the “itemValue”, “id” in this case.
Submitting changes to “parent” entities is covered
in my more recent post.
displayed on screen with appropriate form elements (“input” and “select”). The entities were annotated for use as part of a larger application but the basic structure was:
based on “id” and attach it to the form using a Map “model” and a returned “ModelAndView”. A “ch
14ff2
ildService” is used to get the list of every possible child entity (also not shown) and this is also added to the “ModelAndView”:
as the contents of a simple textbox input using “form:input”. The interesting part is the “form:select” which automatically populates the select with option objects based on the set of “child” objects in “children” and selects the correct “child” option for
the “parent” based on the “itemValue”, “id” in this case.
Submitting changes to “parent” entities is covered
in my more recent post.
相关文章推荐
- Spring SpEL in JSP and Assign SpEL value to Java variable in JSP
- Use a Spring InitBinder to Resolve Type Mismatch and Bind Exceptions in POST from Spring Framework M
- How-To : Using EJB 3.0 and Java Persistence API with Spring in OC4J
- Export Grid Data To Excel In Advance Kendo UI Using MVC WEB API And Entity Framework
- Beyond REST: How to build a HATEOAS API in Java with Spring MVC, Jersey (JAX-RS) and VRaptor
- Spring Form Tags - How to use Text Box, Radio Button, Check Box and Drop Down List in Spring
- 异常-----springmvc + ajaxfileupload解决ajax不能异步上传图片的问题。java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.
- How to use JSon data in mvc action and post form data use JQuery ajax
- Injecting and Binding Objects to Spring MVC Controllers--转
- CQRS and Event Sourcing in Java with Spring Framework
- ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: An attempt by a client to chec
- how to make form:checkboxes in JSP
- IE 9 打开FORM 界面,JAVA TM is blocked because is out of date and needs to update
- Spring MVC exception - Invoking request method resulted in exception : public static native long java.lang.System.currentTimeMillis()
- How to configure spring boot through annotations in order to have something similar to <jsp-config> in web.xml?
- Why and when to use AtomicIntegerFieldUpdater in java?
- Configuring Autofac to work with the ASP.NET Identity Framework in MVC 5
- Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplicatio
- The specified type member 'IsLock' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
- How to determine negative number and positive number in Java?