SpringMVC和Struts2一样,是前后台的一个粘合剂,struts2用得比较熟悉了,现在来配置一下SpringMVC,看看其最基础配置和基本使用。SpriingMVC不是太难,学习成本不高,现在很多人都喜欢使用它了。

本次demo工程是一个maven工程,使用maven来对项目进行管理。

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

目录结构如下:

二、配置maven的pox.xml

  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配置界面如下图。

这里我们使用的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

  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

  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

  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

  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

  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项目测试请参看:

http://blog.csdn.net/clj198606061111/article/details/20221133

1)  浏览器中输入:http://localhost:8080/demoSpringMVC/

便可进入index.html页面,如下图:

2)  提交后

 
2
0
 

我的同类文章

spring-mvc(3)
 

参考知识库

最新文章

  1. [python]初探socket
  2. 对象化的Http和请求对象HttpRequest
  3. cookie 操作
  4. HDU2027 统计元音
  5. 通过email分享
  6. python下实现汉诺塔
  7. vijos1056题解
  8. 将电脑文件复制到vm虚拟机中,然后安装步骤
  9. hadoop2.x HDFS HA linux环境搭建
  10. http协议状态码大全
  11. C#方法过滤器
  12. 部署flask
  13. 1013 ACM 杭电 root
  14. String的intern()方法和java关键字、保留字
  15. Chrome浏览器导出pdf时,隐藏链接HREF
  16. tushare使用
  17. [bug report] 当springboot报错 找不到类 javax.xml.bind.JAXBException
  18. debug?用对拍!
  19. python进行des加密解密,而且可以与JAVA进行互相加密解密
  20. java利用HttpClient进行https接口调用

热门文章

  1. Android进阶笔记04:Android进程间通讯(IPC)之Messenger
  2. Fedora 19修改主机名
  3. 【字符串排序,技巧!】UVa 10905 - Children’s Game
  4. Volatile变量
  5. php json_encode转JSON 编码显示中文
  6. centos vim 中文乱码解决方案
  7. 针对SharePointFarm场时安装部署OWA的步骤
  8. akka创建actor时报错:IllegalArgumentException: no matching constructor found on class $iwC$$iwC$$iwC$$iwC$
  9. items 与iteritems
  10. Jquery inArray的使用