multiple command name - spring

How to declare multiple commandName in JSP? Normal example (one command name):
<form:form method="post" action="saveContact.do" commandName="newContact">
I want:
<form:form method="post" action="saveContact.do" commandName="newContact" commandName="newAdress">

It is allowed to have only one object per form, you can create another class with fields newContact and newAdress.

I suggest you to make a Pojo with newContact which contains newAdress.
public class Contact{
Address adress;
getAddress(){
return address
}
setAddress(Address address){
this.address=address;
}
Fix the form to work in this way and all work well.
<form:form method="post" action="saveContact.do" commandName="newContact">
<form:input name="address.street" />
</form:form>

Related

Why do I lose information after submit a form with Spring MVC?

As I say int the title I loose information in the object that comes back from JSP to Controller.
From my Controller I pass a ModelAndView with an object of class Historic.
In the JSP page I have access to all of the values of this object, but when I submit I just get part of this information, some looses on the way on.
Controller:
#GetMapping("/tt")
public ModelAndView index(Model model) {
HistoricBO historic = new HistoricBO();
// ... I fulfill this object ...
return new ModelAndView("tt", "historic", historic);
}
In JSP I have access to all the information that I passed.
I use the values in two different ways. The first one (information that later I won't be able to recover) is:
<form:form method="POST" action="/addInput" modelAttribute="historic">
....
<form:label path="userHistoric[0].user.name" />
<form:input path="userHistoric[0].user.name" disabled="true" />
Being userHistoric a list inside HistoricBO object.
And the other way that I use the object values is daoing loop to the registers and show them. I can have these values after submit:
c:forEach items="${historic.userHistoric[0].periods[0].registers}" var="reg" varStatus="rog">
...
<td class="tab-odd">
<form:input path="userHistoric[0].periods[0].registers[${rog.index}].hours[0]" class="monin" type="number" />
</td>
The method that catch the submit is as follows:
#PostMapping("/addInput")
public String savePeriod(
#ModelAttribute("historic") HistoricBO inputs,
BindingResult result, ModelMap model) {
if (result.hasErrors()) {
return "error";
}
...
And here the object inputs only has setted the hours values, the rest of the object is empty.
Can you please why is the info loosing and how to solve it?
Thanks
Remove disabled="true" and use readonly="true" or readonly="readonly" instead like below.
<form:input path="userHistoric[0].user.name" readonly="readonly" />
Disabled values will not be submitted with the form.
See this values-of-disabled-inputs-will-not-be-submitted and demo here.

Spring - How to bind form data to a List in model

I have a User model which has a list of address, on User form person can add as many as addresses he wants, how can I bind this data directly to address list?
one way I found is to do like below in jsp, but this requires index variable to add.
<form:input path="address[0].street" type="text"/>
<form:input path="address[0].state" type="text"/>
<form:input path="address[0].postalcode" type="text"/>
Is there any way to set this data dynamically
One workaround you can try, create an AddressWrapper object which accepts list of address . And use this AddressWrapper object inside user model
class AddressWrapper{
private List<Address> addressWrapper;
//generate getters and setters
}
Use this wrapper object for binding address.
class User{
private AddressWrapper addressWrapper;
//generate getters and setters
}
you can use foreach like below code
<c:forEach items="${allAdresses}" var="address" varStatus="status">
<form:input path="address[${status.index}].street" type="text"/>
<form:input path="address[${status.index}].state" type="text"/>
<form:input path="address[${status.index}].postalcode" type="text"/>
</c:forEach>
and if you have new Address you just add new object in allAdresses Array

Spring relationships between tables in view

Well, I started a new project using Spring MVC (I'm beginner in technology) and soon I had a basic question which I am unable to find on the internet, maybe the reason that I'm doing wrong or implementing the wrong question.
I have a form in which the data will be persisted are in two different tables.
What better way to do this?
I created two related tables, one called "Agency" and another called "Login". An "Agency" may contain one or more "Login" (# OneToMany), but the problem takes the view creation time, because data from both tables will compose a single form. With some research I noticed that I can not have two modelAttribute in my form.
I apologize for the mistakes in English.
Best regards!
if the mapping is correct and Agency contain one or many login, what you have to do is render the Agency in the model and view and in your form iterate the logins
<form:form id="foo"
method="post"
action="url"
modelAttribute="agency">
<form:input type="hidden" path="id"/>
<c:forEach var="login" items="${agency.logins}"
varStatus="login_index">
<form:input type="hidden" path="login.id" />
</c:foreach>
</form:form>
thanks for the reply.
But is not it :(
I have a form in which the data will be persisted are in two different tables.
<form class="form-signin" method="post" action="addAgency">
<div class="input-group">
<span class="input-group-addon entypo-user"></span>
//Table Agency
<spring:bind path="tenant.firstName"/>
<input class="form-control" placeholder="Nome"/>
//Table Login
<spring:bind path="login.email"/>
<input class="form-control" placeholder="Nome"/>
</div>
//Rest of my form...
</form>
In my view I have the annotation "bind" Spring, searching in the internet I found this way to make the connection between the controller and the view for persist two tables.
#RequestMapping(value = "/", method = RequestMethod.GET)
public String home(#ModelAttribute("tenant") Agency tenant, #ModelAttribute("login") Login login, ModelMap map) {
Agency agency = dashboardFacade.getAgency();
map.addAttribute("agency", agency);
if (tenantResolver.isMasterTenant()) {
//Here is the problem!!
// Add an attribute in my view of type login and agency, but i don't kwon if it is correct.
map.addAttribute("tenant", tenant);
map.addAttribute("login", login);
return "landing/index";
} else {
return "dashboard/home";
}
}
The method below to save an agency and login.
// Add a new agency
#RequestMapping(value = "/addAgency", method = RequestMethod.POST)
public String addAgency(#ModelAttribute("tenant") Agency agency, #ModelAttribute("login") Login login, Model model, final RedirectAttributes redirectAttributes) {
agency = dashboardFacade.addAgency(agency);
login = dashboardFacade.addLogin(login);
return "redirect:" + getAgencyFullUrl(agency);
}
What better way to do this?
Thank you

How to pass a default value to a Spring input/hidden element?

Suppose, I have the following Spring form
<form:form id="mainForm" name="mainForm"
method="post" action="Temp.htm" commandName="tempBean">
<form:hidden path="stringValue" />
</form:form>
The hidden field is mapped with the command bean - TempBean. What if, I need to pass a default value which is dynamic to this hidden field and dependent upon some other operations?
HTML context:
<c:set var="someVariable" value="${someValue}"/>
<input type="hidden"
id="stringValue"
name="stringValue"
value="${someVariable}"/>
The tags like <form:input> and <form:hidden> don't have a value attribute. So, how to pass a default value to a command object in this scenario?
I'm using Spring 3.2.0.
You can set the default value to the bean you are using in common name
#RequestMapping(value="/someView.html", method=RequestMethod.GET)
public String someView(ModelMap modelMap){
TempBean tempBean = new TempBean();
tempBean.setStringValue(somevalue);
modelMap.addAttribute(tempBean );
return "something";
}

Spring MVC spring:bind tag

Trying to get hold of <spring:bind> tag. I am trying to print a String value. The code looks something like:
mySimpleBean myBean = presentStudent.getSubjects().getTeacherName();
String firstName = myBean.getTeacherFirstName()
Where I get "myBean" from another bean "presentStudent". On jsp I am trying:
<spring:bind path="presentStudent.subjects.teacherName.teacherFirstName">
<input type="text" name="${status.expression}" value="${status.value}">
But it doesnt print anything.
Also "presentStudent" is commandObject for this form:
<form:form id="stuTeachForm" name="stuTeachForm" method="post" commandName="presentStudent"
action="getStuTeachData.html">
You can use the sping input tag instead of the bind tag
<form:form id="stuTeachForm" name="stuTeachForm" method="post" commandName="presentStudent" action="getStuTeachData.html">
<form:input type="text" path="subjects.teacherName.teacherFirstName"/>
Might also be worth outputting the desired value to check your bean has been properly populated
First name is ${presentStudent.subjects.teacherName.teacherFirstName}

Resources