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

本文介绍了如何使用SpringBoot入门,包括创建简单和复杂的HTTP请求控制器,处理不同类型的参数如简单参数、实体参数、数组和集合、日期、JSON以及路径参数。通过实例演示了如何接收和处理表单数据。
摘要由CSDN通过智能技术生成

1、springboot入门程序撰写并启动。

主要代码:

package com.caz;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootWebQuickstartApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootWebQuickstartApplication.class, args);
    }

}

运行结果 

2、使用postman练习参数的获取。 

2.1简单参数

package com.cazrequest.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
@RestController
public class RequestController {
    @RequestMapping("/simpleParam")
    public String sompleParam(String name,Integer age){

        System.out.println(name+":"+age);
        return "OK";
    }
}

get

post

2.2实体参数

简单实体参数

package com.cazrequest.pojo;

public class User {
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

}

复杂实体参数

package com.cazrequest.pojo;

public class Address {
    private String province;
    private String city;
    public String getProvince() {
        return province;
    }

    public void setProvince(String name) {
        this.province= province;
    }

    public String getCity() {
        return city;
    }

    public void setAge(String city) {
        this.city = city;
    }

    @Override
    public String toString() {
        return "Address{" +
                "province='" + province + '\'' +
                ", city=" + city +
                '}';
    }
}

2.3数组集合参数

数组

集合

2.4日期参数

2.5json参数

2.6路径参数

多个参数

3.案例

3.1

product.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <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="增加商品">
</form>
</body>
</html>

Product

package com.cazrequest.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;
    }

}

 ProductController

package com.cazrequest.controller;
import com.cazrequest.pojo.Product;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
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";
    }

}

 3.2

form.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>表单信息获取</title>
</head>

<body>
<form action="formCatch" method="post">
    <ul style="list-style: none; line-height: 30px">
        <li>
            输入用户姓名:
            <input type="text" name="name" />
            <br />
        </li>
        <li>
            选择性别:
            <input name="sex" type="radio" value="boy" />
            男
            <input name="sex" type="radio" value="girl" />
            女
        </li>
        <li>
            选择密码提示问题:
            <select name="question">
                <option value="母亲生日">
                    母亲生日
                </option>
                <option value="宠物名称">
                    宠物名称
                </option>
                <option value="电脑配置">
                    电脑配置
                </option>
            </select>
        </li>
        <li>
            请输入问题答案:
            <input type="text" name="key" />
        </li>
        <li>
            请选择个人爱好:
            <div style="width: 400px">
                <input name="like" type="checkbox" value="dance" />
                唱歌跳舞
                <input name="like" type="checkbox" value="web" />
                上网冲浪
                <input name="like" type="checkbox" value="hill" />
                户外登山
                <br />
                <input name="like" type="checkbox" value="sports" />
                体育运动
                <input name="like" type="checkbox" value="reading" />
                读书看报
                <input name="like" type="checkbox" value="movie" />
                欣赏电影
            </div>
        </li>
        <li>
            <input type="submit" value="提交" />
        </li>
    </ul>
</form>
</body>
</html>

Form

package com.cazrequest.pojo;

public class Form {
    private String name;
    private String sex;
    private String question;
    private String key;
    private String[] like;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String[] getLike() {
        return like;
    }

    public void setLike(String[] like) {
        this.like = like;
    }
}

FormController

package com.cazrequest.controller;

import com.cazrequest.pojo.Form;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FormController {
    @RequestMapping("/formCatch")
    public String simpleParam(Form form){
        System.out.println("您输入的信息是:"+form);
        return "OK";
    }
}

运行结果 

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值