Answer the question
In order to leave comments, you need to log in
How can I implement a selection based on the data that the user enters in a JSP form?
I have an Entity (Schedule), I have a service (ScheduleService), I have a DAO (ScheduleDAO).
All functionality works, tested. And the main problem is this:
I wrote a simple JSP form where I enter the number of the station where the train is coming from, the number of the station where the train is going and the departure time. Here is the form:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Actual train traffic</title>
</head>
<body>
<form:form action="scheduleByStationsAndDate" modelAttribute="schedule">
Station departure:
<form:input path="stationDeparture"/>
Station arrive:
<form:input path="stationArrival"/>
Time departure:
<form:input path="dateDeparture"/>
<br><br>
<input type="submit" value="Submit">
</form:form>
</body>
</html>
@RequestMapping("/scheduleByStationsAndDate")
public String getScheduleByStationsAndDate(@ModelAttribute("schedule")Schedule schedule) {
...
//List<Schedule> schedules = scheduleService.getByStationsAndDate(schedule); примерно так работает сервис
return JspFormNames.SCHEDULE_INPUT_FOR_STATIONS_AND_DATE;
}
Answer the question
In order to leave comments, you need to log in
The form leaves the client using the GET or POST method, depending on what is specified in the method attribute of the form tag. The default will be GET. The form data will be sent in the URL specified in the action attribute .
In your case, the query string will look like
`scheduleByStationsAndDate/?stationDeparture="data"&stationArrival="data"` and so on.
Thus, to accept all this on the server, you need to write something like this:
@RequestMapping(value="scheduleByStationsAndDate", params=["stationDeparture", "stationArrival", "dateDeparture"], method=RequestMethod.GET)
@ResponseBody
public String getScheduleByStationsAndDate(
@RequestParam("stationDeparture") String stationDeparture,
@RequestParam("stationArrival") String stationArrival,
@RequestParam("dateDeparture") Date dateDeparture
) { ... }
@RequestMapping(value ="scheduleByStationsAndDate", method = RequestMethod.POST)
@ResponseBody
public String getScheduleByStationsAndDate(@RequestBody MyForm form) {...}
public class MyForm {
private String stationDeparture;
private String stationArrival;
private Date dateDeparture;
//тут еще геттеры/сеттеры, ну всё как мы любим
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question