Return the completed command object?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/code-flow.club/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
4 views
Used Spring MVC, sample code presented below:


The methods of the controller
/*
* Create a new object and initialize it.
*/
@RequestMapping(value = "/new", method = RequestMethod.GET)
public String newObject(Model model) {
Citizen citizen= citizenDao.getNewObject();
citizen.setIsLoyalToTheParty(true);
model.addAttribute("citizen", citizen);
return "CitizenForm";
}

/*
* Processing the completed forms.
*/
@RequestMapping(value = "/onSubmit", method = RequestMethod.POST)
public String onSubmit(Citizen to citizen, BindingResult result, SessionStatus status, Model model) {
citizenValidator.validate(citizen, result);
if (result.hasErrors()) {
return "СitizenForm";
} else {
citizenDao.save(citizen);
status.setComplete();
return "successView";
}
}



Essence:
@Entity
@Table(name = "ASD_CIT")
public class Citizen implements Serializable {
@Id
private Long id;
private String firstName;
private String secondName;
private boolean IsLoyalToTheParty;

public Citizen(){
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getSecondName() {
return secondName;
}

public void setSecondName(String secondName) {
this.secondName = secondName;
}

public boolean isIsLoyalToTheParty() {
return IsLoyalToTheParty;
}

public void setIsLoyalToTheParty(boolean IsLoyalToTheParty) {
this.IsLoyalToTheParty = IsLoyalToTheParty;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}
}



And finally, the form:

<form:form commandName="citizen" method="post" action="onSubmit">
<form:hidden path="id" id="id"/>
<form:input path="firstName" id="firstName"/>
<form:input path="secondName" id="secondName"/>
</form:form>



As you can see in the code there is no form field for the property IsLoyalToTheParty — it is established at object creation and editing in the form is not provided.
Question following: in the presented case, the value of the property IsLoyalToTheParty get lost on the way the Controller -> Form -> Controller. If I create a hidden field in the form for this property, it will not be lost. But since real objects are composed of a much greater number of such properties would not want on the form create fields for these properties. How to force to return the shape of the object that was passed to it originally?
by | 4 views

2 Answers

0 like 0 dislike
Fundamentally 3 approaches:
1. pass hidden fields using input type='hidden' (if it's okay that the user is able in theory to change them)
2. in the onSubmit variable citizenDb to download Citizen from the database and override the necessary fields with values from the database (if the user should not be able to directly change some fields, such as tariff plan, the amount of money or something)
3. use the session to temporarily store an object (as written by karabara above)
\r
Now details about the session:
I) not sure if ask in the form of a new parameter, it will not change the field, i.e. can be used as # 1 above, but not as p. 2
II) if the user walked away "smoke" or he was distracted by a phone call, it may be that the session is lost and with it the form was just the (preferably separately to test this case and to do something so as to restore the form, it is desirable with the preserve user input)
\r
Because p. II generally do not use session except to store the user id.
\r
A small note: in the Spring inconvenient to do p. 2. For example, in rails possible in the model to specify which fields can be updated from Rees request automatic bindingon and which are not. Here you need to check everything manually.
by
0 like 0 dislike
Lord, all is resolved, you can go
Just need to add an attribute to the description of the controller:
\r
@SessionAttributes("citizen")
by

Related questions

0 like 0 dislike
3 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
22,997 users