Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java MVC

  1. #1
    Junior Member
    Join Date
    Dec 2021
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java MVC

    Hi, need help, the site is not finding city and date information. Why

    @Controller
    @RequestMapping("/secondSubPage")
    public class secondSubPageController {
        private static List<Travel> travels = new ArrayList<>();
     
        public static void main(String[] args) {
            Travel Wroclaw = new Travel("Wroclaw", "01-02-2001");
            Travel Krakow = new Travel("Kraków", "01-02-2001");
            Travel JeleniaGora = new Travel("Jelenia Góra", "01-02-2001");
            Travel Zgorzelec = new Travel("Zgorzelec", "01-02-2001");
            Travel Luban = new Travel("Lubań", "01-02-2001");
            Travel Lodz = new Travel("Lódz", "01-02-2001");
            Travel Karpacz = new Travel("Karpacz", "01-02-2001");
            Travel ZielonaGora = new Travel("Zielona Góra", "01-02-2001");
            Travel Katowice = new Travel("Katowice", "01-02-2001");
            Travel Miedzyzdroje = new Travel("Miedzyzdroje", "01-02-2001");
            travels.add(Wroclaw);
            travels.add(Krakow);
            travels.add(JeleniaGora);
            travels.add(Zgorzelec);
            travels.add(Luban);
            travels.add(Lodz);
            travels.add(Karpacz);
            travels.add(ZielonaGora);
            travels.add(Katowice);
            travels.add(Miedzyzdroje);
     
            SpringMvcApplication.run(secondSubPageController.class, args);
        }
     
        private Travel findTravel(String city){
            for (Travel e : travels){
                if (e.getCity().equals(city)){
                    return e;
                }
            }
            return null;
        }
        @GetMapping()
        public List<Travel> getTravels(){
            return travels;
        }
     
        @GetMapping("/city")
        public Travel getTravelCity(@RequestParam(value = "city") String city) {
            return findTravel(city);
        }
     
        @GetMapping("/date")
        public List<Travel> getTravelDate(){
            return null;
        }
     
        @GetMapping("/cityAndDate")
        public Travel getTravelCityAndDate(@RequestParam(value = "city") String city,
                                           @RequestParam(value = "date") String date){
            return findTravelCityAndDate(city, date);
        }
     
        private Travel findTravelCityAndDate(String city, String date){
            for (Travel e : travels){
                if (e.getCity().equals(city) && e.getDate() == date){
                    return e;
                }
            }
            return null;
        }
    }


    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
     
    <p th:text="'Data: ' + ${date}" />
    <p th:text="'Miasto: ' + ${city}" />
    </p>
    <table>
        <tr>
            <th>City</th>
            <th>Date</th>
        </tr>
        <tr>
            <td><p th:text="${city}"/></td>
        </tr>
        <tr>
            <td><p th:text="${date}"/></td>
        </tr>
    </table>
    </body>
    </html>


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java MVC

    I don't know anything about the Spring framework. Try asking the question on this forum: http://www.coderanch.com/forums

    Posted at: https://coderanch.com/t/749193/java/MVC
    If you don't understand my answer, don't ignore it, ask a question.