S
S
Sland Show2018-09-04 20:27:28
Java
Sland Show, 2018-09-04 20:27:28

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>


And my question is, how do I write a mapping in my controller that will pull out user-entered data?

For example, here:
@RequestMapping("/scheduleByStationsAndDate")
    public String getScheduleByStationsAndDate(@ModelAttribute("schedule")Schedule schedule) {
        ...
       //List<Schedule> schedules = scheduleService.getByStationsAndDate(schedule); примерно так работает сервис
        return JspFormNames.SCHEDULE_INPUT_FOR_STATIONS_AND_DATE;
    }


I'm not entirely sure that I have the correct mapping method in the controller. In general, the POST method should happen right there?

I'm a little confused, I'd appreciate it if you could clarify.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2018-09-05
@SlandShow

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
) { ... }

In the case of a POST request, the method might look like this:
@RequestMapping(value ="scheduleByStationsAndDate", method = RequestMethod.POST)
@ResponseBody
public String getScheduleByStationsAndDate(@RequestBody MyForm form) {...}

The main idea I think is clear. MyForm in this case is a regular POJO into which Spring will try to map the data coming from the client, in your case MyForm might look like this
public class MyForm {

    private String stationDeparture;
    
    private String stationArrival;

    private Date dateDeparture;

    //тут еще геттеры/сеттеры, ну всё как мы любим
}

Something like this ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question