I have a problem with binding my object in ftl form.
Here is my controller method:
#RequestMapping(method = RequestMethod.POST)
public String saveConfigProperties(#ModelAttribute("configCmdList") ConfigCmdList configCmdList, BindingResult bindingResult) {
configurationDao.setConfigValues(configCmdList.getConfigurations());
return "config";
}
and here is part of my ftl:
<form action="" method="POST">
<#spring.bind "configCmdList" />
<#list configCmdList.configurations as config>
${config.name}
</#list>
<input type="submit" value="submit"/>
</form>
I have an access to my list of objects which I sent previous using GET method in my ftl, but my object list is null after sending object without modifications back to controller.
I tried to bind my configCmdList.configurations and also bind separately each element of that list in loop but without success.
What I'm missing?
VairalPatel web page is down and I remember that he wrote good example about freemarker form and spring mvc.
Thanks in advance for your help.
Ok, I resolved an issue. I had to bind each list element and it parameters separately in loop using ${spring.status.expression} and ${spring.status.value}.
Here is my code:
<form action="" method="POST">
<#list configCmdList.configurations as config>
<#spring.bind path="configCmdList.configurations[${config_index}].id"/>
<input type="hidden" name="${spring.status.expression}" value="${spring.status.value}" />
<#spring.bind path="configCmdList.configurations[${config_index}].name"/>
<input type="text" name="${spring.status.expression}" value="${spring.status.value}" />
<#spring.bind path="configCmdList.configurations[${config_index}].value"/>
<input type="text" name="${spring.status.expression}" value="${spring.status.value}" />
</#list>
<input type="submit" value="submit"/>
</form>
Thank you for your help :)
This is another way to write Caro's solution:
<form action="" method="POST">
<#list configCmdList.configurations as config>
<input type="hidden" name="configurations[${config_index}].id" value="${config.id}"/>
<input type="text" name="configurations[${config_index}].name" value="${config.name}"/>
<input type="text" name="configurations[${config_index}].value" value="${config.value}" />
</#list>
<input type="submit" value="submit"/>
</form>
Related
this code sent get request, why?
<form th:action="#{/books/edit/rename}" th:object="${book}" th:method="POST">
<input type="text" id="id" name="id" th:value="*{id}" value="1"/>
<input type="text" id="bookName" name="bookName" th:value="*{bookName}" value="nameExample"/>
<button type="submit">Rename for model</button>
</form>
Try removing the th: in the method declaration, to be as the following:
<form th:action="#{/books/edit/rename}" th:object="${book}" method="POST">
…
Because it is not reading the code and the default is GET, check this for more details https://www.w3.org/TR/html401/interact/forms.html#h-17.3
Ok, so, basically I have to use an input with this value in order to sent it to a form post to delete the row I want...
<input type="text" th:field="*{id}" value="${customer.id}" />
but what happens? Well.. the property th:field="*{id}" is not doing its job, as you can see here:
That number 3 you can see is the submit button, as you see its grabbing the value correctly but doesn't matter what I do, everytime I try to delete something, it wont do, and not, is not the logic because hardcoded the id on html on some dumb input and it worked..
Here is the html code:
<form th:action="#{/DeleteCustomer}"
th:object="${customersdelete}" method="post">
<span th:value="${customer.id}"></span>
<span th:field="*{id}" value="${customer.id}"></span>
<input type="text"
th:field="*{id}" value="${customer.id}" />
<!-- <input type="text" th:value="${customer.id}" th:field="*{id}" /> -->
<input
type="submit" name="btnInsert" class="btn btn-primary"
value="Delete" th:value="${customer.id}" />
</form>
CONTROLLER
#PostMapping("/DeleteCustomer")
public ModelAndView DeletDis(#ModelAttribute("customersdelete") Customers Customers) {
ModelAndView mav = new ModelAndView();
System.err.println("ID =" + Customers.getId());
customersService.removeCustomer(Customers.getId());
customersService.listAllCustomers();
mav.setViewName("redirect:/" + MAIN_VIEW);
return mav;
}
ERROR:
ID =0
Hibernate: select customers0_.id as id1_0_0_, customers0_.address as address2_0_0_, customers0_.course as course3_0_0_, customers0_.firstname as firstnam4_0_0_, customers0_.lastname as lastname5_0_0_ from customers customers0_ where customers0_.id=?
2018-02-13 11:17:25.924 INFO 10812 --- [nio-8080-exec-9] c.p.s.component.RequestTimeInterceptor : --REQUEST URL:'http://localhost:8080/DeleteCustomer'-- TOTAL TIME: '8'ms
2018-02-13 11:17:25.927 ERROR 10812 --- [nio-8080-exec-9] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.EmptyResultDataAccessException: No class com.project.springinventory.entity.Customers entity with id 0 exists!] with root cause
org.springframework.dao.EmptyResultDataAccessException: No class com.project.springinventory.entity.Customers entity with id 0 exists!
Things that i've tried and doesn't worked for me:
<input type="text"
th:field="*{id}" value="${customer.id}" />
<input type="text"
th:field="*{id}" th:value="${customer.id}" />
<input type="hidden"
th:field="*{id}" th:value="${customer.id}" />
<= displays 0 while id is 4 or something
checked objects, checked everything. I don't know what to do.
What i've saw, if i delete th:field="*{id}" the id will load on the input but won't send into the post, tried creating another object just with diferent name in order to avoid clash of data or something and still the same..
<tr th:each="customer:${customers}">
<th><span th:value="${customer.id}"></span></th>
<th><span th:text="${customer.id}"></span></th>
<th><span th:text="${customer.firstname}"></span></th>
<th><span th:text="${customer.lastname}"></span></th>
<th><span th:text="${customer.course}"></span></th>
<th><span th:text="${customer.address}"></span></th>
<th>
<form th:action="#{/showCustomer}" th:object="${customers}"
method="post">
<button class="btn btn-warning">
<span class="glyphicon glyphicon-pencil" aria-hidden="true">Show</span>
</button>
</form>
<form th:action="#{/UpdateCustomer}" th:object="${customers}"
method="post">
<button class="btn btn-warning">
<span class="glyphicon glyphicon-pencil" aria-hidden="true">Edit</span>
</button>
</form>
<form th:action="#{/DeleteCustomer}"
th:object="${customersdelete}" method="post">
<span th:value="${customer.id}"></span>
<span th:field="*{id}" value="${customer.id}"></span>
<input type="text"
th:field="*{id}" value="${customer.id}" />
<!-- <input type="text" th:value="${customer.id}" th:field="*{id}" /> -->
<input
type="submit" name="btnInsert" class="btn btn-primary"
value="Delete" th:value="${customer.id}" />
</form>
th:field sets the name, value and id of an input tag. (Overwriting some of the values you've set manually.)
Since your th:object is defined as ${customersdelete}, the expressions *{id} means ${customersdelete.id} -- which is different than what you are trying with
th:value="${customer.id}". The expression th:value="${customer.id}" is completely ignored.
You should set the id of customersdelete in the controller before the form, and not use th:value="${customer.id}".
I would like to use spring Boot MVC with Freemarker and display a form in a similar way to how it is done with JSP tags. E.g. this form:
<form:form method="post" action="save" modelAttribute="form" class="form-horizontal">
<form:hidden path="id"/>
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<form:input id="name" path="name" class="form-control" />
</div>
</div>
</form:form>
Naturally, the tags form:form, form:input, form:hidden etc. are not supported. Is there a way to bind the model to the view in Freemarker?
Here it is:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/view.html#view-simple-binding
<!-- freemarker macros have to be imported into a namespace. We strongly
recommend sticking to spring -->
<#import "/spring.ftl" as spring />
<html>
...
<form action="" method="POST">
Name:
<#spring.bind "command.name" />
<input type="text"
name="${spring.status.expression}"
value="${spring.status.value?default("")}" /><br>
<#list spring.status.errorMessages as error> <b>${error}</b> <br> </#list>
<br>
...
<input type="submit" value="submit"/>
</form>
...
</html>
Hi I have the following function:
<script type="text/javascript" src="clientscript/vbulletin_md5.js?v=387"></script>
<form action="login.php?do=login" method="post" onsubmit="md5hash(vb_login_password, vb_login_md5password, vb_login_md5password_utf, 0)">
<input type="hidden" name="s" value="" />
<input type="hidden" name="do" value="login" />
<input type="hidden" name="vb_login_password" />
<input type="hidden" name="vb_login_md5password" />
<input type="hidden" name="vb_login_md5password_utf" />
<table cellpadding="0" cellspacing="3" border="0">
<tr>
Unfortunately the function has no id or name etc. How is it possible to click on the Javascript button and get back the result? Thanks for all your answers in advance.
Use as below
onsubmit="return md5hash(vb_login_password, vb_login_md5password, vb_login_md5password_utf, 0)"
And define md5hash into Javascript with ture/false value
I've have two forms in two jsp files. Where in I, submit the first form and save it in a the database using servlet. And then, I've go to other form to fill up the details. As in the second form I have few fields the same as first form. As I, enter the dataid in the second form automatically the first name and last name to be field matching the dataid.
how can I do this in a servlet?
<form>
Data id:<input type="text" name="dataid"><br>
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname">
<input type="submit value="submit"/>
</form>
<form>
Some id:<inut type="text" name="someid"><br>
Age:<input type="text" name="age"> <br>
Data id:<inut type="text" name="dataid"><br>
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname">
<input type="submit" type="transmit">
</form>
If you still have the parameters in the request you can simply write them in the valueof the ìnput` tag. Something like:
<form>
Data id:<input type="text" name="dataid" value="<%=request.getParameter("dataid")%>"><br>
First name: <input type="text" name="firstname" value="<%=request.getParameter("firstname")%>"><br>
Last name: <input type="text" name="lastname" value="<%=request.getParameter("lastname")%>">
<input type="submit value="submit"/>
</form>
Take a look at The Java EE 5 Tutorial