简体   繁体   English

如何从 JSP 表单获取表单数据并将其发送到数据库控制器和 RestController 并将其显示回另一个 JSP?

[英]How to get form data from JSP forms and send it to database controller and RestController and display it back in another JSP?

I am doing a spring boot project where I have to use JSP, Controller and RestController along with SQL database.我正在做一个 spring boot 项目,我必须在其中使用 JSP、Controller 和 RestController 以及 SQL 数据库。 When I try to map the POST method from JSP into a controller, either it is showing null value or showing error.当我尝试将 POST 方法从 JSP 映射到控制器时,它要么显示空值,要么显示错误。

the jsp page is: jsp页面是:

    <h3>To add an Item:</h3>
    <form action="additem" method="post">
        Enter Color  name: <input type="text" name="color"><br>
        Enter Description: <input type="text" name="description"><br>
        Enter Price: <input type="number" name="price"><br>
        Enter Shoe Name: <input type="text" name="shoe_name"><br>
        Enter Size: <input type="text" name="size"><br>
        Enter User Group: <input type="text" name="user_group"><br>
        <button>Submit</button>
    </form>

The controller is:控制器是:

    @RequestMapping(value="/additem", method=RequestMethod.POST)
    public String insertItem(Model model, @RequestParam(value = "shoe_name") String shoename, ShoeData data) {
        data = resource.createShoe(shoename, data);
        model.addAttribute("totalShoes", data);
        return "warehouse";
    }

the restful controller is:宁静的控制器是:

@RestController
public class RestfulResource {

    @Autowired
    private ShoeService shoeService;

    @PostMapping(path = "/{shoename}/shoelist")
    public ShoeData createShoe(@PathVariable String shoename, @Valid 
   @RequestBody ShoeData data) {
        ShoeData saveData = shoeService.insertData(data);
        return saveData;
    }

the JSP page to display the result:显示结果的 JSP 页面:


    <c:forEach var="item" items="${totalShoes}"> 

            <tr>
                <td>${item.id}</td>
                <td>${item.color}</td>
                <td>${item.description}</td>
                <td>${item.price}</td>
                <td>${item.shoeName}</td>
                <td>${item.size}</td>
                <td>${item.userGroup}</td>              
            </tr>
            
    </c:forEach>

the error is:错误是:

There was an unexpected error (type=Bad Request, status=400).出现意外错误(类型=错误请求,状态=400)。 Validation failed for object='shoeData'. object='shoeData' 的验证失败。 Error count: 1 org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'shoeData' on field 'price': rejected value [];错误计数:1 org.springframework.validation.BindException:org.springframework.validation.BeanPropertyBindingResult:1 个错误字段“price”上的对象“shoeData”中的字段错误:拒绝值[]; codes [typeMismatch.shoeData.price,typeMismatch.price,typeMismatch.int,typeMismatch];代码 [typeMismatch.shoeData.price,typeMismatch.price,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [shoeData.price,price];参数 [org.springframework.context.support.DefaultMessageSourceResolvable: codes [shoeData.price,price]; arguments [];参数 []; default message [price]];默认消息[价格]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'price';默认消息 [无法将类型为“java.lang.String”的属性值转换为属性“price”所需的类型“int”; For input string: ""]对于输入字符串:""]

the restcontroller is working fine and able to POST data through advanced REST client. restcontroller 工作正常,能够通过高级 REST 客户端发布数据。 But, I am not able to get data from form into the database and display it in the JSP page as it is showing the above error.但是,我无法将数据从表单获取到数据库中并将其显示在 JSP 页面中,因为它显示了上述错误。 How can I get ShoeData entity inside the controller method?如何在控制器方法中获取 ShoeData 实体?

I solved it on my own.我自己解决了。

The controller class is:控制器类是:

    @RequestMapping(value="/additem", method=RequestMethod.POST)
    public String insertItem(Model model, @RequestParam(value = "shoe_name") String shoename, ShoeData data) {
        List<ShoeData> convertedData = new ArrayList<ShoeData>();
        convertedData.add(data);
        System.out.println(data.getClass());
        model.addAttribute("totalShoes", convertedData);
        return "warehouse";
    }

Restful controller class is as below: Restful 控制器类如下:

@PostMapping(path = "/{shoename}/shoelist")
    public ShoeData createShoe(@PathVariable String shoename, @Valid @RequestBody ShoeData data) {
        ShoeData saveData = shoeService.insertData(data);
        return saveData;
    }

I still got error that there is type mismatch between the price variable in model class which was int but the method was trying to covnert to String which caused the error, and so I changed the type of the price variable to String for now.我仍然得到错误,模型类中的价格变量之间存在类型不匹配,它是 int 但该方法试图转换为导致错误的字符串,所以我暂时将价格变量的类型更改为字符串。 I will identify the root cause and update soon.我将确定根本原因并尽快更新。

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

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