Spring MVC: Difference between spring:bind and form:form - forms

I am looking to find out the difference between the spring:bind and form:form tag libraries when submitting a form.
A snippet of my JSP is as follows:
<form:form modelAttribute="testRulesForm">
....
<td>
<form:checkbox path="rules[${counter.index}].isActive" value="rules[${counter.index}].isActive"/>
</td>
<td>
<form:select path="rules[${counter.index}].leftCondition.name">
<form:options items="${testRulesForm.ruleAttributes}" itemLabel="name" itemValue="name" />
</form:select>
</td>
<td>
<form:select path="rules[${counter.index}].operator">
<form:options itemLabel="operator" itemValue="operator" />
</form:select>
</td>
....
Seeing as I have my path variable specified and this will be bound to my modelAttribute, does this mean that I do not need spring:bind?
Thanks

Normally you don't need to use <spring:bind> if you already use form taglib.
They do basically the same with respect to model attributes, but tags from form taglib also generate HTML form markup, whereas with <spring:bind> you need to generate markup yourself.
The following code with form tags:
<form:form modelAttribute = "foo">
<form:input path = "bar" />
</form:form>
is similar to the following code with <spring:bind>:
<spring:bind path = "foo">
<form method = "get">
<spring:bind path = "bar">
<input name = "bar" value = "${status.displayValue}" />
</spring:bind>
</form>
</spring:bind>
<spring:bind> is useful when you need something customized, that cannot be achieved by form taglib.

Related

Powermail: create fields from entries in database

My problem:
i need a matrix of fields in Powermail:
product_1 - price_1 - number_1
product_2 - price_2 - number_2
product_3 - price_3 - number_3
and so on. No problem to create this fields manually, but i need it derived from a database. The numbers of lines depends on the number of entries in the database.
is there a possibility to create fields "on the fly", perhaps by typoscript or a userfunc?
Thanks!
I would create a new field type an call it (e.g.) productsheet. In the manual there is an example how to do it: https://docs.typo3.org/typo3cms/extensions/powermail/ForDevelopers/AddNewFields/Index.html
Here two example page TSConfig lines for the new field:
# Add new fields
tx_powermail.flexForm.type.addFieldOptions.productsheet = Product Fields
tx_powermail.flexForm.type.addFieldOptions.productsheet.dataType = 1
Here is an example Productsheet.html partial file for this:
{namespace vh=In2code\Powermail\ViewHelpers}
<h2><vh:string.escapeLabels>{field.title}</vh:string.escapeLabels><f:if condition="{field.mandatory}"><span class="mandatory">*</span></f:if></h2>
<table>
<thead>
<tr>
<th scope="col">Menge</th>
<th scope="col">Artikel Nr.</th>
<th scope="col">Bezeichnung</th>
<th scope="col">Preis Fr./m</th>
</tr>
</thead>
<tbody>
<f:for each="{0:1,1:2,2:3,3:4,4:5,5:6,6:7,7:8,8:9,9:10}" as="key">
<tr>
<td>
<f:form.textfield type="number" class="mdl-textfield__input " name="field[{field.marker}][amount_{key}]" value="" />
</td>
<td>
<f:form.textfield class="mdl-textfield__input " name="field[{field.marker}][article_no_{key}]" value="" />
</td>
<td>
<f:form.textfield class="mdl-textfield__input " name="field[{field.marker}][description_{key}]" value="" />
</td>
<td>
<f:form.textfield class="mdl-textfield__input " name="field[{field.marker}][price_{key}]" value="" />
</td>
</tr>
</f:for>
</tbody>
</table>
Next step would be to insert fields - as you wrote - on the fly. So what about inserting an own viewhelper instead of defining a hardcoded array in the
Now you could prefill the fields with value="" by your own.
Hope that helps
You can use the TypoScript field of powermail to generate code from typoscript.
You can also use your own field type like discribed here with page TSConfig:
tx_powermail.flexForm.type.addFieldOptions.new = New Field
# The label could also be written with LLL: to localize the label
# Example to grab a value from locallang.xml or locallang.xlf
#tx_powermail.flexForm.type.addFieldOptions.new = LLL:EXT:ext/Resources/Private/Language/locallang.xlf:label
# Tell powermail that the new fieldtype will transmit anything else then a string (0:string, 1:array, 2:date, 3:file)
# Example for dataType array
#tx_powermail.flexForm.type.addFieldOptions.new.dataType = 1
# The new field is not just a "show some text" field. It's a field where the user can send values and powermail stores the values?
# You can tell powermail that this new field should be exportable in backend module and via CommandController
#tx_powermail.flexForm.type.addFieldOptions.new.export = 1
newis the field identifier. Powermail search by default for a partial with the identifier name e.g. New.html.
Now you can use a ViewHelper to get the data and create the html for the fields.

GetVMImageUrl doesn't work when Overriding

