简体   繁体   中英

Why does my Java program ignore my List in another html?

So my problem is that my List, to which I am adding a new record provided by the user, is ignored by my program when called upon.

This is the first html. The user provides me with their income, type of contract and their age:

@RequestMapping(value = "/hello", method = RequestMethod.POST)
    public String initialData(@RequestParam() double incomeParameter,
                              @RequestParam() String contractParameter,
                              @RequestParam() int ageParameter
    ) {
        if (!contractParameter.equals("Contract work") &&
                !contractParameter.equals("Contract of mandate") &&
                !contractParameter.equals("Contract of employment")) {
            return "firsterror";
        } else {
            User newUser = new User();
            newUser.setIncome(incomeParameter);
            newUser.setTypeOfContract(contractParameter);
            newUser.setAge(ageParameter);
            service.getUsersListCommon().add(newUser);

            return "secondpage";
        }
    }

Unless the contractParameter is one of the three mentioned in the code, he gets the first error. This works fine.

Then, I am trying to create a List to store the data provided by the user (income, contract, age). This is being done, because I will need this data for my method to work in the next page.

The List in which I am storing that initial data is located in the Service class and looks like this (not sure if the setter is even relevant, I am not using it anywhere):

List<User> usersListCommon = new ArrayList<User>();

    public List<User> getUsersListCommon() {
        return usersListCommon;
    }

    public void setUsersListCommon(List<User> usersListCommon) {
        this.usersListCommon = usersListCommon;
    }

It is @Autowired into my Controller class as service , hence you see service.getUsersListCommon() .

Then the user pushes Submit button and they are directed to another HTML in which below should happen:

@RequestMapping(value = "/secondpage", method = RequestMethod.POST)
    public String detailedData(@RequestParam() int amountParameter,
                               @RequestParam() int timeParameter
    ) {
        User finalUser = new User();
        for (int i = 0; i < service.getUsersListCommon().size(); i++) {
            finalUser.setIncome(service.getUsersListCommon().get(i).getIncome());
            finalUser.setTypeOfContract(service.getUsersListCommon().get(i).getTypeOfContract());
            finalUser.setAge(service.getUsersListCommon().get(i).getAge());
        }
        if (finalUser.getAge() < 18) {
            return "tooyoung";
        } else if (finalUser.getIncome() < 2000) {
            return "toopoor";
        } else if (amountParameter > 10000) {
            return "toomuch";
        } else if (timeParameter > 35) {
            return "toolong";
        } else if (finalUser.getIncome() >= 2000 &&
                finalUser.getTypeOfContract().equals("Contract work") &&
                finalUser.getAge() <= 50 &&
                amountParameter <= 10000 &&
                amountParameter > 0 &&
                timeParameter <= 35) {
            return "granted";
        } else if (finalUser.getIncome() >= 2000 &&
                finalUser.getTypeOfContract().equals("Contract mandate") &&
                finalUser.getAge() <= 50 &&
                amountParameter <= 10000 &&
                amountParameter > 0 &&
                timeParameter <= 35) {
            return "granted";
        } else if (finalUser.getIncome() >= 2000 &&
                finalUser.getTypeOfContract().equals("Contract of employment") &&
                finalUser.getAge() <= 50 &&
                amountParameter <= 10000 &&
                amountParameter > 0 &&
                timeParameter <= 35) {
            return "granted";
        } else {
            return "requirementsnotmet";
        }
    }

Now, here the user is supposed to give me two more types of information - the amount of money they want as a loan and the desired length of time of the loan.

Don't mind the conditions being all the same for every type of contract, I didn't bother with it so far, due to it being a cosmetic thing at the moment.

My main problem is that when the user provides this additional data and presses Submit ... There is this error: There was an unexpected error (type=Bad Request, status=400). Required double parameter 'incomeParameter' is not present

So my program doesn't acknowledge that the previous data was provided and stored in the List which I am calling upon at the beginning of the second mapping method.


Also, not sure if relevant, but the first page of my program looks like this in the browser: http://localhost:8082/hello

But when pushing the button in my html:

<body>
<form method="post">
<div id="header">Welcome to a swift creditworthiness' check.</div>
<div class="subheader">Please, provide all the required data below and push the button below to see the result.</div>
<input type="text" name="incomeParameter" placeholder="Your monthly income">
<input type="text" name="contractParameter" placeholder="Kind of contract">
<input type="text" name="ageParameter" placeholder="Your age">
<input type="submit" value="Calculate">
</form>
<div class="questions">In case of any questions, please proceed here:</div>
</body>

... it does redirect me to the next page:

<body>
<form method="post">
<div id="header">Please, provide additional required data below:</div>
<input type="text" name="amountParameter" placeholder="Desired amount of the loan">
<input type="text" name="timeParameter" placeholder="Desired time of the loan">
<input type="submit" value="Calculate">
</form>
</body>

But instead of looking like this: http://localhost:8082/secondpage , it still is http://localhost:8082/hello .

I think this is rather a long post, so thank you for dedicating the time to listen to my problems. :)

In your "form" tags I can't see the "action" attribute.

Without "action", when you submit a form, the next web call is itself.

So, if you are in hello page and you press submit, you go (again) to hello page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM