1、準備工作spring mvc 必須的架包
2、下面是項目的架構
3、配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>simpleSpringMCV</display-name>
<!-- 加載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:mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 以.htm結尾的都被mvc攔截 -->
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>
4、配置Spring mvc的文件 這裏放在config資源包下
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:p="http://www.springframework.org/schema/p"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven/>
<!-- 自動掃描包 -->
<context:component-scan base-package="com.cat.spring.controller" />
<!-- mvc返回頁面的配置 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 模板路徑爲WEB-INF/pages/ -->
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<!-- 視圖模板後綴爲.JSP -->
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
5、在com.cat.spring.controller包下建一個Controller控制器
這裏命名爲HomeController
/**
*
*/
package com.cat.spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
/**
* @author chenlf
*
* 2014-3-24
*/
@Controller
@RequestMapping(value = "/")
public class HomeController {
// 攔截/index.htm 方法爲GET的請求
@RequestMapping(value = "/index", method = RequestMethod.GET)
public ModelAndView index() {
ModelAndView view = new ModelAndView();
view.setViewName("index");
return view;
}
}
6、最後我們寫一個jsp文件來測試一下mvc返回的視圖
在WEB-INF/pages/文件夾下建一個index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Spring mvc 簡單的例子</title>
</head>
<body>
<h3>
請求成功返回的界面
</h3>
</body>
</html>
7、最後在tomcat下啓動 打開地址http://localhost:8080/demo_springmvc_simple/index.htm 就可以返回這樣的頁面
好了,到這裏簡單的一個springmvc 就搭好了,這裏配置的比較簡單,主要是讓一些初學者瞭解一下Spring mvc的一些簡單配置,大神看到了勿噴
8、另外附一下項目demo的源代碼地址http://download.csdn.net/detail/a124753561/7092845