<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...">
Related
My Java/Springboot/Thymeleaf app has a form called direct bind. On this form there is a modal that pops up when you click a button, in this modal you can attach a file. The submit button to the modal, does not seem to be registering or sending to the #Requestmapping. Any idea why and how to fix this?
I've tried setting the controller up like this instead #RequestMapping(value="/attachDoc", params="document"). I've also tried adding formnovalidate="formnovalidate" on the submit input. As well as moving it to the same controller file as the original form. Not sure what else to try.
HTML:
<div id="modal" class="modal" data-izimodal-title="Upload a Document">
<div id="newRequiredDocForm">
<form enctype="multipart/form-data" th:action="#{/attachDoc}" method="post" th:object="${newDocument}">
<div class="row">
<div class="col-xs-4 col-sm-3 text-right"><label class="modalLabel">Type:</label></div>
<div class="col-xs-4 col-sm-3 text-right">
<label class="modalLabel">File:</label>
</div>
<div class="col-xs-8 col-sm-7">
<input type="file" name="document" multiple="multiple" style="margin-right:-20px;"/>
</div>
</div>
<br/><br/>
<div style="text-align: right;"><input type="submit" name="attachNewDoc" value="Submit" class="btn btn-docModal"/></div>
</form>
</div>
</div>
Controller:
#RequestMapping(value="/attachDoc")
public String attachDoc(Model model, #ModelAttribute(value="newDocument") Document newDocument, #RequestParam("document") MultipartFile file){
Document doc=documentRepository.save(newDocument);
doc.setStorage(storageService.store(file));
documentRepository.save(doc);
return "directBind";
}
Define method=RequestMethod.POST in your mapping.
By default if you are not specifying any method in request mapping then it will be GET.
You are using post method in your thymeleaf but the mapping in the controller is for GET(By default)
So you have to change requestmapping like below.
#RequestMapping(value="/attachDoc",method=RequestMethod.POST) by specifying POST method.
I'm using thymeleaf for my web application. There is a problem about save and update functions. When I want to save a campaign from ui, the campaign object fields come null to rest controller class. This problem just occure in weblogic server(12.1.3). When I try it in tomcat server, do not occure any error.
my edit and create page as follow. There are a few fields for campaign, but I wrote some of them in here. Bytheway, I am sure that all fields ready in the html page. Some of them hidden, some of them visible.
<div class="row">
<form name="Form" class="col s8 offset-s2" id="upload-file-form"
enctype="multipart/form-data" th:object="${campaign}"
th:action="#{/admin/getCampaign}" onsubmit="return validateForm()"
method="post">
<div class="row">
<input type="hidden" th:field="*{id}"/>
<input type="hidden" th:field="*{version}"/>
</div>
<div class="row">
<div class="input-field">
<input id="brandname" type="text" class="validate" th:field="*{brandname}">
<label for="brandname">Brand Name</label>
</div>
</div>
</form>
</div>
#RequestMapping(value = "admin/getCampaign", method = RequestMethod.POST)
public String uploadingPost(#RequestParam("uploadingFiles") MultipartFile[] uploadingFiles,
#RequestParam("uploadingFiles1") MultipartFile[] uploadingFiles1,
#RequestParam("uploadingFiles2") MultipartFile[] uploadingFiles2,
#RequestParam("uploadingFiles3") MultipartFile[] uploadingFiles3,
Campaign campaign) throws IOException {
/** this is my controller method for save or update.
*/
}
in weblogic server campaign parameter fields come null (as a new object), but in tomcat server, everything is normal.
UPDATE:
I changed my ui fields to value like this post. But the problem continue.
<input type="hidden" th:value="*{id}"/>
Your form enctype is "multipart/form-data".
So you must add spring.http.encoding.enabled=false to application.properties.
It should be <input type="hidden" th:field="*{id}"/>not th:value
My index.jsp is as following:
<form action="FileUploadServlet" id="formSubmit" method="post" enctype="multipart/form-data">
<input type="text" id="txtFileName" value="${fname}"/>
<input type="file" name="fileName" id="selectedFile" style="display: none;">
<input type="button" id="btnBrowse" value="Browse..." onclick="document.getElementById('selectedFile').click();" />
<input type="submit" value="Upload" id="btnUpload">
</form>
with value=${fname} is get from dopost method in servlet when form summited
request.setAttribute("fname", fileName);
getServletContext().getRequestDispatcher("/index.jsp").forward(
request, response);
But it's weird that when I deployed index.jsp
My text field always show ${fname} in text area, even after form submitted, its still get that value (correctly it must show 'filename')
Does anyone meet this problem like me?
First thing is that, you are using wrong syntax to display value in JSP.
Below syntax is wrong.
value="${fname}"
We use expression tag fordisplaying value in JSP page. Expression tag convert into Java statement.
<%=request.getAttribute("fname")%>
When you first time open your index.jsp page, it will show you blank value and when request will come back from server side, then it will show you correct result.
Use below code.
<%if(request.getAttribute("fname")!=null){%>
<input type="text" value ="<%=request.getAttribute("fname") %>"/>
<%}else
{ %>
<input type="text"/>
<%} %>
In JSP, the correct syntax to retrieve the values is
<%=request.getAttribute("fname")%>
This is known as assignment OR expression tag (<%=%>).
Try this ...
<form action="FileUploadServlet" id="formSubmit" method="post" enctype="multipart/form-data">
<input type="text" id="txtFileName" value="<%=request.getAttribute("fname")%>"/>
<input type="file" name="fileName" id="selectedFile" style="display: none;">
<input type="button" id="btnBrowse" value="Browse..." onclick="document.getElementById('selectedFile').click();" />
<input type="submit" value="Upload" id="btnUpload">
</form>
More related to this, here.
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"/>
in my jsp, when i navigate through tab key it skips my Save and Reset button. and move to next component in my page. even though i have not used tabindex in my jsp files. please suggest what could be the reason. thanks
code is like:
<div>
<input type="hidden" name="_flowExecutionKey" value="${flowExecutionKey}"/>
<input type="submit" class="button" name="_eventId_search" value="Search"/>
<span class="save_button_common" onClick="submitForm('save')">Save</span>
<span class="reset_button_common" onClick="submitForm('reset')">Reset</span>
</div>
May be because it is not input type , you can explicitly try setting tabIndex
Try:
<div>
<input type="hidden" name="_flowExecutionKey" value="${flowExecutionKey}"/>
<input type="submit" class="button" name="_eventId_search" value="Search"/>
<input type="button" class="save_button_common" onClick="submitForm('save')">Save</input>
<input type="button" class="reset_button_common" onClick="submitForm('reset')">Reset></input>
</div>
Note if it's a form you need input type="button" rather than just using <button> which you can otherwise just for straight links that don't submit a form. This is particularly relevant with Internet Explorer that treats the button tag somewhat differently to other browsers (submitting the "value=" rather than the enclosed text or something like that)