SpringMVC和Struts2一樣,是前背景的一個粘合劑,struts2用得比較熟悉了,現在來配置一下SpringMVC,看看其最基礎配置和基本使用。SpriingMVC不是太難,學習成本不高,現在很多人都喜歡使用它了。
本次demo工程是一個maven工程,使用maven來對項目進行管理。
一、首先需要建立一個maven的webapp工程。
目錄結構如下:

二、配置maven的pox.xml
[html] view plain copy
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.10</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>3.2.8.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>jstl</groupId>
- <artifactId>jstl</artifactId>
- <version>1.1.2</version>
- </dependency>
Maven配置界面如下圖。
這裡我們使用的spring版本是3.2.8.RELEASE;配置好後,maven會自動給我們加載其他依賴的jar包,如下圖:
具體依賴的包如下:
1) aopalliance-1.0.jar
2) commons-logging-1.1.3.jar
3) spring-aop-3.2.8.RELEASE.jar
4) spring-beans-3.2.8.RELEASE.jar
5) spring-context-3.2.8.RELEASE.jar
6) spring-core-3.2.8.RELEASE.jar
7) spring-expression-3.2.8.RELEASE.jar
8) spring-web-3.2.8.RELEASE.jar
9) spring-webmvc-3.2.8.RELEASE.jar
10) jstl-1.1.2.jar
三、工程配置
1、建立一個最基礎的springMVC測試工程目錄結構如下圖:
2、application_spring_mvc.xml
[html] view plain copy
- <?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:tx="http://www.springframework.org/schema/tx"
- 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.2.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-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.clj"/>
- <!-- 預設的注解映射的支援,自動注冊DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
- <mvc:annotation-driven />
- <!-- 視圖解釋類 -->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/jsp/"/>
- <property name="suffix" value=".jsp"/>
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
- </bean>
- <!-- 對靜态資源檔案的通路-->
- <mvc:resources mapping="/images/**" location="/WEB-INF/images/" cache-period="31556926"/>
- <mvc:resources mapping="/js/**" location="/WEB-INF/js/" cache-period="31556926"/>
- <mvc:resources mapping="/css/**" location="/WEB-INF/css/" cache-period="31556926"/>
- </beans>
3、web.xml
[html] view plain copy
- <!DOCTYPE web-app PUBLIC
- "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
- "http://java.sun.com/dtd/web-app_2_3.dtd" >
- <web-app>
- <!-- ================spring mvc 擴充卡================ -->
- <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*:/applicationContext/application_spring_mvc.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup> //初始化優先級1, 1~5.
- </servlet>
- <servlet-mapping>
- <servlet-name>springMVC</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- <!-- ================================================== -->
- <servlet-mapping>
- <servlet-name>default</servlet-name>
- <url-pattern>*.html</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- </welcome-file-list>
- </web-app>
4、index.html
[html] view plain copy
- <html>
- <body>
- <h2>Hello World!</h2>
- <form action="./user/save" method="get">
- <input id="name" name="name" value="張三"/><br/>
- <input id="password" name="password" value="123456"/><br/>
- <input type="submit" value="送出"/>
- </form>
- </body>
- </html>
5、UserAction.Java
[java] view plain copy
- package com.clj.test.user.action;
- import org.springframework.context.annotation.Scope;
- 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 Administrator
- * @version [版本号, 2014年3月4日]
- * @see [相關類/方法]
- * @since [産品/子產品版本]
- */
- @Controller
- @Scope("prototype")
- @RequestMapping("/user")
- public class UserAction
- {
- @RequestMapping(value="/save",method=RequestMethod.GET)
- public ModelAndView save(String name,String password)
- {
- System.out.println("接收到的使用者資訊:"+name);
- ModelAndView mov=new ModelAndView();
- mov.setViewName("/test/saveUserSuccess");
- mov.addObject("msg", "儲存成功了");
- return mov;
- }
- }
6、saveUserSuccess.jsp
[html] view plain copy
- <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>添加使用者成功</title>
- </head>
- <body>
- <h1>操作成功了</h1>
- 背景傳回的資訊:${msg}
- </body>
- </html>
四、釋出工程到tomcat進行測試
具體eclipse中如何使用tomcat進行maven webapp項目測試請參看:
javascript:void(0)
1) 浏覽器中輸入:http://localhost:8080/demoSpringMVC/
便可進入index.html頁面,如下圖:
2) 送出後
頂
2
踩
- 上一篇Eclipse中Maven WEB工程tomcat調試
- 下一篇SpringMVC AJAX支援
我的同類文章
- •springMVC 檔案下載下傳2014-03-07閱讀39791
- •SpringMVC AJAX支援2014-03-05閱讀1917
- •springMVC檔案上傳2014-03-07閱讀8398