IDEA2016 创建SpringMVC的maven项目

1、新建maven项目

选中create from archetype

IDEA2016 创建SpringMVC的maven项目_第1张图片

在里面选择maven-archetype-webapp(注意这里要下载一个xml文件特别慢,按这个链接所说的进行修改 http://www.cnblogs.com/beiyeren/p/4566485.html)

填好GroupID和ArtifactID

IDEA2016 创建SpringMVC的maven项目_第2张图片

配置好MavenIDEA2016 创建SpringMVC的maven项目_第3张图片

最后Finish完成创建。


2、通过Maven导入SpringMVC相关的jar包

打开pom.xml
添加依赖如下所示
IDEA2016 创建SpringMVC的maven项目_第4张图片

点击import changes导入jar包

 
        4.2.6.RELEASE
        5.1.0.Final
    

    
        
            junit
            junit
            3.8.1
            test
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        

        
            org.springframework
            spring-context
            ${spring.version}
        
        
            javax.servlet
            jstl
            1.2
        
    


3、SpringMVC框架的配置

打开web.xml 配置如下




    
        mvc-dispatcher
        org.springframework.web.servlet.DispatcherServlet
        1
    

    
        mvc-dispatcher
        /
    

在WebINF文件夹下右键新建xml配置文件spring config,命名为mvc-dispatcher-servlet.xml

IDEA2016 创建SpringMVC的maven项目_第5张图片

创建完成后提示Application context not configured for this file,点后面的Create Spring facet就好。

xml文件内容如下




    
    
        
        
        
    

然后打开Project Structure菜单,选择Modules

IDEA2016 创建SpringMVC的maven项目_第6张图片

在main上右键新建文件夹,命名为java,右键Sources,这个文件夹会变成蓝色的,让这个文件夹存放源代码。

IDEA2016 创建SpringMVC的maven项目_第7张图片

然后在java上新建package,我这里是com.qtree,和前面spring的xml配置文件要对应。

新建一个Java Class  HelloController,内容如下。

package com.qtree;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Created by wang on 16-12-17.
 */
@Controller
@RequestMapping("/welcome")
public class HelloController {
    @RequestMapping(method= RequestMethod.GET)
    public String printWelcome(ModelMap model){
        model.addAttribute("message","HelloWorld");
        return  "hello";
    }
}


IDEA2016 创建SpringMVC的maven项目_第8张图片

接着在Web-INF文件夹下新建文件夹jsp,再创建一个hello.jsp文件


Spring MVC :${message}


IDEA2016 创建SpringMVC的maven项目_第9张图片


4、部署Tomcat

在右上角一个朝下的箭头点Edit Configuration,点击+号添加一个Tomcat Server,选择Local。
IDEA2016 创建SpringMVC的maven项目_第10张图片
再切换到Deployment选项卡,点绿色+号,添加一个artifact。
IDEA2016 创建SpringMVC的maven项目_第11张图片
确定之后,点击绿色箭头运行,输入localhost:8080/welcome就能打开hello.jsp页面了。 IDEA2016 创建SpringMVC的maven项目_第12张图片

你可能感兴趣的:(IDEA2016 创建SpringMVC的maven项目)