I want to override in a ViewModel the way is showed a picture.
According to the DevelopmentView:
Picture for foto2
<div ng-show="ViewModelRoot.VM_Status.vmEditAlumno_foto2_Visible">
<table>
<thead>
</thead><tbody>
<tr>
<img ng-src="{{StreamingViewModelClient.GetVMImageUrl(ViewModelRoot.Curr_vmEditAlumno(), "foto2")}}" img-responsive class="vmImage" />
</tr><tr>
<input type="file" onchange="angular.element(this).scope().onFileSelect('vmEditAlumno.foto2', angular.element(this).scope().ViewModelRoot.Curr_vmEditAlumno().VMClassId)" id="vmEditAlumno.foto2" ng-show="ViewModelRoot.VM_Status.vmEditAlumno_foto2_Enabled" />
</tr>
</tbody>
</table>
</div>
I set "Content Override" in the column and create a AngularUIOverride tag with value "Foto2Alumno.cshtml".
Not it gets the new file "Foto2Alumno.cshtml", as if I change something in that file it is showed, but the picture from the command GetVMImageUrl
isn't show
What am I missing?
Thanks
It was a problem with double quotes ("). I changed them to single quote (') and now the image is shown.
Wrong:
GetVMImageUrl(ViewModelRoot.Curr_vmEditAlumno(), "foto2")
Right:
GetVMImageUrl(ViewModelRoot.Curr_vmEditAlumno(), 'foto2')

web2py: multiple forms on one page

I am trying to make a form which shows all products from a group in a list. They can be given a quantity and added to a quote. Which is then stored in the database.
None of the automagical form options are working for me. So I've made each row showing information for a given product with the Quantity box and an add item button it's own form. But the loop which makes each form is doing something strange.
Controller:
products = db(db.product.group_id == productgroupnumber).select()
forms=[]
for product in products:
form = FORM(TABLE(TR(TD(product.productname),
TD((product.purchasecost or 0)),
TD((product.monthlycost or 0)),
TD(INPUT(_type='number', _name='quantity')),
TD(INPUT(_type='submit', _value=T('Add to Offer')))
)
)
)
forms.append(form)
session.quotedproducts = []
if form.accepts(request, session, keepvalues = True):
product = db(db.product.id == product_id).select().first()
offeritem = [product_id, request.vars.quantity, product.purchasecost, product.monthlycost]
session.quotedproducts.append(offeritem)
response.flash = T("Item added to offer")`
For 2 rows. The View has the below 2 forms, with only one hidden div with the formkey and formname. So I can't name the forms in order to process them properly:
<form action="#" enctype="multipart/form-data" method="post">
<table>
<tr>
<td>Block of 10 Phone Numbers</td>
<td>19.0</td>
<td>0</td>
<td><input name="quantity" type="number" /></td>
<td><input type="submit" value="Add to Offer" /></td>
</tr>
</table>
</form>
<form action="#" enctype="multipart/form-data" method="post">
<table>
<tr>
<td>100 Block of Phone Numbers</td>
<td>149.0</td>
<td>0</td>
<td><input name="quantity" type="number" /></td>
<td><input type="submit" value="Add to Offer" /></td>
</tr>
</table>
<!--Why is there only one of these??-->
<div style="display:none;">
<input name="_formkey" type="hidden" value="b99bea37-f107-47f0-9b1b-9033c15e1193" />
<input name="_formname" type="hidden" value="default" />
</div>
</form>
How do I give the forms individual names (preferably product.id)?
I tried adding the formname argument:
form.accepts(request, session, formname=product.id)
But this only names one form and the other is still named 'Default'.
In your code, you create multiple forms in the for loop, but after exiting the loop, you call form.accepts(). At that point, the value of form is the last form created in the loop, so only that form is processed.
Note, when a form is initially created, the form.accepts (or the preferred form.process) method adds the _formname and _formkey hidden fields to the form (these are used for CSRF protection). When that same method is called after form submission, it additionally handles form validation. So, given your workflow, you must process all the forms both at creation and submission. Maybe something like this:
products = db(db.product.group_id == productgroupnumber).select()
forms = []
for product in products:
quantity_name = 'quantity_%s' % product.id
form = FORM(TABLE(TR(TD(product.productname),
TD((product.purchasecost or 0)),
TD((product.monthlycost or 0)),
TD(INPUT(_type='number', _name=quantity_name)),
TD(INPUT(_type='submit', _value=T('Add to Offer')))
)
)
)
if form.process(formname=product.id, keepvalues=True).accepted:
offeritem = [product.id, form.vars[quantity_name],
product.purchasecost, product.monthlycost]
session.quotedproducts.append(offeritem)
response.flash = T("Item added to offer")
forms.append(form)

Unable to return to the desired URL or view of the JSP

<script type="text/javascript" src="${pageContext.request.contextPath}/resources/js/jquery.validate.min.js"></script>
<script>
function setHiddenVal(){
var goAhead = true;
var myVal="I am hidden value";
document.getElementById("secretValue").value = myVal;
if (goAhead == true) {
document.forms["register-form"].submit();
}
}
</script>
</head>
<body>
<!--Main Container Starts here-->
<div class="main_container">
<div class="header">
<div class="right_panel">
<h2 align="center"><u>User Master</u></h2>
<div class="top-form">
<div>
**<form:form action="/usermaster" modelAttribute="CustomerForm" id="register-form" method="POST">**
<table cellspacing="0" cellpadding="0" border="" class="form1">
<tr>
<td class="label">Name:</td>
<td>
<form:input path="firstname"/>
</td>
</tr>
<tr>
<td class="label">Password:</td>
<td>
<form:input path="password"/>
</td>
</tr>
</tbody>
</table>
<div>
<table>
<tr>
<td> </td>
<td>
<input type="button" class="btn blue px16" value="Search" />
<input type="button" name="submit" id="btnsubmit" value="Submit" onclick="setHiddenVal();"/>
<input type="button" class="btn blue px16" value="Clear" />
<input type="button" class="btn blue px16" value="Change Password" />
<input type="button" class="btn blue px16" value="Manage User Notification Profile" />
</td>
</tr>
</table>
</div>
</form:form>
</div>
</div>
<div class="clear"></div>
</div>
</div>
</div>
</body>
</html>
so above one is my code for jsp and below is the code of controller
#RequestMapping(value={"/usermaster" }, method = RequestMethod.POST)
public final String addUserMaster(#ModelAttribute("CustomerForm") CustomerForm pricing, Map<String, Object> map,
Model model, HttpServletRequest request) {
System.out.println("the first name is "+pricing.getFirstname());
System.out.println("the password is "+pricing.getPassword());
return "usermaster";
}
#RequestMapping(value={"/showusermaster" }, method = RequestMethod.GET)
public String showPage(ModelMap model){
model.addAttribute("CustomerForm", new CustomerForm());
return "usermaster";
}
But my page gets open up using a popup with the url:
C:\Users\ganganshu.s\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\YW6383E8\usermaster
so it should open like
http://localhost:8080/enbee/usermaster
Could you please tell me what should I put in the form action.. as I think some mistake is there in the form action does in spring MVC we put the action like in the case I mentioned above.
Spring confg file is given below :
<mvc:interceptors>
<bean class="com.enbee.admin.interceptor.AuthenticationInterceptor" />
<!-- Declare a view resolver-->
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" p:order="1" />
and the jsp name is usermaster.jsp
and in the sidemenu.jsp I have changed to this :
<li>User Master</li>
Change the method parameter of RequestMapping annotation to RequestMethod.POST:
#RequestMapping(value="/usermaster", method = RequestMethod.POST)
public final String addUserMaster(...){
...
}
so that when you submit your form to the URL /usermaster using method="POST" this method will get executed.
You also need to have a method (mapped to an URL) that will show this page to the user. You can use a method as below for this:
#RequestMapping(value = "/showusermaster", method = RequestMethod.GET)
public String showPage(ModelMap model){
model.addAttribute("CustomerForm", new CustomerForm());
return "usermaster";
}
With this method in place, the URL
http://localhost:8080/enbee/showusermaster
will show the usermaster.jsp page to the user. Now when you submit this form, the above addUserMaster method will be invoked.
You don't have to create new jsp file. The url /showusermaster will return the usermaster.jsp to the user, where the user can add form values and submit the form:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
...
<c:url var="submitUrl" value="/usermaster">
<form:form id="form" action="${submitUrl}" modelAttribute="CustomerForm" method="POST">
Now when the user clicks on the submit button, this form will be submitted to /usermaster URL and will be handled by addUserMaster method.
Try to specify the content type returned by your controller method, adding produces = "text/html" param to your #RequestMapping annotation.

MVC Form in MasterPage not working

Ok, This is a bit confusing and frustrating.
Here's what I have in my masterpage :
<%using (Html.BeginForm("Index", "SearchController", FormMethod.Post, new { #name = "dosearch" }))
{%>
<input type="text" name="ssearch" class="search"><input id="Image1" type="image" runat="server" src="~/App_Themes/DefaultTheme/images/btn_search.gif" width="74" height="29" style="border:none" />
<%} %>
But the corresponding generated html is:
<form action="" method="post" name="dosearch">
<input type="text" name="ticketSearch" class="search"><input src="../App_Themes/DefaultTheme/images/btn_search.gif" name="ctl00$Image1" type="image" id="ctl00_Image1" width="74" height="29" style="border:none" />
</form>
Question is WHY action is empty whereas I have mention action and controllername when declaring the HTML.Helper ????
as a reasult, the seach is not working for obvious reason.
Please help. Thanks.
I think your problem is that you are using "SearchController" but you only need to use "Search" as the MVC framework will add the word "controller".
Also, not that it matters but you are missing a closing input tag