使用eclipse创建spring mvc web工程并部署

创建maven工程

 

创建完成后,目录结构如下:

 

优化目录结构:

 

加载spring配置

1)在pom中加入需要的依赖,包含了spring,velocity,mybatis

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test.tt</groupId>
  <artifactId>springExecise</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spirng.version>3.2.13.RELEASE</spirng.version>
        <mybatis.version>3.2.4</mybatis.version>
    </properties>
  
      <dependencies>
        <!-- 增加spring 框架依赖 结束 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spirng.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spirng.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spirng.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spirng.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spirng.version}</version>
        </dependency>
        <!-- 增加spring-context-support依赖视为了与支持velocity -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>3.2.13.RELEASE</version>
        </dependency>
        <!-- 增加spring 框架依赖 结束 -->
        <!-- 增加velocity 依赖开始 -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-tools</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>commons-digester</groupId>
            <artifactId>commons-digester</artifactId>
            <version>1.7</version>
        </dependency>
        <!-- 增加velocity 依赖结束 -->

        <!-- 增加mybatis 框架依赖 开始 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
        <!-- 增加mybatis 框架依赖 结束 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>springExecise</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>**/*Tests.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 

2)编写引导文件 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://JAVA.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"
    version="2.5">

    <display-name>springExecise</display-name>

    <welcome-file-list>
        <welcome-file>index.vm</welcome-file>
    </welcome-file-list>

    <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*:spring/springmvc-servlet.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>
</web-app>

 

 3) 编写spring配置文件:springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
    ">
    <!-- 向spring中注入BeanPostProcessor,使spring能识别被@Autowired、@ Resource 、@ PostConstruct、@ PreDestroy、PersistenceContext、@Required注解 -->
    <context:annotation-config />
    <!-- 配置注解的扫描路径,该配置项其实也包含了自动注入上述processor的功能,所以上面的不写也ok -->
    <context:component-scan base-package="test.tt.springE"></context:component-scan>

    <!-- 资源文件读取 
    <bean id="configproperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="location" value="classpath:config/db.properties" />
    </bean>-->
    
    <!-- 注册一些bean,这些bean可以基于注解来注册bean、完成requestmapping等 -->
    <!-- 和<context:annotation-config />的区别是什么?? 标签namspace不同-->
    <mvc:annotation-driven/>  
    <!-- 将静态资源的处理经由Spring MVC框架交回Web应用服务器处理 -->
    <mvc:default-servlet-handler /> 
    
    <!-- 允许静态资源放在任何地方,如WEB-INF目录下、类路径下等 -->
    <!-- 将/css/的目录,映射为/css/*,映射到/css/**的请求,不会到controller处理,而是直接访问静态资源 -->
    <mvc:resources location="/css/" mapping="/css/**"/>
    
    <!-- 引入mybatis-数据库配置文件 
    <import resource="classpath*:spring/mybatis-config.xml" />  -->
    
    <!-- 引入velocity模板 -->
    <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath" value="/WEB-INF/velocity/" /><!--指定模板存放的路径 -->
        <property name="velocityProperties">
            <props>
                <prop key="input.encoding">UTF-8</prop>
                <prop key="output.encoding">UTF-8</prop>
                <prop key="contentType">text/html;charset=UTF-8</prop>
            </props>
        </property>
    </bean>
    <!--配置视图的显示 -->
    <bean id="ViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
        <property name="prefix" value="" /><!-- 视图文件的前缀,即存放的路径 -->
        <property name="suffix" value=".vm" /><!-- 视图文件的后缀名 -->
        <property name="contentType" value="text/html;charset=UTF-8" />
    
        <property name="exposeSpringMacroHelpers" value="true" /><!--是否使用spring对宏定义的支持 -->
        <property name="exposeRequestAttributes" value="true" /><!--是否开放request属性 -->
        <property name="requestContextAttribute" value="rc" /><!--request属性引用名称 -->
    </bean>
    
</beans>

 

写controller

package test.tt.springE.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class IndexController {

    @RequestMapping(value="/")
    public String hello(@RequestParam(value="name",required=false,defaultValue="you")String name,Model model){
        model.addAttribute("name", name);
        return "index";
    }
}

 

带参数:

 

不带参数

posted @ 2015-09-08 11:57  大陶陶  阅读(1214)  评论(0编辑  收藏  举报