K
K
karabara2010-10-26 15:00:08
Java
karabara, 2010-10-26 15:00:08

Returning a completed command object?

Used by Spring MVC, sample code is below:
Controller Methods

/*<br/>
 * Создание нового объекта и его инициализация.<br/>
 */ <br/>
 @RequestMapping(value = &quot;/new&quot;, method = RequestMethod.GET)<br/>
 public String newObject(Model model) {<br/>
 Сitizen citizen= citizenDao.getNewObject();<br/>
 citizen.setIsLoyalToTheParty(true);<br/>
 model.addAttribute(&quot;citizen&quot;, citizen);<br/>
 return &quot;CitizenForm&quot;;<br/>
 }<br/>
<br/>
/*<br/>
 * Обработка заполненной формы.<br/>
 */<br/>
 @RequestMapping(value = &quot;/onSubmit&quot;, method = RequestMethod.POST)<br/>
 public String onSubmit(Сitizen citizen, BindingResult result, SessionStatus status, Model model) {<br/>
 citizenValidator.validate(citizen, result);<br/>
 if (result.hasErrors()) {<br/>
 return &quot;СitizenForm&quot;;<br/>
 } else {<br/>
 citizenDao.save(citizen);<br/>
 status.setComplete();<br/>
 return &quot;successView&quot;; <br/>
 }<br/>
 }<br/>

Essence:
@Entity<br/>
@Table(name = &quot;ASD_CIT&quot;)<br/>
public class Citizen implements Serializable {<br/>
 @Id<br/>
 private Long id;<br/>
 private String firstName;<br/>
 private String secondName;<br/>
 private boolean IsLoyalToTheParty;<br/>
<br/>
public Citizen(){<br/>
 }<br/>
 <br/>
 public String getFirstName() {<br/>
 return firstName;<br/>
 }<br/>
<br/>
public void setFirstName(String firstName) {<br/>
 this.firstName = firstName;<br/>
 }<br/>
<br/>
public String getSecondName() {<br/>
 return secondName;<br/>
 }<br/>
<br/>
public void setSecondName(String secondName) {<br/>
 this.secondName = secondName;<br/>
 }<br/>
<br/>
public boolean isIsLoyalToTheParty() {<br/>
 return IsLoyalToTheParty;<br/>
 }<br/>
<br/>
public void setIsLoyalToTheParty(boolean IsLoyalToTheParty) {<br/>
 this.IsLoyalToTheParty = IsLoyalToTheParty;<br/>
 }<br/>
<br/>
public Long getId() {<br/>
 return id;<br/>
 }<br/>
<br/>
public void setId(Long id) {<br/>
 this.id = id;<br/>
 }<br/>
}<br/>

And finally the form:
&lt;form:form commandName=&quot;citizen&quot; method=&quot;post&quot; action=&quot;onSubmit&quot;&gt;<br/>
 &lt;form:hidden path=&quot;id&quot; id=&quot;id&quot;/&gt;<br/>
 &lt;form:input path=&quot;firstName&quot; id=&quot;firstName&quot;/&gt;<br/>
 &lt;form:input path=&quot;secondName&quot; id=&quot;secondName&quot;/&gt;<br/>
&lt;/form:form&gt;<br/>

As you can see, there is no field for the IsLoyalToTheParty property in the form code - it is set when the object is created and its editing in the form is not provided.
The question is the following: in the presented case, the value of the IsLoyalToTheParty property will be lost on the path Controller -> Form -> Controller. If I create a "hidden" field in the form for this property, then it will not be lost. But since real objects consist of a much larger number of such properties, I would not like to create fields for these properties on the form. How to make the form return exactly the object that was originally passed to it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
hippoage, 2010-10-26
@karabara

Fundamentally 3 approaches: 1. pass hidden
fields through input type='hidden' (if it's okay that the user can change them in theory)
should not be able to directly change some fields, such as a tariff plan, amount of money or something like that)
3. use the session to temporarily store the object (as karabara wrote above)
Now more about the session:
I) not sure what if set in form a new parameter, it will not change the field, i.e. can be used as point 1 above, but not as point 2
II) if the user went away to “smoke” or was distracted by a phone call, then it may turn out that the session disappeared and the form became simply unusable along with it (it is advisable to test this case separately and do something so as to completely restore the form, it is highly desirable with saving the data already entered by the user)
Due to paragraph II, as a rule, I do not use the session except for storing the user id.
A small note: in Spring it is inconvenient to do step 2. For example, in rails, you can specify in the model which fields can be updated from a research request by automatic binding, and which cannot. Here you need to manually check everything.

K
karabara, 2010-10-26
@karabara

Gentlemen, everything is decided, you can disperse
. You just need to add an attribute to the controller description:
@SessionAttributes("citizen")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question