SpringMVC 簡單獲取前端html輸入框數據

前端表單:

<form action="/community/hello/student" method="post">
    <p>
        請輸入姓名:<input type="text" name="name">
    </p>
    <p>
        請輸入年齡:<input type="text" name="age">
    </p>
    <p>
        <input type="submit" value="提交">
    </p>
</form>

後端控制器:

@RestController
@RequestMapping("/hello")
public class HelloController {

    @RequestMapping("/student")
    public String getStudentInfo(String name, int age) {
        System.out.println("學生姓名:" + name);
        System.out.println("學生年齡:" + age);
        return "success";
    }
}

表單還有一個 /community 前綴是因爲我在配置文件中配置了項目的前綴
在這裏插入圖片描述

啓動SpringBoot,訪問頁面:
在這裏插入圖片描述
控制檯打印結果:
在這裏插入圖片描述

項目是用 SpringBoot 構建的,html 文件位置如下:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章