zoukankan      html  css  js  c++  java
  • SpringMVC最基础配置

     

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

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

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

    目录结构如下:

    二、配置maven的pox.xml

    [html] view plain copy
     在CODE上查看代码片派生到我的代码片
    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

    [html] view plain copy
     在CODE上查看代码片派生到我的代码片
    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.     <!-- 自动扫描的包名 -->  
    18.     <context:component-scan base-package="com.clj"/>  
    19.   
    20.     <!-- 默认的注解映射的支持,自动注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->  
    21.     <mvc:annotation-driven />  
    22.   
    23.     <!-- 视图解释类 -->  
    24.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
    25.         <property name="prefix" value="/WEB-INF/jsp/"/>  
    26.         <property name="suffix" value=".jsp"/>  
    27.         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />  
    28.     </bean>  
    29.       
    30.     <!-- 对静态资源文件的访问-->  
    31.     <mvc:resources mapping="/images/**" location="/WEB-INF/images/" cache-period="31556926"/>  
    32.     <mvc:resources mapping="/js/**" location="/WEB-INF/js/" cache-period="31556926"/>  
    33.     <mvc:resources mapping="/css/**" location="/WEB-INF/css/" cache-period="31556926"/>  
    34.   
    35. </beans>   

    3、web.xml

    [html] view plain copy
     在CODE上查看代码片派生到我的代码片
    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.   
    5. <web-app>  
    6.   
    7.   <!-- ================spring mvc 适配器================ -->  
    8.     <servlet>  
    9.         <servlet-name>springMVC</servlet-name>  
    10.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    11.         <init-param>  
    12.             <param-name>contextConfigLocation</param-name>  
    13.             <param-value>classpath*:/applicationContext/application_spring_mvc.xml</param-value>  
    14.         </init-param>  
    15.         <load-on-startup>1</load-on-startup>  //初始化优先级1, 1~5.
    16.     </servlet>  
    17.     <servlet-mapping>  
    18.         <servlet-name>springMVC</servlet-name>  
    19.         <url-pattern>/</url-pattern>  
    20.     </servlet-mapping>  
    21.     
    22.   <!-- ================================================== -->  
    23.     <servlet-mapping>          
    24.         <servlet-name>default</servlet-name>         
    25.         <url-pattern>*.html</url-pattern>        
    26.     </servlet-mapping>     
    27.   
    28.     <welcome-file-list>  
    29.         <welcome-file>index.html</welcome-file>  
    30.     </welcome-file-list>  
    31. </web-app>  

    4、index.html

    [html] view plain copy
     在CODE上查看代码片派生到我的代码片
    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
     在CODE上查看代码片派生到我的代码片
    1. package com.clj.test.user.action;  
    2.   
    3. import org.springframework.context.annotation.Scope;  
    4. import org.springframework.stereotype.Controller;  
    5. import org.springframework.web.bind.annotation.RequestMapping;  
    6. import org.springframework.web.bind.annotation.RequestMethod;  
    7. import org.springframework.web.servlet.ModelAndView;  
    8.   
    9. /** 
    10.  * <一句话功能简述> 
    11.  * <功能详细描述> 
    12.  *  
    13.  * @author  Administrator 
    14.  * @version  [版本号, 2014年3月4日] 
    15.  * @see  [相关类/方法] 
    16.  * @since  [产品/模块版本] 
    17.  */  
    18. @Controller  
    19. @Scope("prototype")   
    20. @RequestMapping("/user")  
    21. public class UserAction  
    22. {  
    23.     @RequestMapping(value="/save",method=RequestMethod.GET)  
    24.     public ModelAndView  save(String name,String password)  
    25.     {  
    26.         System.out.println("接收到的用户信息:"+name);  
    27.           
    28.         ModelAndView mov=new ModelAndView();  
    29.         mov.setViewName("/test/saveUserSuccess");  
    30.         mov.addObject("msg", "保存成功了");  
    31.           
    32.         return mov;  
    33.     }  
    34. }  

    6、saveUserSuccess.jsp
    [html] view plain copy
     在CODE上查看代码片派生到我的代码片
    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
     

    我的同类文章

     

    参考知识库

  • 相关阅读:
    读书笔记之理想设计的特征
    一些javascript 变量声明的 疑惑
    LINQ 使用方法
    Google MySQL tool releases
    读书笔记之设计的层次
    EF之数据库连接问题The specified named connection is either not found in the configuration, not intended to be used with the Ent
    转载 什么是闭包
    javascript面向对象起步
    Tips
    数据结构在游戏中的应用
  • 原文地址:https://www.cnblogs.com/iOS-mt/p/6110054.html
Copyright © 2011-2022 走看看