diff --git a/张博凯的Java学习笔记.assets/image-20220120214323545.png b/张博凯的Java学习笔记.assets/image-20220120214323545.png new file mode 100644 index 0000000..5065fac Binary files /dev/null and b/张博凯的Java学习笔记.assets/image-20220120214323545.png differ diff --git a/张博凯的Java学习笔记.assets/image-20220120214332553.png b/张博凯的Java学习笔记.assets/image-20220120214332553.png new file mode 100644 index 0000000..c1ba7b3 Binary files /dev/null and b/张博凯的Java学习笔记.assets/image-20220120214332553.png differ diff --git a/张博凯的Java学习笔记.assets/image-20220120220526068.png b/张博凯的Java学习笔记.assets/image-20220120220526068.png new file mode 100644 index 0000000..9dce503 Binary files /dev/null and b/张博凯的Java学习笔记.assets/image-20220120220526068.png differ diff --git a/张博凯的Java学习笔记.md b/张博凯的Java学习笔记.md index 17f76b5..473016c 100644 --- a/张博凯的Java学习笔记.md +++ b/张博凯的Java学习笔记.md @@ -4351,6 +4351,8 @@ src/main/webapp/WEB-INF/web.xml ###### ajax提交 +> 略 + ##### 控制器接收前端提交的数据 @@ -4371,7 +4373,6 @@ import org.springframework.web.bind.annotation.RequestParam; @Controller @RequestMapping("/user") public class UserController { - @RequestMapping("/add") public void addUser(@RequestParam("bookName") String bookName, @RequestParam("author") String author, @RequestParam("price") String price) { System.out.println(bookName); @@ -4381,6 +4382,109 @@ public class UserController { } ``` +测试 + +![image-20220120214323545](张博凯的Java学习笔记.assets/image-20220120214323545.png) + +![image-20220120214332553](张博凯的Java学习笔记.assets/image-20220120214332553.png) + +>**[注意]** +> +>1.如果控制器方法中接收数据的参数名与请求过来的参数名称一致,则 `@RequestParam("")` 注解可以省略 +> +>如上述例子中的 +> +>```java +>public void addUser(@RequestParam("bookName") String bookName, @RequestParam("author") String author, @RequestParam("price") String price) +>``` +> +>就可以写为 +> +>```java +>public void addUser(String bookName, String author, String price) +>``` +> +>2.当页面传递过来的参数名称与实体类的属性名一一对应,那么控制器方法可以直接通过对象接收 +> +>例如: +> +>src/main/java/org/example/web/UserController.java +> +>```java +> @RequestMapping("/add") +> public void addUser(Book book) { +> System.out.println(book.getBookName()); +> System.out.println(book.getAuthor()); +> System.out.println(book.getPrice()); +> } +>``` +> +>src/main/java/org/example/entity/Book.java +> +>```java +>public class Book { +> private int bookId; +> private String bookName; +> private String author; +> private double price; +> // ... 此处省略 Getter 和 Setter +>} +>``` +> +>测试结果相同 + + + +##### 控制器响应前端请求 + +> form表单/超链接请求跳转页面 + +src/main/webapp/index.jsp + +```html +
+ 提交 + +``` + +src/main/webapp/success.jsp + +```html + +