HelloSpringMVC注解版实现步骤解析
作者:CodeHuba 时间:2021-07-21 01:58:42
注解版步骤
新建一个module,添加web的支持
由于Maven可能存在资源过滤的问题,我们将配置完善pom.xml
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
在pom.xml文件引入相关的依赖
主要有Spring框架核心库、SpringMVC、servlet、JSTL等,我们在父依赖中已经引入了!
配置web.xml
注意点:
注意web.xml版本问题,要最新版;
注册DispatcherServlet
关联SpringMVC的配置文件
启动级别为1
映射路径为/【不要用/*】
配置springmvc配置文件和视图解析器
我们把所有视图都存放在/WEB-INF/目录下,可以保证视图安全,因为这个目录下的文件,客户端不能直接访问。
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--自动扫描包 让指定包下的注解生效 由IOC容器统一管理-->
<context:component-scan base-package="com.kuang.controller"/>
<!--让spring mvc不处理静态资源 .css .js -->
<mvc:default-servlet-handler/>
<!--annotation-driven帮助我们自动完成handlermapper和adapter实例的注入-->
<mvc:annotation-driven/>
<!--视图解析器: 模版引擎 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
创建Controller
@Controller
@RequestMapping("/hello")
public class HelloController {
//真实访问地址:项目名/hello/h1
@RequestMapping("/h1")
public String Hello(Model model){
model.addAttribute("msg","Hello SpringMVC annotation!");
return "hello"; //会被视图解析器处理
}
}
创建视图层
视图可以直接取出并展示从Controller带回的信息,可以通过EL表示取出Model中存放的值或者对象;
配置Tomcat运行
小结
实现步骤其实很简单:
新建一个web项目
导入相关jar包
编写web.xml,注册DispatcherServlet
编写springmvc配置文件
创建对应的控制类,controller
完善前端视图和controller之前的对应
配置tomcat,测试运行调试。
springMVC必须配置的三大件:
处理器映射器
处理器适配器
视图解析器
通常只需要手动配置视图解析器,而处理器映射器和处理器适配器只需要开启注解驱动即可,省去了大段的xml配置。
来源:https://www.cnblogs.com/CodeHuba/p/13602391.html
标签:Spring,MVC,注解
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java建造者设计模式详解
2022-09-19 13:14:11
![](https://img.aspxhome.com/file/2023/6/71236_0s.jpg)
Java实现获取内网的所有IP地址
2023-01-01 07:48:56
![](https://img.aspxhome.com/file/2023/6/88176_0s.png)
spring boot基于Java的容器配置讲解
2023-11-09 05:24:54
C# 通过反射获取类型的字段值及给字段赋值的操作
2021-11-14 13:30:23
关于Java8 parallelStream并发安全的深入讲解
2023-11-15 06:37:35
![](https://img.aspxhome.com/file/2023/5/59545_0s.png)
全面总结java IO体系
2023-05-16 13:19:12
![](https://img.aspxhome.com/file/2023/3/73093_0s.jpg)
简单了解redis常见客户端及Sharding机制原理
2022-03-28 16:49:09
Java实现批量下载(打包成zip)的实现
2022-04-02 01:53:05
android recyclerview模拟聊天界面
2022-01-19 17:02:42
![](https://img.aspxhome.com/file/2023/3/88723_0s.jpg)
java-spark中各种常用算子的写法示例
2023-04-28 23:21:01
C# this关键字的四种用法
2022-10-06 07:09:22
![](https://img.aspxhome.com/file/2023/5/75595_0s.jpg)
java实现文件上传下载
2023-11-23 09:41:18
Java多线程工具CompletableFuture的使用教程
2023-07-30 20:31:45
![](https://img.aspxhome.com/file/2023/1/57711_0s.jpg)
Spring Boot加密配置文件特殊内容的示例代码详解
2023-09-18 08:47:24
![](https://img.aspxhome.com/file/2023/2/64022_0s.jpg)
Java实现时间日期格式转换示例
2023-06-06 04:45:11
JAVA List和Map切割工具详解
2023-01-27 11:32:42
关于springboot2整合lettuce启动卡住问题的解决方法
2022-08-24 09:29:16
Unity TextMeshPro实现富文本超链接默认字体追加字体
2021-10-13 23:57:24
![](https://img.aspxhome.com/file/2023/1/85671_0s.png)
C# GDI+实现时钟表盘
2023-06-20 07:11:32
![](https://img.aspxhome.com/file/2023/7/66117_0s.jpg)
关于Springboot中JSCH的使用及说明
2023-11-28 02:32:16