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.
Related
I have to write a small message board as a homework and struggle with a, as I believe, really easy task:
The user is clicking on "Answer" below an article, which is bringing him to the answer.jsp?id=123. There is an input form for his text which submits his entry to a saveanswer.jsp.
The answer.jsp does a mySQL query do retrieve some data, and here do I need to make a "if-then-else" and (I believe) store the result in a variable to use it for the submit-form.
answer.jsp?id=123
<sql:query dataSource="${snapshot}" var="result">
SELECT * from forum where id=<%= request.getParameter("id") %>;
</sql:query>
<c:forEach var="row" items="${result.rows}">
<input type="text" name="title" value="RE: <c:out value="${row.title}"/>"/> <br/>
<textarea name="answertext" cols="50" rows="5"></textarea> <br/>
<input type="hidden" name="answer_id" value="<%= request.getParameter("Id")%>">
There is a field answer_id in the database, and I need to check:
if answer_id == 0;
then
answer_id=id;
else
answer_id_answer_id //do nothing
The result should then be the value of the hidden "answer_id" field.
Unfortunatly, I was not able to get an if-else with the db data or a variable declaration working :/
I tried for 2 days now, you're my last hope.
Thanks for reading, I really would appreciate your help!
Best regards,
DataSlayer
If I understand your question, you are looking to set the value of the hidden input depending on some values, not sure where answer_id is coming from but you want to use a simple condition, even better a ternary condition
${empty value? other:value}
In this simple line, value and other are both variable, if value is empty, this insert other. Update this to your case, since I don't really know where answer_id come from (the DB but from which table)
SQL INJECTION
As RC point it out, you are open to SQL injection with your query since you add directly the parameter into a query.
Someone could set the parameter as
0; drop table forum
This will result in a query like :
SELECT * from forum where id=0; drop table forum;
Dropping your table.
Never concat parameter receive from the client to you query, you never know what it can be. If this is from you, you should be able to trust yourself but the client, NEVER!
To prevent it, use the parameters system provided (never used it but should work)
<sql:query dataSource="${snapshot}" var="count">
SELECT * from forum where id=?
<sql:param value="${ request.getParameter("id") }" />
</sql:update>
This will prevent the injection to happened.
So I have a .jsp page which has a form on it, like this (naturally this is a massive simplification):
<form:form commandName="myCommand" method="post">
<form:select path="values" class="select-tall" multiple="multiple" id="mySelect">
<option>first</option>
<option>second</option>
<option>third</option>
</form:select>
<button type="submit" class="button">Save</button>
</form:form>
When the submit button is pressed, the form is submitted(somehow) and the path= attributes are used to bind the data inside the form elements to the properties of an instance of a plain old java object. I understand how this POJO is specified, and I understand how the POST request is routed to the correct controller, but I don't understand where or how the form values are mapped to the given POJO.
What I don't understand is:
How does the spring tag library modify the form such that this binding takes place?
How would one go about doing this in a manual or ad-hoc fashion(using a Javascript onSubmit() method, say)?
Essentially my question is: How does the spring-form.tld tag library work?
Any resources, or even a high-level explanation in your own words, would be extremely helpful.
I've implemented a work-around solution in the mean time (dynamically adding items to a hidden form element), but I feel like this is hack-y and the wrong solution.
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
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}"
I have a spring MVC application using JSP as my view technologies with Jquery for AJAX. I have a table such as the following:
<table>
<tr>
<td>name1</td>
<td>value1</td>
<td>setting1</td>
</tr>
<tr>
<td>name2</td>
<td>value2</td>
<td>setting2</td>
</tr>
</table>
I need to serialize this table so that it can later be bound to an object in my controller. However the jquery serialize() method only works on form fields. What would be the best approach to get the table data into the HTTP request so that I can later bind it to a java object?
EDIT:
I have a java object that has a collection so
class MyOject {
private List<AnotherObject> items = new ArrayList<AnotherObject>();
// standard getters and setters
}
class AnotherObject {
private name;
private value;
private setting;
// getters and setters
}
In the screen the user is creating new items on the fly. When the user is done, they submit the form and then I need to process all the items in the list and instantiate a new collection with those items.
For display purposes I am creating a new table row when an item is created.
The <Form> tag is how you tell the browser "Put this stuff in the web request." That's how you get object binding in Spring. What is your reason for not using a Form? You don't necessarily have to put it in a form in the page, you could give your table elements IDs and fetch their contents in the javascript if you really needed to.
Edit: I think maybe it's hard to answer because it's not clear why you want the browser to give you back things that you gave it in the first place. Maybe what you really need is the #SessionAttributes() annotation on your controller so that you can preserve State of the original page shown to the user?
More Edit:
kk, see now. If what you want is Spring web data binding then create a form in parallel as you add more table rows. e.g.,
<form id="myObject" action="whateverYouNeedHere.htm" method="post">
<input type="hidden" id="items[0].name" name="items[0].name" value="foo"/>
<input type="hidden" id="items[0].value" name="items[0].value" value="bar"/>
<input type="hidden" id="items[0].setting" name="items[0].setting" value="buzz"/>
<input type="hidden" id="items[1].name" name="items[1].name" value="foo"/>
<input type="hidden" id="items[1].value" name="items[1].value" value="bar"/>
....
Then just submit that and it will bind right on for you. If you did mean to handle the content yourself, then you probably could use XHR as someone else mentioned.
Use the Spring Data Binding and Validation API to bind it into a Java object of your own design. That documentation is web-agnostic; check out the later chapter to see how the web tier leverages it.
You'll want an abstraction beyond a table, I presume.
in order to stick your table information into a java object, you will first need to send it to the server.. for that you will need to either send it via XHR or in a form.
in order to serialize the object you will need to write some javascript/jquery.
i could write it for you, but your requirements are somewhat vague when it comes to how your table will look, nor do i want to guess about what the java object you want to add your data to looks like.