【SpringMvc】从零开始学SpringMvc之初始化(一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21138819/article/details/83621699

大家好,我们今天开始SpringMvc 这个系列,由于笔者也是安卓出身,对SpringMvc 也是接触不久,所以,这个系列仅仅只是记录笔者学习SpringMvc 过程中的心得,如有错误,欢迎指正。

在开始之前,我们需要准备一些东西,JDK、Eclipse(MyEclipse)、Tomcat、Mysql、Navicat(或者类似软件),这些软件的安装教程网上很多,这里就不一一详述了。

1.创建一个SpringMvc Dynamic Project,如下图,记得勾选 创建web.xml

image.png
image.png

2.下载SpringMvc 需要的jar 包,下载地址,一般选择最新版,然后将jar 包复制到 WebContent/WEB-INF/lib 文件夹下,如下图

image.png

3.在src 下创建springmvc-servlet.xml 文件,并加入springmvc 的基本配置,这里包名用的是com.test,你也可以修改为你自己的包名

<?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 
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

	<!-- scan the package and the sub package -->
	<context:component-scan base-package="com.test" />



	<!-- configure the InternalResourceViewResolver -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		id="internalResourceViewResolver">
		<!-- 前缀 -->
		<property name="prefix" value="/WEB-INF/html/" />
		<!-- 后缀 -->
		<property name="suffix" value=".html" />
	</bean>
</beans>

org.springframework.web.servlet.view.InternalResourceViewResolver 作用是在Controller返回的时候进行解析视图

4.在src 中创建applicationContext.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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans.xsd 
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context.xsd">
	<!--6 容器自动扫描IOC组件 -->
	<context:component-scan base-package="com.test"></context:component-scan>


</beans>

5.修改web.xml,加入引入刚才创建的配置文件,注意applicationContext.xml和springmvc-servlet.xml的引用路径

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>SpringMvc</display-name>

	<!-- 配置spring核心监听器,默认会以 /WEB-INF/applicationContext.xml作为配置文件 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- contextConfigLocation参数用来指定Spring的配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>


	<!--configure the setting of springmvcDispatcherServlet and configure the 
		mapping -->
	<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>
		<!-- <load-on-startup>1</load-on-startup> -->
	</servlet>

	<servlet-mapping>
		<servlet-name>SpringMvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

6.在src 中 创建com.test 包,需要注意,这里需要和上一步的包名保持一致;然后创建IndexController

@Controller
@RequestMapping("")
public class IndexController {

	@RequestMapping("")
	public String hello() {
		return "index";
	}

}

@Controller 注解 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象。分发处理器将会扫描使用了该注解的类的方法。通俗来说,被Controller标记的类就是一个控制器,这个类中的方法,就是相应的动作。

@RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。空字符串代表忽略该路径,所以,hello 方法的完整路径应该为 http://localhost:8080/SpringMvc/

7.因为 在第3步时,我们配置InternalResourceViewResolver 的前缀是WEB-INF/html ,后缀是html,所以我们需要在WebContent/WEB-INF 文件夹下,创建html 文件夹,并创建index.html,只需要在body 中添加一句话即可

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>Hello SpringMvc</h1>

</body>
</html>

8. 到这里,大功告成了,让我们运行下项目,然后在浏览器中输入http://localhost:8080/SpringMvc/,就可以看到我们的刚才写的页面了。

总结 SpringMvc的配置还是比较多的,能变化的就只有配置文件的路径和名字

最后献上源码Github

你的认可,是我坚持更新博客的动力,如果觉得有用,就请点个赞,谢谢

猜你喜欢

转载自blog.csdn.net/qq_21138819/article/details/83621699
今日推荐