SpringMVC基础配置

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

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

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

目录结构如下:

二、配置maven的pox.xml

<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

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

<!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>
    </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>
<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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值