转自https://segmentfault.com/a/1190000017248622

网上看了不少idea搭建SpringMVC Helloworld的例子,但是一个个试下来都没有成功。
我把他们做了个总结再加上自己的经验,最终在idea2018上运行成功,记录下来分享一下。


1.创建项目

IDEA 2018 搭建 Spring MVC helloworld

IDEA 2018 搭建 Spring MVC helloworld

IDEA 2018 搭建 Spring MVC helloworld

点击finish以后会自动下载需要的jar包

2.配置tomcat服务器

IDEA 2018 搭建 Spring MVC helloworld

IDEA 2018 搭建 Spring MVC helloworld

IDEA 2018 搭建 Spring MVC helloworld

IDEA 2018 搭建 Spring MVC helloworld

application context最好改为“/”
*注:如果不改为“/”,那么默认访问路径为localhost:8080/springmvc_hello_war_exploded
修改为“/”的话,默认访问路径为localhost:8080/*

IDEA 2018 搭建 Spring MVC helloworld

IDEA 2018 搭建 Spring MVC helloworld
双击右边两个Spring包,点击OK

IDEA 2018 搭建 Spring MVC helloworld

WEB-INF下新建jsp文件夹,并在里面创建hello.jsp,在<body>里面添加“${message}”

IDEA 2018 搭建 Spring MVC helloworld

IDEA 2018 搭建 Spring MVC helloworld

右键src文件夹,new→Package,取名“com.springmvc.controller”

IDEA 2018 搭建 Spring MVC helloworld
在该package下创建java class,取名“HiController”

IDEA 2018 搭建 Spring MVC helloworld
HiController.java添加代码:

package com.springmvc.controller;

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

@Controller
@RequestMapping("hi")
public class HiController {
    @RequestMapping("hello")
    public String say(ModelMap model){
        model.addAttribute("message","hello world");
        return "hello"; //指向hello.jsp
    }
}

IDEA 2018 搭建 Spring MVC helloworld
修改web.xml,将“*.form” 修改为 “/

IDEA 2018 搭建 Spring MVC helloworld
修改dispatcher-servlet.xml,添加代码:

<context:component-scan base-package="com.tutorialspoint" />

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

这时候会提示有错误,用鼠标点击到 context,然后按“Alt+回车”,自动修复

IDEA 2018 搭建 Spring MVC helloworld

也可以手动修复,在<beans>里添加代码:

xmlns:context="http://www.springframework.org/schema/context"

IDEA 2018 搭建 Spring MVC helloworld

附上dispatcher-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:context="http://www.springframework.org/schema/context"
       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">

    <context:component-scan base-package="com.springmvc.controller" />

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

IDEA 2018 搭建 Spring MVC helloworld
点右上角▶按钮运行程序,会自动弹出http://localhost:8080

IDEA 2018 搭建 Spring MVC helloworld
这也是idea自动创建的index.jsp所显示的内容

我们打开http://localhost:8080/hi/hello

IDEA 2018 搭建 Spring MVC helloworld

 

相关文章: