当前位置: 首页 > news >正文

SpringMVC-基础架构

一、什么是MVC

二、什么是SpringMVC 

三、SpringMVC的特点

四、配置SpringMVC

简单流程:

总体框架

 

1.创建pom.xml依赖

  <!--打包方式--><packaging>war</packaging><!--依赖--><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>6.1.4</version></dependency><!--servlet依赖--><dependency><groupId>jakarta.servlet</groupId><artifactId>jakarta.servlet-api</artifactId><version>6.0.0</version><scope>provided</scope></dependency><!--日志依赖--><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.11</version></dependency><dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf-spring6</artifactId><version>3.1.2.RELEASE</version></dependency></dependencies>

 2.配置web.xml

   <!--配置springmvc的前端控制器,对浏览器发送的请求统一处理--><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping>

3.编写控制类

就是先创建一个简单的FirstController类:

 4.配置Spring MVC框架的配置文件

这个配置文件的名字为web.xml中servelet定义的名字加-servlet.xml

<context:component-scan base-package="com.pon.control"></context:component-scan><!--视图解析器--><bean id="thymeleafViewResolver" class="org.thymeleaf.spring6.view.ThymeleafViewResolver"><!--作用于视图渲染的过程中,可以设置视图渲染后输出时采用的编码字符集--><property name="characterEncoding" value="UTF-8"/><!--如果配置多个视图解析器,它来决定优先使用哪个视图解析器,它的值越小优先级越高--><property name="order" value="1"/><!--当 ThymeleafViewResolver 渲染模板时,会使用该模板引擎来解析、编译和渲染模板--><property name="templateEngine"><bean class="org.thymeleaf.spring6.SpringTemplateEngine"><!--用于指定 Thymeleaf 模板引擎使用的模板解析器。模板解析器负责根据模板位置、模板资源名称、文件编码等信息,加载模板并对其进行解析--><property name="templateResolver"><bean class="org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver"><!--设置模板文件的位置(前缀)--><property name="prefix" value="/WEB-INF/templates/"/><!--设置模板文件后缀(后缀),Thymeleaf文件扩展名不一定是html,也可以是其他,例如txt,大部分都是html,一般情况下后缀名为.html--><property name="suffix" value=".thymeleaf"/><!--设置模板类型,例如:HTML,TEXT,JAVASCRIPT,CSS等--><property name="templateMode" value="HTML"/><!--用于模板文件在读取和解析过程中采用的编码字符集--><property name="characterEncoding" value="UTF-8"/></bean></property></bean></property></bean>

5.将FirstController类进行具体编码

package com.pon.control;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class FirstController {@RequestMapping(value = "/test")public String hehe(){return "first";//返回值代表逻辑视图的名称}
}

 6.提供浏览器显示的视图

<!doctype html>
<html lang="en"><head><title>First Spring MVC</title></head><body><h1>First Spring MVC!</h1></body></html>

 7.运行结果

五、超链接

FirstController类:

package com.pon.control;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class FirstController {@RequestMapping(value = "/test")public String hehe(){return "first";}@RequestMapping(value = "/aaa")//地址public String hihi(){return "other";}
}

first.thymeleaf:编写浏览器看到的界面

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"><head><title>First Spring MVC</title></head><body><h1>First Spring MVC!</h1>     //a:超链接<a th:href="@{/aaa}">hihi</a> //@{}这个是thymeleaf的模式</body></html>

bother.thymeleaf:

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"><head><title>First Spring MVC</title></head><body><h1>你好</h1></body></html>

六、SpringMVC配置文件

SpringMVC配置文件的名字和位置可以自定义。

 

相关文章:

  • 基于软件在环的飞控机建模仿真
  • 国外有哪些知名的CG网站?CG平台及云渲染平台
  • Kettle根据分类实现Excel文件拆分——kettle开发31
  • 视频格式转换avi格式怎么弄?分享视频转换方法
  • sqlcoder:7b sqlcoder:15b sqlcoder:70b 有什么区别呢?
  • 直接使用Three.js的 Shape和ExtrudeGeometry创建带孔几何体实现挖孔效果
  • Go模板页面浏览器显示HTML源码问题
  • 百度OCR初探-python
  • 怎么提升机器人外呼的转化效率
  • linux网络编程poll笔记
  • 实验12 路由重分布
  • STL-常用容器
  • Elastic 索引结构-倒排索引
  • 【AI绘画】Stable Diffusion 3开源
  • Linux3(进程 编辑文件 用户管理 网络)
  • 【Leetcode】101. 对称二叉树
  • [原]深入对比数据科学工具箱:Python和R 非结构化数据的结构化
  • axios 和 cookie 的那些事
  • Date型的使用
  • Flex布局到底解决了什么问题
  • HomeBrew常规使用教程
  • IP路由与转发
  • java正则表式的使用
  • mysql 数据库四种事务隔离级别
  • Node + FFmpeg 实现Canvas动画导出视频
  • python_bomb----数据类型总结
  • VUE es6技巧写法(持续更新中~~~)
  • 大整数乘法-表格法
  • 技术胖1-4季视频复习— (看视频笔记)
  • 一些css基础学习笔记
  • 原创:新手布局福音!微信小程序使用flex的一些基础样式属性(一)
  • 基于django的视频点播网站开发-step3-注册登录功能 ...
  • #宝哥教你#查看jquery绑定的事件函数
  • (+4)2.2UML建模图
  • (10)Linux冯诺依曼结构操作系统的再次理解
  • (7)svelte 教程: Props(属性)
  • (Forward) Music Player: From UI Proposal to Code
  • (vue)页面文件上传获取:action地址
  • (剑指Offer)面试题34:丑数
  • (生成器)yield与(迭代器)generator
  • (十八)devops持续集成开发——使用docker安装部署jenkins流水线服务
  • (十二)python网络爬虫(理论+实战)——实战:使用BeautfulSoup解析baidu热搜新闻数据
  • (转)Oracle 9i 数据库设计指引全集(1)
  • (转)shell中括号的特殊用法 linux if多条件判断
  • .net反编译的九款神器
  • .pub是什么文件_Rust 模块和文件 - 「译」
  • .py文件应该怎样打开?
  • .vue文件怎么使用_我在项目中是这样配置Vue的
  • ??javascript里的变量问题
  • ?php echo ?,?php echo Hello world!;?
  • @NoArgsConstructor和@AllArgsConstructor,@Builder
  • @PreAuthorize注解
  • [BSGS算法]纯水斐波那契数列
  • [C\C++]读入优化【技巧】
  • [CareerCup][Google Interview] 实现一个具有get_min的Queue