Answer the question
In order to leave comments, you need to log in
Returning a completed command object?
Used by Spring MVC, sample code is below:
Controller Methods
/*<br/>
* Создание нового объекта и его инициализация.<br/>
*/ <br/>
@RequestMapping(value = "/new", method = RequestMethod.GET)<br/>
public String newObject(Model model) {<br/>
Сitizen citizen= citizenDao.getNewObject();<br/>
citizen.setIsLoyalToTheParty(true);<br/>
model.addAttribute("citizen", citizen);<br/>
return "CitizenForm";<br/>
}<br/>
<br/>
/*<br/>
* Обработка заполненной формы.<br/>
*/<br/>
@RequestMapping(value = "/onSubmit", 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 "СitizenForm";<br/>
} else {<br/>
citizenDao.save(citizen);<br/>
status.setComplete();<br/>
return "successView"; <br/>
}<br/>
}<br/>
@Entity<br/>
@Table(name = "ASD_CIT")<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/>
<form:form commandName="citizen" method="post" action="onSubmit"><br/>
<form:hidden path="id" id="id"/><br/>
<form:input path="firstName" id="firstName"/><br/>
<form:input path="secondName" id="secondName"/><br/>
</form:form><br/>
Answer the question
In order to leave comments, you need to log in
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question