史上超详细的SpringMVC框架搭建

一、创建项目

用eclipse或myeclipse创建项目MySpringConfig自动生成web.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>MySpringConfig</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

二、导入Jar包

先导入springMVC有关的Jar包,Jar包如下所示:

  1. Spring-beans-XXXX.RELEASE.jar(xxxx为版本号)
  2. Spring-aop-XXXX.RELEASE.jar
  3. Spring-context-XXXX.RELEASE.jar
  4. Spring-core-XXXX.RELEASE.jar
  5. Spring-expression-XXXX.RELEASE.jar
  6. Spring-web-XXX.RELEASE.jar
  7. Spring-webmvc-XXXX.RELEASE.jar
  8. commons-logging-XXXX.jar(必须要这个jar包)
    在这里插入图片描述

三、配置springMVC

在web,xml进行springMVC的配置

   <!-- springMVC的配置 -->
   <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:config/spring-mvc.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>

在src下创建config包和spring-mvc.xml文件
在这里插入图片描述
在spring-mvc.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"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/mvc
	    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
	    
	<!-- 注解扫描包 -->
	<context:component-scan base-package="com.my" />
	
	<!-- 打开Spring的Annotation的支持 -->
	<context:annotation-config />
	
	<!-- 定义跳转的文件的前后缀 ,视图模式配置-->
    <bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 自动给后面action的方法return的字符串加上前缀和后缀,变成一个可用的url地址 -->
		<property name="prefix" value="/my/" />
		<property name="suffix" value=".jsp" />
	</bean> 
	    
</beans>

四、测试执行

在src下建立一个com.my.controller包和MyTestController类
在这里插入图片描述
在MyTestController里面写测试代码

package com.my.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

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

	@RequestMapping("mytest")
	public void mytest(){
		System.out.println("这是MyTestController的mytest()");
	}
}

将项目保存后部署到tomcat上执行,在浏览器中输入测试链接

http://localhost:8080/MySpringConfig/MyTestController/mytest
可以看到,控制台成功打印出输出样例
在这里插入图片描述

到这里,简单的springMVC的框架就搭建成功了,后续我将加入更多的功能,搭建出一个完整的SSM框架

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章