快速搭建Spring MVC项目

使用Maven + SpringMVC架构快速搭建一个的服务端项目

1、使用Maven的maven-archetype-webapp创建web项目

新建project,勾选Create from archetype
快速搭建Spring MVC项目_第1张图片

填写项目信息
快速搭建Spring MVC项目_第2张图片

然后继续操作,直到项目创建完成,等待一会儿,注意右下角的这个提示框,点击Enable Auto-Import,等Maven处理依赖结束
Auto-Import

2、添加Spring依赖、配置web.xml文件
在项目的pom.xml文件的标签下引入Spring全家桶(也不是全都必要)和servlet-api依赖

    
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webmvcartifactId>
                <version>${org.springframework.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webartifactId>
                <version>${org.springframework.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-aopartifactId>
                <version>${org.springframework.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-aspectsartifactId>
                <version>${org.springframework.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-beansartifactId>
                <version>${org.springframework.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-contextartifactId>
                <version>${org.springframework.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-context-supportartifactId>
                <version>${org.springframework.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-coreartifactId>
                <version>${org.springframework.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-expressionartifactId>
                <version>${org.springframework.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-instrumentartifactId>
                <version>${org.springframework.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-jdbcartifactId>
                <version>${org.springframework.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-jmsartifactId>
                <version>${org.springframework.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-oxmartifactId>
                <version>${org.springframework.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-txartifactId>
                <version>${org.springframework.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-ormartifactId>
                <version>${org.springframework.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-testartifactId>
                <version>${org.springframework.version}version>
                <scope>testscope>
            dependency>
            <dependency>
              <groupId>javax.servletgroupId>
              <artifactId>javax.servlet-apiartifactId>
              <version>3.0.1version>
              <scope>compilescope>
            dependency>

标签下维护Spring的verson

<org.springframework.version>4.2.0.RELEASEorg.springframework.version>

使用下面的标签代替项目的web.xml文件的标签

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

  <servlet>
    <servlet-name>springservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
  servlet>

  <servlet-mapping>
    <servlet-name>springservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>

web-app>

注意标签和标签的子标签必须相同

在/WEB-INF/文件夹下创建spring-servlet.xml文件,配置如下


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    
    <context:component-scan base-package="com.swallow.demo.*" />


    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/value>
        property>
        <property name="suffix">
            <value>.jspvalue>
        property>
    bean>

    
    <mvc:default-servlet-handler />

beans>

如果不采用视图模式,在spring-servlet.xml文件的标签加入以下子标签


    <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
        <mvc:message-converters>
            <bean id="mappingJacksonHttpMessageConverter"
                  class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8value>
                        <value>application/json;charset=UTF-8value>
                        <value>application/javascript;charset=UTF-8value>
                        <value>text/plain;charset=UTF-8value>
                    list>
                property>
            bean>
        mvc:message-converters>
    mvc:annotation-driven>

    <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
        
        <property name="favorPathExtension" value="false"/>
    bean>

然后引入fasterxml.jackson依赖全家桶


    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-databindartifactId>
      <version>2.6.3version>
    dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-coreartifactId>
      <version>2.6.3version>
    dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-annotationsartifactId>
      <version>2.6.3version>
    dependency>
    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>fastjsonartifactId>
      <version>1.2.36version>
    dependency>

3、创建Controller
右击项目的main文件夹,选择New -> Directory,创建java文件夹
右击java文件夹,选择Make Directory as -> Sources Root,效果如图
快速搭建Spring MVC项目_第3张图片

右击java文件夹,选择New -> Package,创建包com.swallow.demo
快速搭建Spring MVC项目_第4张图片

在spring-servlet.xml文件中的标签中配置要自动扫描的包,这里设置为com.swallow.demo.*1

然后,创建DemoController类2

package com.swallow.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/",method = {RequestMethod.GET,RequestMethod.POST})
public class DemoController {

    @RequestMapping("first")
    @ResponseBody
    public String firstMethod() {
        return "Hello Spring";
    }
}

4、启动项目
点击图中的小三角,选择Edit Configurations
快速搭建Spring MVC项目_第5张图片
点击左上角的+号,选择Tomcat Server -> Local
快速搭建Spring MVC项目_第6张图片

选择Deployment Tab,点击+号,选择Artifact…
快速搭建Spring MVC项目_第7张图片

选择图示选项,点击OK,再点击OK
快速搭建Spring MVC项目_第8张图片

点击绿色三角或者绿虫子(debug模式)启动tomcat
start

在浏览器输入http://localhost:8080/first ,看到以下页面,就启动成功了
快速搭建Spring MVC项目_第9张图片


  1. 这里的*代表所有子包. ↩
  2. firstMethod方法的@ResponseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML ↩

你可能感兴趣的:(快速搭建Spring MVC项目)