1
0
mirror of https://gitee.com/coder-xiaomo/java-note synced 2025-09-06 03:51:37 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
2022-01-20 21:41:32 +08:00
parent afb58bf659
commit 0ec0e8f939
2 changed files with 76 additions and 2 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

View File

@@ -4154,7 +4154,7 @@ src/main/resources/spring-servlet.xml
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 声明注解配合 IOC -->
<!-- 声明注解配合 IOC --><!-- Spring管理的注解 -->
<context:annotation-config></context:annotation-config>
<!-- 扫描注解的包 -->
@@ -4296,7 +4296,13 @@ src/main/webapp/WEB-INF/web.xml中url-pattern内部配置
src/main/webapp/WEB-INF/web.xml
```xml
<!-- 请求数据访问 -->
<!-- 扫描注解的包 -->
<context:component-scan base-package="org.example.web"></context:component-scan>
<!-- 声明MVC的注解 --><!-- MVC管理的注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 请求数据访问 --><!-- 默认的Servlet处理器 -->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!-- 配置静态资源放行 -->
@@ -4305,9 +4311,77 @@ src/main/webapp/WEB-INF/web.xml
<mvc:resources mapping="/images/**" location="/images/"></mvc:resources>
```
![image-20220120184525560](张博凯的Java学习笔记.assets/image-20220120184525560.png)
#### SpringMVC框架数据应用
> 前端提交数据到控制器,控制器接收处理数据
##### 前端提交数据到控制器
###### 表单提交
> 输入框需要提供name属性,SpringMVC控制器是通过name属性取值的
```html
<body>
<h1>图书管理系统</h1>
<form action="" method="get">
<p>图书名称:<input type="text" name="bookName"> </p>
<p>图书作者:<input type="text" name="author"> </p>
<p>图书价格:<input type="text" name="price"> </p>
<p><input type="submit" value="提交" > </p>
</form>
</body>
```
###### URL提交
> 超链接提交
```html
<a href="/user/add?id=1">跳转</a>
```
###### ajax提交
##### 控制器接收前端提交的数据
###### @RequestParam("")接收请求行传值
`@RequestParam("")` 注解用于接收请求行传递的数据
src/main/java/org/example/web/UserController.java
```java
package org.example.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
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);
System.out.println(author);
System.out.println(price);
}
}
```
#### Thymeleaf