SpringMVC注解开发详解
程序员文章站
2023-12-03 11:17:58
没有什么正确的选择,努力将自己的选择变的正确。使用注解开发会让SpringMVC项目变的更加简单,下面就使用IDEA中构建maven项目创建注解开发SpingMVC:1.导入依赖:springmvc所需的依赖 junit junit
没有什么正确的选择,努力将自己的选择变的正确。
使用注解开发会让SpringMVC项目变的更加简单,下面就使用IDEA中构建maven项目创建注解开发SpingMVC:
1.导入依赖:springmvc所需的依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${springframework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${springframework.version}</version>
</dependency>
</dependencies>
2.编写web.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">
<!--配置前端控制器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
</servlet>
<!--映射所有请求-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3.配置springmvc-servlet.xml
使用注解之后以前繁琐的各种bean(比如一些处理器映射器,处理器适配器就不需要自己配置)就没有了交给spring去处理
<?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
http://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 http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--自动去处理包下的注解,由IOC进行控制-->
<context:component-scan base-package="com.jektong.controller"/>
<!--过滤静态资源,比如.css,.js等-->
<mvc:default-servlet-handler/>
<!--支持注解驱动-->
<mvc:annotation-driven/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
4.编写HelloController.java
/**
* @author jektong
* @Date 2020/7/2-10:31
*/
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello";
}
}
5.编写hello.jsp
文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
hello springmvc-annotation
</body>
</html>
6.运行效果图:
一个简单的SpringMVC项目完成了
您的每一个赞都是我学习的动力!一起加油吧!
本文地址:https://blog.csdn.net/qq_41857955/article/details/107080422