《web应用技术》第三次课后练习

发布于:2024-04-15 ⋅ 阅读:(168) ⋅ 点赞:(0)

实验目的:

1、springboot入门程序撰写并启动。(参考B站黑马视频Day04-10. Web入门-SpringBootWeb-快速入门_哔哩哔哩_bilibili

2、使用postman练习参数的获取。(参考B站黑马视频)

3、体会前端页面向后端发送数据的过程。并且自己尝试将之前的注册页面的信息发送到服务端。

(1)、product.html的操作代码,输入产品名称和价格,点击“增加商品”按钮,页面返回“ok”。服务台返回用户输入的值。相关页面如下:

注意,这里action的属性值需要根据情况变更不同的值

相关代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body style="font-size: 30px">
<form action="addProduct">

  产品名称 :<input type="text" name="name" value=""><br />
  产品价格: <input type="text" name="price" value=""><br />

  <input type="submit" value="增加商品">

  <br><br><br><br>test2,addProduct1 ,productResult.html
</form>
</body>
</html>
package edu.wust.pojo;

public class Product {

    private String name;
    private float price;


    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public float getPrice() {
        return price;
    }
    public void setPrice(float price) {
        this.price = price;
    }



}
package edu.wust.controller;
import edu.wust.pojo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import edu.wust.pojo.Product;

import javax.servlet.http.HttpServletRequest;

@RestController
public class ProductController {

    @RequestMapping("/addProduct1")
    public String simpleParam1(HttpServletRequest request) {
        String name = request.getParameter("name");
        String ageStr = request.getParameter("price");
        int price = Integer.parseInt(ageStr);
        System.out.println("addProduct1:"+name + "  :  " + price);
        return "OK";
    }

    @RequestMapping("/addProduct")
    public String simpleParam(String name , Integer price){
        System.out.println("您输入的信息是:"+name+"  :  "+price);
        return "OK";
    }

    @RequestMapping("/addProduct2")
    public String simpleParam2(Product product){
        System.out.println(product);
        return "OK";
    }

}

(2)、自己尝试将之前的注册页面的信息发送到服务端。