mirror of
https://gitee.com/coder-xiaomo/java-note
synced 2025-09-06 20:01:39 +08:00
6
This commit is contained in:
248
张博凯的Java学习笔记.md
248
张博凯的Java学习笔记.md
@@ -4485,22 +4485,20 @@ src/main/webapp/success.jsp
|
|||||||
</body>
|
</body>
|
||||||
```
|
```
|
||||||
|
|
||||||
###### 转发
|
###### 转发(返回类型为String)
|
||||||
|
|
||||||
src/main/java/org/example/web/UserController.java
|
src/main/java/org/example/web/UserController.java
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@RequestMapping("/add")
|
@RequestMapping("/add")
|
||||||
public String addUser(Book book) {
|
public String addUser(Book book) {
|
||||||
return "/success.jsp"; // 转发
|
return "/success.jsp"; // 或者 return "forward:/success.jsp";
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
###### 重定向(返回类型为String)
|
||||||
|
|
||||||
###### 重定向
|
|
||||||
|
|
||||||
src/main/java/org/example/web/UserController.java
|
src/main/java/org/example/web/UserController.java
|
||||||
|
|
||||||
@@ -4511,9 +4509,36 @@ public String addUser(Book book) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
###### 转发(返回类型为ModelAndView)
|
||||||
|
|
||||||
|
src/main/java/org/example/web/UserController.java
|
||||||
|
|
||||||
|
```java
|
||||||
|
@RequestMapping("/add")
|
||||||
|
public String addUser(Book book) {
|
||||||
|
ModelAndView modelAndView = new ModelAndView("/success.jsp");
|
||||||
|
// 或者 "forward:/success.jsp"
|
||||||
|
return modelAndView;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
###### 重定向(返回类型为ModelAndView)
|
||||||
|
|
||||||
|
src/main/java/org/example/web/UserController.java
|
||||||
|
|
||||||
|
```java
|
||||||
|
@RequestMapping("/add")
|
||||||
|
public String addUser(Book book) {
|
||||||
|
ModelAndView modelAndView = new ModelAndView("redirect:/success.jsp");
|
||||||
|
return modelAndView;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -4521,6 +4546,219 @@ public String addUser(Book book) {
|
|||||||
|
|
||||||
> 模板引擎
|
> 模板引擎
|
||||||
|
|
||||||
|
##### 介绍
|
||||||
|
|
||||||
|
>**Thymeleaf**是一个现代的服务器端 Java 模板引擎,适用于 Web 和独立环境。
|
||||||
|
>
|
||||||
|
>Thymeleaf 的主要目标是为您的开发工作流程带来优雅*的自然模板*——**HTML可以在浏览器中正确显示**,也可以用作静态原型,从而在开发团队中实现更强大的协作。
|
||||||
|
>
|
||||||
|
>Thymeleaf 是现代 HTML5 JVM Web 开发的理想选择
|
||||||
|
|
||||||
|
###### 官网
|
||||||
|
|
||||||
|
https://www.thymeleaf.org/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
##### 导入依赖
|
||||||
|
|
||||||
|
###### 导入thymeleaf依赖
|
||||||
|
|
||||||
|
https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf/3.0.14.RELEASE
|
||||||
|
|
||||||
|
```xml
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.thymeleaf</groupId>
|
||||||
|
<artifactId>thymeleaf</artifactId>
|
||||||
|
<version>3.0.14.RELEASE</version>
|
||||||
|
</dependency>
|
||||||
|
```
|
||||||
|
|
||||||
|
###### 导入thymeleaf依赖
|
||||||
|
|
||||||
|
https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring4/3.0.14.RELEASE
|
||||||
|
|
||||||
|
```xml
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring4 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.thymeleaf</groupId>
|
||||||
|
<artifactId>thymeleaf-spring4</artifactId>
|
||||||
|
<version>3.0.14.RELEASE</version>
|
||||||
|
</dependency>
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
##### 编写SpringMVC的核心配置文件
|
||||||
|
|
||||||
|
src/main/resources/spring-servlet.xml
|
||||||
|
|
||||||
|
```xml
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:mvc="http://www.springframework.org/schema/mvc"
|
||||||
|
xmlns:context="http://www.springframework.org/schema/context"
|
||||||
|
xsi:schemaLocation="
|
||||||
|
http://www.springframework.org/schema/context
|
||||||
|
http://www.springframework.org/schema/context/spring-context.xsd
|
||||||
|
http://www.springframework.org/schema/mvc
|
||||||
|
http://www.springframework.org/schema/mvc/spring-mvc.xsd
|
||||||
|
http://www.springframework.org/schema/beans
|
||||||
|
http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||||
|
|
||||||
|
<!-- 声明注解配合 IOC --><!-- Spring管理的注解 -->
|
||||||
|
<context:annotation-config></context:annotation-config>
|
||||||
|
|
||||||
|
<!-- 扫描注解的包 -->
|
||||||
|
<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>
|
||||||
|
|
||||||
|
<!--thymeleaf模板解析器-->
|
||||||
|
<bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
|
||||||
|
<property name="prefix" value="/WEB-INF/templates/"></property>
|
||||||
|
<property name="suffix" value=".html"></property>
|
||||||
|
<property name="templateMode" value="HTML5"></property>
|
||||||
|
<property name="cacheable" value="false"></property>
|
||||||
|
<property name="characterEncoding" value="utf-8"></property>
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
|
||||||
|
<property name="templateResolver" ref="templateResolver"></property>
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
<!--视图解析器-->
|
||||||
|
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
|
||||||
|
<property name="templateEngine" ref="templateEngine"></property>
|
||||||
|
<property name="characterEncoding" value="utf-8"></property>
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
<!-- 配置静态资源放行 -->
|
||||||
|
<mvc:resources mapping="/css/**" location="/css/"></mvc:resources>
|
||||||
|
<mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
|
||||||
|
<mvc:resources mapping="/images/**" location="/images/"></mvc:resources>
|
||||||
|
</beans>
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
##### 在web.xml中配置首页启动项
|
||||||
|
|
||||||
|
src/main/webapp/WEB-INF/web.xml
|
||||||
|
|
||||||
|
```xml
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
|
||||||
|
version="4.0">
|
||||||
|
|
||||||
|
<!-- 首页启动项 -->
|
||||||
|
<welcome-file-list>
|
||||||
|
<welcome-file>/WEB-INF/templates/login.html</welcome-file>
|
||||||
|
</welcome-file-list>
|
||||||
|
|
||||||
|
<servlet>
|
||||||
|
<servlet-name>springmvc</servlet-name>
|
||||||
|
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
||||||
|
<init-param> <!-- 加载SpringMVC的配置文件 -->
|
||||||
|
<param-name>contextConfigLocation</param-name>
|
||||||
|
<param-value>classpath:spring-servlet.xml</param-value>
|
||||||
|
</init-param>
|
||||||
|
</servlet>
|
||||||
|
<servlet-mapping>
|
||||||
|
<servlet-name>springmvc</servlet-name>
|
||||||
|
<url-pattern>/</url-pattern>
|
||||||
|
<!-- / 代表从根目录开始拦截 -->
|
||||||
|
</servlet-mapping>
|
||||||
|
</web-app>
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
##### 使用
|
||||||
|
|
||||||
|
###### ****
|
||||||
|
|
||||||
|
一般thymeleaf的模板 放在WEB-INF下面
|
||||||
|
|
||||||
|
src/main/webapp/WEB-INF/templates/login.html
|
||||||
|
|
||||||
|
```html
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en" xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Title</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>thymeleaf</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
```
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
##### 运用
|
||||||
|
|
||||||
|
src/main/webapp/WEB-INF/templates/login.html
|
||||||
|
|
||||||
|
```html
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en" xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Title</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>图书管理系统</h1>
|
||||||
|
<form action="/user/add" 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>
|
||||||
|
</html>
|
||||||
|
```
|
||||||
|
|
||||||
|
src/main/webapp/WEB-INF/templates/success.html
|
||||||
|
|
||||||
|
```html
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en" xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Title</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>图书管理系统</h1>
|
||||||
|
<form action="/user/add" 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>
|
||||||
|
</html>
|
||||||
|
```
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## 提升
|
## 提升
|
||||||
|
Reference in New Issue
Block a user