intellij 对 Spring MVC + tomcat 7简单配置

来源:互联网 发布:网络集成商有哪些 编辑:程序博客网 时间:2024/05/22 15:53

文章目的:尝试一下用 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. */@Controllerpublic 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
原创粉丝点击