What are spring.status.value|expression|errorMessages? - spring

I'm new to Freemarker and am trying to incorporate it with Spring. I see references to spring.status.expression and spring.status.value and spring.status.errorMessages, but no where have I found a description of what they are.
I'm not sure if they're specific to forms, but that is where I find the references. Example:
<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>
All help appreciated!
Thanks.
Les

Yes, these are related to forms and field errors. It's exactly like the Spring binding in JSP (just better!): https://docs.spring.io/spring/docs/4.3.x/spring-framework-reference/html/spring-tld.html#spring.tld.bind

Related

Get all content of POST HTML form in Gin

I've an HTML form:
<body>
<div>
<form method="POST" action="/add"name="submitForm">
<label>Message</label><input type="text" name="message" value="" />
<input type="checkbox" name="complete" value=""> Complete<br>
<input type="submit" value="submit" />
</form>
</div>
</body>
Now I want to access both values (message and complete) at the same time when a POST occurs.
How can I do this in Go (gin gonic)?
I can do it in two times by using c.PostForm but is there a way to collect them both?:
func AddTodoHandler(c *gin.Context) {
fmt.Println(c.PostForm("message"))
fmt.Println(c.PostForm("complete"))
I tried fmt.Println(c.PostForm("submitForm")) but that did not work.
I don't know if you've already solved this, but the way I managed to capture the 2 values simultaneously was by removing the value from the checkbox and using the Request.PostForm instead of the PostForm.
I tried using the form's name or id, but it seems that it's still not possible to get the data from them.
<body>
<div>
<form method="POST" action="/add" name="submitForm"> << not work
<label>Message</label><input type="text" name="message" value="" />
<input type="checkbox" name="complete"> Complete<br> << remove value attribute
<input type="submit" value="submit" />
</form>
</div>
</body>
func AddTodoHandler(c *gin.Context) {
fmt.Println(c.Request.PostForm)
Note: as my reputation is less than 50pts, I cannot comment on the post.

not able to click on button where xpath is showing correct as per selenium IDE 2.9.1

I need to write xpath for 'New' button.
have tried:-
By.xpath("//form[#action='/intruvert/action/CustomRolesAction']/div/div[3]/div/a[1]")
By.xpath("//form[#name='CustomRolesForm']/div/div[3]/div/a[#style='background-color: rgb(242, 242, 242);' and #title='New']")
By.xpath("//form[#name='CustomRolesForm']/div/div[3]/div/a[#title='New']")
but not able to click on New button.
I am not sure here if i missing anything.
kindly suggest.
here is the code:-
<html>
<head>
<body>
<div class="bodywrap">
<form action="/intruvert/action/CustomRolesAction" method="post" name="CustomRolesForm">
<input type="hidden" value="CustomRolesDetails_t" name="userAction"/>
<input type="hidden" value="fullaccess" name="accessRight"/>
<input type="hidden" value="" name="uuid"/>
<input type="hidden" value="true" name="fromTab"/>
<input type="hidden" value="" name="selectedDomain"/>
<input type="hidden" value="/My Company:0/Manager:0" name="resourceName"/>
<input type="hidden" value="MANAGER" name="topMenuName"/>
<input type="hidden" value="Users and Roles" name="secondMenuName"/>
<input type="hidden" value="Roles" name="thirdMenuName"/>
<input type="hidden" value="/My Company:0" name="domainName"/>
<input type="hidden" value="/My Company:0" name="currentDomainName"/>
<input type="hidden" value="/Manager:0" name="shortResourceName"/>
<input type="hidden" value="false" name="ucaplModeEnabled"/>
<table width="100%" cellspacing="0" cellpadding="0" border="0" align="center">
<div class="gensecholder clearfix">
<div class="gensecheader clearfix">
<div class="gensecbody pad10all clearfix">
<div class="gensecfooter clearfix">
<div class="gensecfootright">
<a class="genericbtn" title="New" href="javascript:doSubmit('add')">New</a>
</div>
#Pranay after viewing your code, i got javascript error. inside your html you have not defined anywhere to javascript. see the image
Your html
<a class="genericbtn" title="New" href="javascript:doSubmit('add')">New</a>
Modified html
<a class="genericbtn" title="New" href="#">New</a>
Now use xpath method using javascript executor.
WebElement new_button = driver.findElement(By.xpath("//a[contains(text(), 'New')]"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", new_button);
OR
driver.findElement(By.xpath("//a[contains(text(), 'New')]")).click();
You can try to locate the button directly without ancestors
// by class name
By.className("genericbtn")
// by title
By.cssSelector("[title='New']")
// by partial href
By.cssSelector("[href*='add']")
//a[.="New"]
Just use text find a tag.
. means current context node(a).
You can just use either title or text property to find out the required link:
By.xpath("//a[#title='New']");
Or
By.linkText("New");
Or
By.xpath("//a[text()='New']");

Populating a DropDown Menu From a MySQL Database Using Hibernate in Spring MVC

I've looked into many sources to find the exact answer that I need, but unfortunately, I couldn't understand or they never hit the exact spot that I need.
Basicly, I am developing a spring-mvc web application and I am going to allow user to add post to the website. While adding this post, he/she is going to identify some features of the post, and one of the feature is category. Everything is working fine but category. I've tried to implement this category field with a dropdown menu, but I get nothing populated in dropdown when I run the project.
Here is my jsp page:
<%#taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%#include file="/WEB-INF/views/template/header.jsp" %>
<div id="page">
<div id="main">
<div class="row">
<div class="three columns"></div>
<div class="six columns">
<form:form action="${pageContext.request.contextPath}/addPost" method="post" commandName="post"
enctype="multipart/form-data">
<div class="form-group">
<label>Kategori</label>
<form:select path="category" id="category">
<form:option value="NONE" label="--- Seçiniz ---"/>
<form:options items="${category}"></form:options>
</form:select>
<!--select name="category">
<option name="category_id" value="0">Seçiniz</option>
</select-->
</div>
<div class="form-group">
<label>İlan Başlığı</label> <form:errors path="postTitle" cssStyle="color: #ff0000"/>
<form:input path="postTitle" id="title" class="form-Control"/>
</div>
<div class="form-group">
<label>İlan Açıklaması</label>
<form:input path="description" id="description" class="form-Control"/>
</div>
<!--div class="form-group">
<label>Etiketler</label>
<input type="text" class="asdasd" name="title" placeholder="İlanınız ile ilgili etiketler" required>
</div-->
<div class="form-control">
<label>Fiyat</label> <form:errors path="price" cssStyle="color: #ff0000"/>
<form:input path="price" id="price" class="form-Control"/>
</div>
<div class="form-group">
<label>Lokasyon</label>
<form:input path="postAddress" id="address" class="form-Control"/>
</div>
<div class="form-group">
<label class="control-label" for="postImage">Fotoğraf Yükle</label>
<form:input name="file" path="postImage" id="postImage" type="file" class="form:input-large"/>
</div>
<div class="form-group">
<button type="submit" value="submit" class="btn btn-send-message pull-right">İLAN OLUŞTUR</button>
</div>
</form:form>
</div>
<div class="three columns"></div>
</div>
</div>
And here is my Controller function:
#RequestMapping(value= "/addPost", method = RequestMethod.GET)
public String addPost(Model model) {
Post post = new Post();
post.setActive(true);
List<Category> category = categoryService.getAllCategories();
model.addAttribute("category", category);
model.addAttribute("post", post);
return "addPost";
}
What I want to do here is, I want to get the necessary data from the database table that I've created by the help of Hibernate, and send it to dropdown menu.
Many examples here in SO and some other blogs shows that feature like filling the dropdown menu from the controller by manually. This is the one that I don't want. So far, the closest answer that I can find is this. But since I am new in Spring, I couldn't get it.
If anyone can even point me in the right direction on how to set this up that would be a great help.
Thanks in advance.
From what I can see, it looks like you have two model attributes, the post and list of categories. The JSP has a form, this form is bound to the post bean, and you want to have the list of categories appear in a dropdown on the form.
As far as I know, and google this is not possible, as long as you have two separate model attributes. The problem is that <form:select path="category"..>
is scoped by your commandName bean, so it is looking for category as a property on post, and not in the view model.
I think you need to make a new class FormBean which has two properties, category and post, and then bind that to the from using commandName="formBean", and then use path="post.postTitle" for a single property on the post object, and <form:select path="category"...> for the dropdown.
Hope this helps

Is it possible to trigger different HTTP methods on a single page with only one form on it?

Until now, I have dealt with PUT and DELETE HTTP methods with jQuery AJAX (or XMLHttp) and HiddenHttpMethodFilter including GET and POST in Spring (currently I'm working with Spring 3.2.0). I have one general question about using these methods.
Basically, I want to perform basic database operations like INSERT, EDIT, UPDATE and UPDATE on the same page (JSP) using a single form. In this scenario, is it possible to trigger these methods at appropriate actions?
For example, when a user presses the delete button, the DELETE method should be raised, when he presses the update button, the PUT method should be triggered, on pressing the insert button, the POST method should be invoked and while retrieving data, the GET method should be used and accordingly an appropriate method which is mapped in the controller should be invoked (again on the same page with a single form <form:form>...</form:form>).
Of course, it is possible by means of AJAX but using AJAX everywhere is not appropriate.
Maybe I'm missing some important aspects of RESTFul APIs. Apparently, it is not possible (unknowingly). What is the actual answer?
EDIT:
Suppose, I wanted to deal with the PUT method then the Spring form would look like the following.
<form:form id="mainForm" name="mainForm" method="put" action="Temp.htm" commandName="tempBean" enctype="multipart/form-data">
<input type="submit" id="btnSubmit" name="btnSubmit" value="Submit"/>
</form:form>
and its generated HTML would look like the following.
<form id="mainForm" name="mainForm" action="Temp.htm" method="post" enctype="multipart/form-data">
<input type="hidden" name="_method" value="put"/>
<input type="submit" id="btnSubmit" name="btnSubmit" value="Submit"/>
</form>
The hidden <input type="hidden" name="_method" value="put"/> field is automatically added to be dealt with the PUT method by the HiddenHttpMethodFilter
If I now needed to raise the DELETE method then the form and its generated HTML should respectively be modified as follows.
<form:form id="mainForm" name="mainForm" method="delete" action="Temp.htm" commandName="tempBean" enctype="multipart/form-data">
<input type="submit" id="btnSubmit" name="btnSubmit" value="Submit"/>
</form:form>
Generated HTML
<form id="mainForm" name="mainForm" action="Temp.htm" method="post" enctype="multipart/form-data">
<input type="hidden" name="_method" value="delete"/>
<input type="submit" id="btnSubmit" name="btnSubmit" value="Submit"/>
</form>
How likely is it to do so dynamically at run time?
You could do something like:
<form id="mainForm" name="mainForm" action="Temp.htm" method="post" enctype="multipart/form-data">
<input type="hidden" name="_method" value="delete"/>
<input type="submit" name="btnUpdate" value="Update"/>
<input type="submit" name="btnDelete" value="Delete"/>
<input type="submit" name="btnCreate" value="Create"/>
</form>
Then on your processing page just check to see which button submitted the form.

MVC (in SItefinity)

In my View I have something like:
#model CreatorModel
<form name="myForm" action="/Creator/CreateAction" enctype="multipart/form-data" id="myForm" method="post">
<div class="a">
<h2 class="a">....</h2>
<div class="b">
<br />
Select a file for a <input type="file" name="aFile" id="aFile" />
<br /><br />
Select a file for b<input type="file" name="bFile" id="bFile" />
<br /><br />
<input type="submit" id="CreateAction" name="CreateAction" value="CreateAction" />
</div>
</form>
But I have an error : File /Creator/CreateAction is not found (CreateAction is an action from CreatorController)
What can I enter as action in <form...> in order to find action from controller?
When I'm using this syntax:
#using (Html.BeginForm("FileUpload", "Board", FormMethod.Post, new { enctype = "multipart/form-data" })) { <input type="file" /> <input type="submit" /> }
I also have an error: A reference is not set to an instance of an object"
Maybe there is a bad routing?
when using MVC widgets in Hybrid mode (on a webforms template) you need to use a special version of BeginForm (because webforms only allows one form on the page).
#Html.BeginSitefinityForm()
should be the form you use to define a form area in your widget. Replace the regular one with that and the widget should work as expected.
I hope this is helpful!

Resources