Spring - Displaying a Label that reflects and updates a #ModelAttribute field - java

I have a #ModelAttribute account which has a field named title. I need to display this field in my JSP, and also bind it in the next call cycle. If I do this;
Title: ${editAccountForm.account.title} <br/>
It only displays the value. When someone submits the Form in the JSP, account is empty again. How do I get the label to reflect the value, just like a form:input tag?
I tried this:
<form:label path="account.issuer">some text</form:label> <br/>
but it dint work. Please help.

You can put a
<input type="hidden" name="account.title" value="${editAccountForm.account.title}" />
The name attribute must be the same as the one generated by a spring form:input.

but that is the same to displaying it normally ${editAccountForm.account.title}. After that, populate the value in a hidden field. that will update the value in the model

Related

Nested Loop Binding in Thymeleaf

I am making web app using Java/Spring/Thymeleaf and don't seem to be able to get past this problem!
So, here's my thymeleaf template code:
<form th:action="#{/holiday/create}" th:object="${holiday}" method="post">
<select name="user_scroll">
<option th:each="user : ${allUsers}" th:value="${user.id}"
th:text="${user.email}" th:field="${holiday.user_id}" />
</select>
<button type="submit">
Create
</button>
</form>
I have read the related questions on here but am still stumped.
I did read that you can't bind an object directly that's why I am trying to bind to the user_id property of the holiday object. I did suspect the holiday object my be out of scope but that doesn't seem to be the case. Perhaps I am accessing the user_id property incorrectly?
When I click submit and follow debug through to my controller user_id is just sent through as null.
I hope that's enough info - let me know if I need to provide more.
Thanks!
Frankie, try adding the th:field attribute to the select object instead of putting it in the option. Like this:
<select name="user_scroll" th:field="*{user_id}">
Notice the SPel syntax. Starts with an asterisk instead of $ and says user_id instead of holiday.user_id. You can access the field directly this way, since you already have defined it as your selected object in the form definition using
th:object="${holiday}"
This will also bind the selected value to the user_id property of your backing object and should solve the problem.

Cannot display a JSTL Value into input field - Spring MVC

I have this value ${agact.ppr} that i want to show in my page jsp. It can be shown like this easly:
<c:out value="${agact.ppr}" />
but I want to use it into input form like this:
<form:input type="text" value="${agact.ppr}" path="ppr" />
but it's not working I don't know why. I displayed others attributes in input fields without problem and this one no.
My class is like this :
Class Agent{
int ppr;
/** setters and getters**/
}
Try removing the 'value' attribute, 'path' should be all you need there. Also make sure the getter and setter are valid.

javax.validation.Valid with Spring MVC

I am using #Valid for validation during a form submission and encountered error that a property is null. I have a Country class which have properties such as code, name and createdBy all with their predefined values. When user load the page the loaded property on the form are only code and name. After user changes the value and perform submission the value of createdByis null thus validation fails. I am thinking of storing createdBy value in a hidden field on html so the validation should pass. In view of security I think anyone can actually modify the hidden field value. Is there a better approach ? Will OpenSessionInViewFilter helps in any way ?
UPDATED
Sorry that I forgot to mention, the data the browser gets from the server is in form of JSON (List of json object). Before form submission a wrote a JavaScript to populate the form based on the chosen object (with ID) within a html table. So what I send to the server is basically
<input type="hidden" name="id"/>
<html:field name="code" labelCode="label.code" type="text"/>
<html:field name="name" labelCode="label.name" type="text"/>

How to Get an id in jsp instead of value?

how to get button id from jsp to servlet instead of getting the button value
<input id="${section.id}" type="submit" name="submit" value="Edit">
how to get that id in servlet?
You can't that id is for client side use only. You will need to set the name or value to match the id of the element.
Alternatively as a workaround you could create a hidden input field that contains the id value by adding something like this to your JSP:
<input type="hidden" name="submit_id" value="${section.id}" />
This will then be available in the servlet upon form submit under the submit_id parameter.
String submitId = (String)request.getParameter("submit_id");
The only way you would be able to do that is by intercepting the form submit using javascript and setting the id as an extra post/get parameter.
the only way is make a javascript function that change your button value for the id , but i dont know why you want to do that, you could use a hidden input to send the data in the form
<input type="hidden" name="id" value="the_id_number" />
You cannot get any button id value to servlet. When a request from browser is submitted, all the input fields(input tag) will be transferred to server.The value of each input attribute can be accessed using the name of that field.All other fields like id, class etc are used for css and JavaScript functionalists.You should not design to pass the button id to server side.Think of other methods like hidden input fields

Retrieving inputvalue from JSF-repeat

I have recently come across a question involving JSF and Javabeans.
To get a value from an input you need a declared property with getter and setter in the bean. Now I am trying to generate a list of entities to edit a value (in this code example called number), but how can i retrieve these values (as the ID of h:inputText is empty at this point)?
I have tried to name this ID "name_#{bean.id}", but there is no way to set this value in the bean.
Any help would be appreciated!
index.xhtml
<ui:repeat var="entity" value="#{bean.getEntities()}">
<p>
Value:
<h:inputText id="" value="#{entity.number}" />
</p>
</ui:repeat>
Bean.java
public List<Entity> getEntities() {
return entities;
}
You don't need the client id of the inputText to get the values to the server. As stated in Daniel's comment changes are saved if you submit the surrounding form.
You don't even need to set the id parameter. JSF does it for you.
But your value attribute is not correct. It needs to be
value="#{bean.entities}"

Categories