1
0
mirror of https://gitee.com/coder-xiaomo/java-note synced 2025-01-10 19:48:15 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
程序员小墨 2022-01-20 22:11:55 +08:00
parent 0ec0e8f939
commit af31bb7d24
4 changed files with 105 additions and 1 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

View File

@ -4351,6 +4351,8 @@ src/main/webapp/WEB-INF/web.xml
###### ajax提交 ###### ajax提交
> 略
##### 控制器接收前端提交的数据 ##### 控制器接收前端提交的数据
@ -4371,7 +4373,6 @@ import org.springframework.web.bind.annotation.RequestParam;
@Controller @Controller
@RequestMapping("/user") @RequestMapping("/user")
public class UserController { public class UserController {
@RequestMapping("/add") @RequestMapping("/add")
public void addUser(@RequestParam("bookName") String bookName, @RequestParam("author") String author, @RequestParam("price") String price) { public void addUser(@RequestParam("bookName") String bookName, @RequestParam("author") String author, @RequestParam("price") String price) {
System.out.println(bookName); 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
<body>
<a href="/user/add?bookName=1&author=1&price=1">提交</a>
</body>
```
src/main/webapp/success.jsp
```html
<body>
<h1>成功页面</h1>
</body>
```
###### 转发
src/main/java/org/example/web/UserController.java
```java
@RequestMapping("/add")
public String addUser(Book book) {
return "/success.jsp"; // 转发
}
```
![image-20220120220526068](张博凯的Java学习笔记.assets/image-20220120220526068.png)
###### 重定向
src/main/java/org/example/web/UserController.java
```java
@RequestMapping("/add")
public String addUser(Book book) {
return "redirect:/success.jsp"; // 重定向
}
```
![image-20220120220526068](张博凯的Java学习笔记.assets/image-20220120220526068.png)
#### Thymeleaf #### Thymeleaf