1、官网下载spring的jar包。但是spring官网上一搬都是通过maven方式添加spring,但有时候网络连接不上,这个时候就需要直接jar包的方式来添加了。方式如下,根据有限的版本号,直接修改版本号,简单那粗暴的下载jar包。
http://repo.spring.io/libs-release-local/org/springframework/spring/4.1.4.RELEASE/spring-framework-4.1.4.RELEASE-dist.zip
下载解压后,将libs文件夹中jar文件都添加项目的lib中。注意其中commons-logging-1.1.1.jar包是需要另外下去添加进去的。
2、完成jar包的添加后,就需要去配置spring MVC功能了。第一步在WEB-INF文件夹下添加一个位文件:springMVCTemplet-servlet.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:p = "http://www.springframework.org/schema/p"
xmlns:context = "http://www.springframework.org/schema/context"
xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" >
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 , 这里以 jsp 为后最 -->
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix = "/" p:suffix = ".jsp" />
<!-- 告诉 spring 在 cn.lym.control 包下应用了 spring 注解 -->
<context:component-scan base-package = "com.lym.control" />
</beans >
3、在web.xml文件中配置:
<!-- spring MVC -->
<servlet>
<servlet-name> springMVCTemplet </servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup >0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVCTemplet</servlet-name>
<url-pattern> *.htm </url-pattern>
</servlet-mapping>
<listener >
<listener-class >org.springframework.web.context.ContextLoaderListener</listener-class >
</listener >
4、在src目录下创建
com.lym.control
创建的一个IndexController.java类
package com.lym.control;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse ;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
@RequestMapping ( "/index.htm" )
public void index(HttpServletRequest request,
HttpServletResponse response) throws Exception{
System.out.println("huangll..");
//return model;
}
}