1.SpringMVC结构:
2.项目结构图:
3.新建“SpringMVC”项目
4.导入相关jar包
commons-logging-1.1.1.jar
org.springframework.aop-3.0.5.RELEASE.jar
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar
org.springframework.web.servlet-3.0.5.RELEASE.jar
5.配置web.xml
<!-- 配置springmvc -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
6.添加springmvc配置文件,默认在WEB-INF下添加[DispatcherServletName]-servlet.xml (springmvc-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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置handlerMapping -->
<bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping" />
<!-- 配置handlerAdapter -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!-- 配置渲染器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置请求和处理器 -->
<bean name="/hello.do" class="cn.com.controller.Controller" />
</beans>
7.在WEB-INF目录下加jsp文件夹再加hello.jsp
8.编写Controller代码
package cn.com.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
public class Controller implements org.springframework.web.servlet.mvc.Controller {
public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response)
throws Exception {
ModelAndView mv = new ModelAndView();
// 封装要显示到视图中的数据
mv.addObject("msg", "这是我的第一个SpringMVC");
// 视图名
mv.setViewName("hello");
return mv;
}
}
9.发布到tomcat服务,访问地址:http://localhost:8080/springmvc/hello.do