I have a problem in my app. I have a form in which I'm editing some fields from databases.
And 'time' field is always 'not in line' with the rest. Problem is with 'form:input path="time"' mark. When I change 'time' to 'date' or 'description' everything is nice. Here is image how it looks like:
And here is part of my jsp file:
<form:form method="post" commandName="note" action="update_note">
<form:hidden path="note_id"/>
<div id="vertical">
<div id="edit">
Date
<form:input path="date"/>
</div>
<div id="edit">
Time
<form:input path="time"/>
</div>
<div id="edit">
Description
<form:input size="30" path="description"/>
</div>
</div>
</div>
Time in database is TIME type and in java class I'am mapping it to java.sql.Time object. Any ideas what can be wrong with this 'time' in input path?
Put the size attribute on input field time and date
<form:input size="20" path="date"/>
<form:input size="20" path="time"/>
Related
I'm developing a webApp in java, maven, JPA and thymeleaf and I have this problem:
I have this method in my controller:
#RequestMapping(value = "/admin/searchResults", method = RequestMethod.GET)
public String listResults(#ModelAttribute Step step,Model model,#PathVariable("dateStart") Date dateStart,#PathVariable("dateEnd") Date dateEnd){
model.addAttribute("dateStart", dateStart);
model.addAttribute("dateStart", dateEnd);
return "thisAreTheDates";
}
And this is a fragment of the view that in theory has to send the 2 date parameters to my fancy controller:
<div class="container">
<form class="form-inline" data-th-action="#{/admin/searchResults}" method="get" >
<div class="input-group">
<div class="form-group">
<label>From:</label>
<input type="date" id="dateStart" class="form-control"/>
<label>To:</label>
<input type="date" id = "dateEnd" class="form-control"/>
</div>
</div>
<br/>
<button type="submit" class="button">Generate Report</button>
</form>
</div>
Everything goes so good, until I go to that view, fill the fields with 2 dates and when I press the button of generate report, this error appears:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun May 28 10:54:07 BOT 2017
There was an unexpected error (type=Internal Server Error, status=500).
Missing URI template variable 'dateStart' for method parameter of type Date
In your HTML, you need to add name="dateStart" and name="dateEnd" to each of your dateStart and dateEnd input fields, respectively. This will allow your form connect to your model attribute.
Additionally, you will want to add a method to your controller annotated with #PostMapping to actually handle what you want to be done with your form input.
Finally, you will not want to use get on your HTML form but more likely, post.
So:
#GetMapping("/admin/searchResults") //use the shorthand
public String listResults(#ModelAttribute("step") Step step,
#PathVariable("dateStart") Date dateStart,
#PathVariable("dateEnd") Date dateEnd,
Model model) {
Step step = new Step(); //or however you are creating this
model.addAttribute("step", step);
model.addAttribute("dateStart", dateStart);
model.addAttribute("dateStart", dateEnd);
return "thisAreTheDates";
}
Your Post Mapping:
#PostMapping("/admin/searchResults") //use the shorthand
public String postResults(#ModelAttribute("step") Step step,
Errors result) {
//validate your step bean
someValidator.validate(step, result);
//persist it, do whatever
someService.save(step);
return "confirmationPage";
}
Updated HTML:
<div class="container">
<form class="form-inline" th:object="${step}" th:action="#{/admin/searchResults}" method="post" >
<div class="input-group">
<div class="form-group">
<label>From:</label>
<input type="date" name="dateStart" id="dateStart" class="form-control"/>
<label>To:</label>
<input type="date" name="dateEnd" id="dateEnd" class="form-control"/>
</div>
</div>
<br/>
<button type="submit" class="button">Generate Report</button>
</form>
</div>
This is an editing form that will have all the fields filled out using existing data from database. Regular text input fills out just fine but I am having issue doing the same with datetime.
<sf:form class="form-horizontal" role="form" modelAttribute="sighting" action="edit" method="POST">
<div class="form-group">
<label for="Reported Time" class="col-md-4 col-sm-4 col-xs-12 control-label">
Reported Time:
</label>
<div class="col-md-4 col-sm-6 col-xs-12">
<sf:input type="datetime-local" class="add-form form-control" path="sightingDate" name="sightingDate"/>
</div>
</div>
Use the input type date instead of datetime-local since you don't have a timestamp part.
Modify your controller to populate your date in YYYY-MM-DD format
(e.g. "2016-11-11") in the sightingDate attribute. You can't
specify the date in any other format for an HTML5 date input type. You might want to make sightingDate as String so that you can store date in this format.
Use value attribute,
<input type="datetime-local" name="sightingDate" value="sighting.sightingDate">
I want to insert an Attribute into the html Code.
I tried this, but it's not working:
<div id="${var}"> ... </div>
I think you know what I mean. The attribute 'var' should be the id. I didn't find a solution...
You just need to use the th:attr attribute. It is explained in the reference documentation 5.1:
5.1 Setting the value of any attribute
Enter then the th:attr attribute, and its ability to change the value
of attributes of the tags it is set in:
<form action="subscribe.html" th:attr="action=#{/subscribe}">
<fieldset>
<input type="text" name="email" />
<input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/>
</fieldset>
</form>
The
concept is quite straightforward: th:attr simply takes an expression
that assigns a value to an attribute. Having created the corresponding
controller and messages files, the result of processing this file will
be:
<form action="/gtvg/subscribe">
<fieldset>
<input type="text" name="email" />
<input type="submit" value="¡Suscríbe!"/>
</fieldset>
</form>
Use this
<div th:attr="id=${var}"> ... </div>
Thymeleaf only evaluates attributes that are prefixed with th:. Here is a list of the attributes that are evaluated:
http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#setting-value-to-specific-attributes
In your case, th:id is already built in so you can simply do <div th:id="${var}"> ... </div> and it will work. th:attr, is used to define attributes that thymeleaf doesn't normally support.
<center>
<form class="form-horizontal" action="${pageContext.servletContext.contextPath}/LargeLandmarkListGet" method="post">
<div class="form-group">
<label class="control-label col-sm-2" for="SLLRID">SLLRID:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="SLLRID" placeholder="Enter SLLRID...">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>
</center>
That is the particular definition of the action in the JSP.
String SLLRID = request.getParameter("SLLRID");
This is how I am trying to get it in the servlet, within the doPost method. However, SLLRID is always null. I've tried everything I can think of but I cannot get it to return the inputted value. However, if I hardcode the value, everything works perfectly, so I think there is something wrong when communicating between the JSP and the servlet.
What I Have Tried:
Changing the name in for in the label definition, so that id and for are named differently
Adding an id value to the button
Hardcoding the value to verify the servlet
Trying a get rather than a post (though that seemed wildly inappropriate)
Testing other servlets with the same JSP (this worked, though not with this particular submission id)
Ensuring that everything that needed entries in web.xml had said entries made
The form sends the data based on the name attribute. So instead put: <input type="text" class="form-control" id="SLLRID" name="SLLRID" placeholder="Enter SLLRID...">
I have the following page and it does not load
<form:form action="/test.htm" method="post" commandName="demoForm" >
<div id="testSection" style="margin-top: 1.5%;margin-left: 3.5%;">
<span class="test-container">
<label>UserName</label>
<span class="test-container-right">
<form:input path="username" value="${UNAME}" class="text simpleTextField" maxlength="200" style="width:60%" disabled/>
</span>
</span>
<span style="width:auto; padding-left: 30%; padding-bottom: 4%; text-align:center; float:right; clear:both;">
<input type="submit" class="btn" style="width:auto;" value="Save" />
</span>
</div>
</form:form>
but when I change it to
...
<span class="test-container-right">
<form:input path="username" />
</span>
...
it works and loads correctly. Why am I not allowed to set html properties for form:input spring tag. How can I achieve this? On inspecting the element it expands to be
<input id="username" type="text" value="" name="username"></input>
I need to populate its value as well as provide it with a class and additional attributes like width.
Ok I resolved some part of it. Looks like tags used inside form:input tag are different than regular html tags. All of them are listed here. For ex style is cssStyle.
So I change my code to
<form:input path="username" cssClass="text simpleTextField" maxlength="200" cssStyle="width:60%" disabled="true"/>
and now it works..
I still don't know how to populate value in this input. These seems no equivalent of value keyword.
#Aniket You actually have an equivalent , consider in a case you have to populate the select box with the values from the model attribute . You can make use of items attribute.
For instance ,
<tr>
<td>City :</td>
<td><form:select path="city" items="${cityList}" /></td>
</tr>
It will generate the select with the the list of objects.
cityList here refers the object that has been sent from the server side.
Hope this helps !