做 java Web 開發的你,一定聽說過SPRingMVC的大名,作為現在運用最廣泛的Java框架,它到目前為止依然保持著強大的活力和廣泛的用戶群。
本文介紹如何用eclipse一步一步搭建SpringMVC的最小系統,所謂最小系統,就是足以使項目在SpringMVC框架下成功跑起來,并且能夠做一些簡單的事情(比如訪問頁面)的系統。 
jar包
下面直接給出配置及源碼
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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd "></beans>dispatcher-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" xmlns:util="http://www.springframework.org/schema/util" 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/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!-- 開啟注解模式驅動 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 掃包 --> <context:component-scan base-package="com.springmvc.*"></context:component-scan> <!-- 靜態資源過濾器 --> <mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources> <!-- 視圖渲染 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 定制頁面存放的路徑 --> <property name="prefix" value="/WEB-INF/pages/"></property> <!-- 文件的后綴 --> <property name="suffix" value=".jsp"></property> </bean> <!-- 1. 它會掃描 com.springmvc 包下所有的Java類,但凡是遇到有注解的,比如@Controller , @Service , @Autowired ,就會將它們加入到Spring的bean工廠里面去。 2. 所有的靜態資源文件,比如說 js , CSS , images 都需要放在/resources目錄下, 這個目錄現在我們還沒有建。 3. 所有的展示頁面,比如jsp文件,都需要放置在/WEB-INF/pages目錄下, 這個目錄現在我們也沒有建。 --> </beans>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>SpringMVC_First</display-name> <!-- 配置監聽器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- 配置過濾器,解決POST亂碼問題 --> <filter> <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置SpringMVC 分發器,攔截所有請求 --> <servlet> <servlet-name>SpringMVC_First</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>namespace</param-name> <param-value>dispatcher-servlet</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>SpringMVC_First</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>index.jsp源碼
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><html> <head> <meta charset="UTF-8" /> </head> <style> body { background:url(${contextPath}/resources/img/bg.jpg); background-size : 80% 80%; } </style> <body> This is my JSP page. <br> <br/><br/> 辰小狼睡不醒------------^_^ <br/><br/> </body></html>ViewController.java源碼
package com.springmvc.controller;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;/** * @author :辰 * E-mail: 15538323378@163.com * 創建時間:2017-3-6 下午5:34:24 * */@Controllerpublic class ViewController { @RequestMapping("/view") public ModelAndView view(HttpServletRequest request){ String path = request.getParameter("path")+""; ModelAndView mav =new ModelAndView(); String contextPath = request.getContextPath(); mav.addObject("contextPath", contextPath); mav.setViewName(path); return mav; }}在瀏覽器中輸入http://localhost:8080/SpringMVC_First/view?path=index 就可以顯示index.jsp 源碼 以及背景圖 
新聞熱點
疑難解答