intellij 对 Spring MVC + tomcat 7简单配置

文章目的:尝试一下用 intellij 配置Spring MVC + tomcat 7,简单地记录一下步骤以及遇到的坑。

Step 1. create a new empty maven project.
Step 2. specify packaging type and add dependencies to pom file(minimum dependencies for Spring MVC are as follows).
Important Note: A war file is needed for tomcat deployment, as when tomcat is launched, class loader will load the jar files from the lib folder in tomcat. However, in maven project, the jar files are in maven repository and will not import to the lib folder. Packaging the war file and deploying it to tomcat will be needed.

        <packaging>war</packaging>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.5.RELEASE</version>
        </dependency>

Step 3. project structure -> Modules to add a new web module.
Step 4. project structure -> Artifacts to create an artifact of current module. (Check the checkbox of Include in project build). After this, we have the web module project structure.
Step 5. in web.xml, add a sample servlet config as follows

    <servlet>
        <servlet-name>sample</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>sample</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

Step 6. add a servlet config file sample-servlet.xml in folder WEB-INF

<?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: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">
    <mvc:annotation-driven/>
    <context:component-scan base-package="com.warrantli.demo"/>
</beans>

Step 7. create package and class SampleController as follows

package com.warrantli.demo.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Created by warrantli on 2017/6/18.
 */

@Controller
public class SampleController {
    private final Log logger = LogFactory.getLog(SampleController.class);

    @RequestMapping(value = {"/"}, method = {RequestMethod.HEAD})
    public String head() {
        return "sample.jsp";
    }

    @RequestMapping(value = {"/"}, method = {RequestMethod.GET})
    public String sample(Model model) {
        logger.info("==== processing sample method ====");
        model.addAttribute("title", "Sample Title");
        model.addAttribute("message", "This is a sample page!");
        return "sample.jsp";
    }
}

Step 8. create sample.jsp file under web folder as follows

<%--
  Created by IntelliJ IDEA.
  User: warrantli
  Date: 2017/6/18
  Time: 16:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>${title}</title>
</head>
<body>
${message}
</body>
</html>

Step 9. Run -> Edit Configurations -> +(Left upper) -> Tomcat Server -> Local to create a tomcat server. Add an artifact to the deployment by clicking Fix button at the bottom of the popup window(or in the deployment tab).
Step 10. Run tomcat and launch browser to visit http://localhost:8080/ ~!

Important Note: If below error occurs, you may check your project structure -> modules -> Web -> Deployment Descriptor & Web Resource Directories settings whether the web.xml location is correct or the .iml file contains below code.

  <component name="FacetManager">
    <facet type="web" name="Web">
      <configuration>
        <descriptors>
          <deploymentDescriptor name="web.xml" url="file://$MODULE_DIR$/web/WEB-INF/web.xml" />
        </descriptors>
        <webroots>
          <root url="file://$MODULE_DIR$/web" relative="/" />
        </webroots>
        <sourceRoots>
          <root url="file://$MODULE_DIR$/src/main/java" />
          <root url="file://$MODULE_DIR$/src/main/resources" />
        </sourceRoots>
      </configuration>
    </facet>
    <facet type="Spring" name="Spring">
      <configuration />
    </facet>
  </component>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值