i have created a java project in eclipse,in this project i have an html form customer.html as shown below
<form>
Name :<input type="text" id="name"/>
Address: <input type="text" id="add"/>
Age:<input type="int" id="age"/>
<input type="submit" onclick="what should i call here in my program"/>
</form>
once the user fills the form and submits it i want to access the form field in java code is it possible.if so how or else is there anyother solution to create an xml file for this form
<input type="submit" onclick="only Javascript function can be called here"/>
if you want to access html form elements at java code you need to submit your form at some controller or another jsp. Like..
<form name="new_employee" method="post" action="../admin/newEmployee.do">
Name :<input type="text" name="employeeName"/>
Address: <input type="text" name="add"/>
Age:<input type="text" name="age"/>
<input type="submit" value="Submit"/>
</form>
form elements can be accessed from generated request by their names like..
request.getParameter("employeeName");
Related
I have form and corresponding controller to which list of objects is passed. After that this list is redirected as an attribute to another method and displayed in a website. After that I would like to pass object from this list via form in a carSearchResult.html file to controller. I was trying to do like it is described here Spring and Thymeleaf: Sending an object to a controller from a th:each table but it does not work properly. It is sending an object to controller but with fields property and model set as blank (but they are not null).
searchCar form
<form action="#" th:action="#{/user/searchCar}" method="post">
<input type="radio" name="type" value="hatch" /> Hatchback <br/>
<input type="radio" name="type" value="sedan" /> Sedan <br/>
<input type="radio" name="type" value="combi" /> Kombi <br/>
<input type="radio" name="type" value="sport" /> Sport <br/>
<input type="radio" name="type" value="cabrio" /> Cabrio <br/>
<input type="text" name="dailyPrice" placeholder="Price" /> <br/>
<input type="date" name="dateOfRent" placeholder="Date of rent" /> <br/>
<input type="date" name="dateOfReturn" placeholder="Date of return" /> <br/>
<input type="submit" value="Show" />
</form>
Controller method for searchCar post request
#PostMapping("/user/searchCar")
public String searchCar(Model model, #RequestParam String type, #RequestParam double dailyPrice,
#RequestParam Date dateOfRent, #RequestParam Date dateOfReturn, RedirectAttributes redirectAttr) {
List<Car> allCarsList = carDao.getAllCars(type, dailyPrice, dateOfRent, dateOfReturn);
redirectAttr.addFlashAttribute("carList", allCarsList);
return "redirect:/user/carSearchResults";
}
Method where list is redirected
#GetMapping("/user/carSearchResults")
public String showCarSearchResults(Model model) {
model.addAttribute("car", new Car());
return "user/carSearchResults";
}
Form where list is displayed
<div th:each="car: ${carList}">
<h3 th:text="${car.producer}"></h3>
<h3 th:text="${car.model}"></h3>
<form action="#" th:action="#{/user/showCarDetails}" method="post" th:object="${car}">
<input type="hidden" th:field="*{producer}" /> <br/>
<input type="hidden" th:field="*{model}" /> <br/>
<input type="submit" value="Show" />
</form>
</div>
Method to which I want to send object from th:each loop
#PostMapping("/user/showCarDetails")
public String showCarDetails(#ModelAttribute Car car) {
System.out.println(car);
return "user/showCarDetails";
}
After printing information about car in console I receive object below. It looks like it is passed but with parameters as a blank space. Can anyone help me? Many thanks.
Car [id=null, producer=, model=, seatsNumber=null, type=null, registrationNumber=null, dailyPrice=null, description=null]
EDIT
I have changed the form. Option with th:attr and th:value works but I do not really understand why th:field does not work and what is the difference between them. As I understand from documentation th:value is some kind of older version of th:field?
<div th:each="car: ${carList}" th:object="${car}">
<h3 th:text="*{producer}"></h3>
<h3 th:text="*{model}"></h3>
<form action="#" th:action="#{/user/showCarDetails}" method="post">
<input type="hidden" th:value="*{producer}" name="producer" /> <br/> <!-- It works -->
<input type="hidden" th:attr="value=*{producer}" name="producer" /> <br/> <!-- It works -->
<input type="hidden" th:field="*{producer}" /> <!-- It does not work -->
<input type="submit" value="Pokaż" />
</form>
</div>
th:object works differently when you use it on a <form>, vs. when you use it other places. When you use it in a form, it expects it to resolve to a single variable on the model. You cannot use it with a temporary variable (like {$car} -- which isn't on the model, it's just a temporary variable created byth:each) because the attribute just isn't built to work like that.
th:field generates both the value and name tags -- but again, it has to work within the same limitations as th:object. The reason manually adding name and value attributes work, is because they happen to line up with what spring is expecting in a post and it's able to create the object based on the names.
As for how to solve the original problem... I think you have two options:
Create a command object which has a List of Cars as a property. That way you can use the th:object and th:field properties. You'll have to have the submit button to supply which car is being edited.
Continue manually creating the name and value fields like you already are.
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 am new to JSP/Servlet.
I want to use one servlet for handling different request sent from different JSP form/button.
For example
student.jsp =>
<form action="SingleController?action=student" method="POST">
Student ID: <input type="text" name="stid">
Name: <input type="text" name="stname">
<input type="submit" value="Submit" name="student">
</form>
teacher.jsp =>
<form action="SingleController?action=teacher" method="POST">
Teacher ID:<input type="text" name="tid">
Name: <input type="text" name="tname">
<input type="submit" value="Submit">
</form>
SingleController
How can I manage these two different requests using a single Servlet ?
use if conditions in servlet on action .
String action = request.getParameter("action");
if (action.equals("student")){
// to do rest of code..
}
else if (action.equals("teacher")){
// to do rest of code..
}
I have form as given below. Now, when I enter something in the #search_string and press ENTER button it automatically takes it to the action=Paid... I'm not sure why the form gets submitted on the ENTER button..
<!--<div id="startsearchbox">-->
<form id="bigsearchform_new" method="post" action="Paid">
<!--<label style="display:none" for="search_string">SEARCH</label>-->
<input id="search_string" name="search_string" type="text" class="startnewsearch rounded" placeholder="Search..." maxlength="500" >
<input id="searchButton1" type="button" class="searchButton" title="Click here to search the database">
<input type="hidden" name="antiCSRF" value="{{acsrf}}" />
<input type="hidden" name="session_id" value="{{session_id}}" />
<input type="hidden" name="commodity_id" id="commodity_id" />
</form>
It's default behavior of the browser to submit the form on ENTER button. If you want to prevent form submitssion use onSubmit event handler.
That is the default behavior for an HTML form, see http://www.whatwg.org/specs/web-apps/current-work/multipage/forms.html#implicit-submission
<c:forEach items="${movieList}" var="movie" varStatus="status">
<tr class="<c:if test="${status.count % 2 == 0}">even</c:if>">
<td>${movie.title}</td>
<td>${movie.genre}</td>
<td>${movie.year}</td>
<td>${movie.boxoffice}</td>
<td>
<form:form action=edit.htm>
<input type="hidden" name="edit" value="movie name">
<input type="submit" value="Edit">
</form:form>
<form:form action=delete.htm>
<input type="hidden" name="delete" value="movie name">
<input type="submit" value="Edit">
</form:form>
</tr>
</c:forEach>
This is the section of code I have at the moment. The idea is to display movie data, but also provide buttons to send the user to either a filled form page to edit the data or simply delete the respective data and redirect to the same page. I am just unsure as how to pass along the movie object. I know it is simple, but I can't find the reference I used previously...
Thanks.
U can try this :
put this object in a hidden input
<input type="hidden" id="hidden_object" value="" name="hidden_object"></input>
than you can access the value with the method getParameter()
OR
put this object in your session
in your jsp
<% session.setAttribute("movie", ${movie}); %>
good luck