天天看點

SpringMVC最基礎配置

SpringMVC和Struts2一樣,是前背景的一個粘合劑,struts2用得比較熟悉了,現在來配置一下SpringMVC,看看其最基礎配置和基本使用。SpriingMVC不是太難,學習成本不高,現在很多人都喜歡使用它了。

本次demo工程是一個maven工程,使用maven來對項目進行管理。

一、首先需要建立一個maven的webapp工程。

目錄結構如下:

SpringMVC最基礎配置

二、配置maven的pox.xml

[html] ​​view plain​​ ​​copy​​

 ​​​​​​​

  1. <dependency>
  2. <groupId>junit</groupId>
  3. <artifactId>junit</artifactId>
  4. <version>4.10</version>
  5. <scope>test</scope>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework</groupId>
  9. <artifactId>spring-webmvc</artifactId>
  10. <version>3.2.8.RELEASE</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>jstl</groupId>
  14. <artifactId>jstl</artifactId>
  15. <version>1.1.2</version>
  16. </dependency>

Maven配置界面如下圖。

SpringMVC最基礎配置

這裡我們使用的​​spring​​版本是3.2.8.RELEASE;配置好後,maven會自動給我們加載其他依賴的jar包,如下圖:

SpringMVC最基礎配置

具體依賴的包如下:

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​​

 ​​​​​​​

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xmlns:mvc="http://www.springframework.org/schema/mvc"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  10. http://www.springframework.org/schema/tx
  11. http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context-3.2.xsd
  14. http://www.springframework.org/schema/mvc
  15. http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
  16. <!-- 自動掃描的包名 -->
  17. <context:component-scan base-package="com.clj"/>
  18. <!-- 預設的注解映射的支援,自動注冊DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
  19. <mvc:annotation-driven />
  20. <!-- 視圖解釋類 -->
  21. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  22. <property name="prefix" value="/WEB-INF/jsp/"/>
  23. <property name="suffix" value=".jsp"/>
  24. <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
  25. </bean>
  26. <!-- 對靜态資源檔案的通路-->
  27. <mvc:resources mapping="/images/**" location="/WEB-INF/images/" cache-period="31556926"/>
  28. <mvc:resources mapping="/js/**" location="/WEB-INF/js/" cache-period="31556926"/>
  29. <mvc:resources mapping="/css/**" location="/WEB-INF/css/" cache-period="31556926"/>
  30. </beans>

3、web.xml

[html] ​​view plain​​ ​​copy​​

 ​​​​​​​

  1. <!DOCTYPE web-app PUBLIC
  2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
  4. <web-app>
  5. <!-- ================spring mvc 擴充卡================ -->
  6. <servlet>
  7. <servlet-name>springMVC</servlet-name>
  8. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  9. <init-param>
  10. <param-name>contextConfigLocation</param-name>
  11. <param-value>classpath*:/applicationContext/application_spring_mvc.xml</param-value>
  12. </init-param>
  13. <load-on-startup>1</load-on-startup>  //初始化優先級1, 1~5.
  14. </servlet>
  15. <servlet-mapping>
  16. <servlet-name>springMVC</servlet-name>
  17. <url-pattern>/</url-pattern>
  18. </servlet-mapping>
  19. <!-- ================================================== -->
  20. <servlet-mapping>
  21. <servlet-name>default</servlet-name>
  22. <url-pattern>*.html</url-pattern>
  23. </servlet-mapping>
  24. <welcome-file-list>
  25. <welcome-file>index.html</welcome-file>
  26. </welcome-file-list>
  27. </web-app>

4、index.html

[html] ​​view plain​​ ​​copy​​

 ​​​​​​​

  1. <html>
  2. <body>
  3. <h2>Hello World!</h2>
  4. <form action="./user/save" method="get">
  5. <input id="name" name="name" value="張三"/><br/>
  6. <input id="password" name="password" value="123456"/><br/>
  7. <input type="submit" value="送出"/>
  8. </form>
  9. </body>
  10. </html>

5、UserAction.​​Java​​

[java] ​​view plain​​ ​​copy​​

 ​​​​​​​

  1. package com.clj.test.user.action;
  2. import org.springframework.context.annotation.Scope;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;
  6. import org.springframework.web.servlet.ModelAndView;
  7. /**
  8. * <一句話功能簡述>
  9. * <功能較長的描述>
  10. *
  11. * @author  Administrator
  12. * @version  [版本号, 2014年3月4日]
  13. * @see  [相關類/方法]
  14. * @since  [産品/子產品版本]
  15. */
  16. @Controller
  17. @Scope("prototype")
  18. @RequestMapping("/user")
  19. public class UserAction
  20. {
  21. @RequestMapping(value="/save",method=RequestMethod.GET)
  22. public ModelAndView  save(String name,String password)
  23. {
  24. System.out.println("接收到的使用者資訊:"+name);
  25. ModelAndView mov=new ModelAndView();
  26. mov.setViewName("/test/saveUserSuccess");
  27. mov.addObject("msg", "儲存成功了");
  28. return mov;
  29. }
  30. }

6、saveUserSuccess.jsp

[html] ​​view plain​​ ​​copy​​

 ​​​​​​​

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6. <title>添加使用者成功</title>
  7. </head>
  8. <body>
  9. <h1>操作成功了</h1>
  10. 背景傳回的資訊:${msg}
  11. </body>
  12. </html>

四、釋出工程到tomcat進行測試

具體eclipse中如何使用tomcat進行maven  webapp項目測試請參看:

​​javascript:void(0)​​

1)  浏覽器中輸入:​​http://localhost:8080/demoSpringMVC/​​

便可進入index.html頁面,如下圖:

SpringMVC最基礎配置

2)  送出後

SpringMVC最基礎配置

2

  • 上一篇​​Eclipse中Maven WEB工程tomcat調試​​
  • 下一篇​​SpringMVC AJAX支援​​

我的同類文章

  • •​​springMVC 檔案下載下傳​​2014-03-07閱讀39791
  • •​​SpringMVC AJAX支援​​2014-03-05閱讀1917
  • •​​springMVC檔案上傳​​2014-03-07閱讀8398

繼續閱讀