I'm creating a form to allow users to edit my objects, but it seems that only the object fields that are editable in the form are available within my controller once the form is submitted. My example is a user object that I present in a form to allow the user to edit the name field, when they submit the form my controller needs to determine if this is a new user or an exiting user. To determine this I check the ID field, but since the ID field is not editable on the form it is null, when I check it in the controller.
Have I missed something?
For this scenario, keep your id has hidden input in template. So that it will be available for you in controller after user hitting form's submit button.
<input type="hidden" name="id" value='#userForm.field("id").value' >
Related
I'm working on a website in which users can add and edit things (it's not relevant describing what these things are in particular).
I'm implementing it using Thymeleaf for the frontend, Spring MVC for the backend and JPA for the database logic. Now I'm trying to implement the edit logic but I don't know which is the best way to do it.
What I'm thinking is: display to the user all the input fields (on a HTML page) that he is allowed to edit, already filled in with the current values. He can edit then whatever fields he wants and finally press the edit button to persist the changes.
Once I get the new object in the backend, I retrieve the old version from the database in order to check which field the user changed. For every field that got changed I update the old version and only when I've finished I call the JPA method save and I persist the new version of that object.
Is there a better way to do it?
It would be perfect if the object I put inside the model in order to display all its field to the user inside the HTML page, could mantain all the information of the old object and not just the ones the user can change. Let me explain better what I'm trying to say with an example:
Let's say the object we are trying to edit its called Person and has these attributes:
id
name
surname
money
nickname
sex
But the user can edit only the following attributes:
money
nickname
sex
so the controller which handler the get request of the page would look like this:
#GetMapping("person/{personId}/edit")
public String getEditPersonPage(#PathVariable Integer personId, Model model) {
Person person = personService.getById(personId); //person has all the attributes filled in
model.addAttribute("person", person);
and the controller which handler the put request looks like this:
#PutMapping("person/{personId}/edit")
public String editPerson(#ModelAttribute Person person, #PathVariable Integer personId){
personService.editPerson(person); //person has only the three fields filled in and all the other attributes as NULL
return "redirect:/person/" + personId;
}
the HTML page:
<form th:object="${person}" th:method="PUT">
<fieldset>
<p th:text="*{id}"></p>
<p th:text="*{name}"></p>
<p th:text="*{surname}"></p>
<input type="number" th:field="*{money}" th:value="*{money}" />
<input type="text" th:field="*{nickname}" th:value="*{nickname}" />
<input type="text" th:field="*{sex}" th:value="*{sex}" />
<input type="submit" value="Edit" />
</fieldset>
</form>
When I insert the object person inside the model to render the HTML page the object has all the attributes. Indeed I can decide which of its attributes display inside the HTML page (in this case only three: money, nickname and sex). But when the user press the submit button (edit), I receive only the fields displayed inside the HTML page (money, nickname, sex). So what I have to do is: I have to take these three fields, check if they are changed and if so, update the old version.
It would be perfect if when the user press the submit button, all the fields of person (and not just those three he is allowed to update) could be retrieved by the controller. In that case I could skip the check phase and persist directly the new version inside the database (with the old values unchanged and not set to NULL).
Any thoughts?
You can use readonly attributes for the ones that are not going to be edited.
I think is the best way in order to keep all the values visible in the form to give all the info to the user that is editing. All the values will be submitted and serialized with your object, but the form just allows to edit the three of them that you need.
A readonly element is just not editable, but gets sent when the according form submits. A disabled element isn't editable and isn't sent on submit.
I am using a Spring form in my application:
<form:form class="form-horizontal" method="POST" commandName="basic-contact-information">
In the controller, if I find that the form data is invalid, how do I have the form filled back in with the data the user put into the form when I send the user back to the form? Do I need to set it as an attribute with the name 'basic-contact-information' or do I need to set a value attribute on each of the form elements in the JSP? I'm trying to avoid forcing the user to start over with a blank form.
I have two different forms to register users and an action, form 1 is used by Users of type A and form 2 is used by Users of type B. Therefore when they get to index they will choose the form based on their type so I do not have any option to figure out their type, the only way is to receive the request and find out which form has sent the request to the action.
I need some hints on how can I find out which form has sent the request to the action.
jsp file of UserA
<s:form name="form1" action="register">
.... registration form for User A .....
</s:form>
jsp file of UserB
<s:form name="form2" action="register">
.... Registration form for User B .......
</s:form>
please note the forms are different
action
public String Register() {
if (request is from form1)
{
.....
}
if (request is from form2)
{
....
}
}
Some options:
Put a hidden value in the form.
Look at the session user to determine their type.
Separate logic appropriately and have separate action methods.
Assuming they already have Sessions, you can use
ActionContext.getContext().getSession().getId()
in the Struts2-Bean to identify the Session.
From now, you can differ the user1's actions and user2's actions using the Session-ID;
I need to post a text-field value to server but i have not placed the text-field in side the form tag.Here is the details of my use-case
i have an anchor tag like
LOGIN
This anchor tag is not inside any form tag and i need to send one extra value to the server and don't want that value to append as query string.
I have created a hidden filed and have provided the required value to that hidden field, but when i click on the Login link and its getting to my Controller class this hidden field value is not available.
Is there any way to send that value to server side class as a request parameter?
You can use ajax to do that, i suggest to use Jquery
$.post('loginhandle', {username:$('#username').val(), password: $('#password').val()} function(){});
By using Javascript get value from hidden fields like this
<script>
var name= document.getElementById("login").value
document.getElementById("topage").innerHTML='LOGIN'
</script>
<input type="hidden" name="name" value="ashraf" id='login'>
<div id='topage'>
LOGIN
</div>
You're doing a get rather than a post. You could append to he querystring as this works with get.
Get the hidden field value using javascript before the form is submitted. Use
document.getElementById("hiddenID").value; Append the value obtained in the URL before the form is submitted. The value should be there in the server.
Regards,
Ajai G
Context : One of the application has a xhtml form where a text area would be rendered as disabled field.
Problem : And when user submits the form, the converter associated with that field doesn't get invoked, but works fine when the text area is not disabled.
Is this how JSF request/response life cycle works or, am I missing something ?
Pseudo Code:
<tag:field id="xyz" label="abc" value="#{something.something}"
disaply="mutable" required="false" styleClass="Text_Area"
disabled="#{somethingelse.something}" rows = "4" cols="50"
converter="freeTextConverter">
</tag:field>
As per HTML specification, the values of disabled input fields are not sent along with the form submit. So JSF simply got null as value and there's nothing to convert. You perhaps want to use readonly instead. This way the value is not editable, but it will be sent along with the form submit. You only need to take into account that the client can still tamper the request and edit the value before it get sent. You'd perhaps want to store the value in a view scoped bean instead and keep the field disabled.