简体   繁体   English

为什么我的 Java 程序在另一个 html 中忽略我的 List?

[英]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.这是第一个 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.除非contractParameter是代码中提到的三个之一,否则他会得到第一个错误。 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 位于 Service 类中,看起来像这样(不确定 setter 是否相关,我没有在任何地方使用它):

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() .它是 @Autowired 作为service进入我的 Controller 类,因此您会看到service.getUsersListCommon()

Then the user pushes Submit button and they are directed to another HTML in which below should happen:然后用户按下提交按钮,他们被定向到另一个 HTML,其中应该发生以下情况:

@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).我的主要问题是,当用户提供此附加数据并按提交时...出现此错误:发生意外错误(类型=错误请求,状态=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另外,不确定是否相关,但我的程序的第一页在浏览器中看起来像这样: http://localhost:8082/hello

But when pushing the button in my html:但是在我的 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 .但不是像这样: http://localhost:8082/secondpage ,它仍然是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.所以,如果你在 hello 页面并按下提交,你会(再次)进入 hello 页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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