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

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


1.创建项目

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

2.配置tomcat服务器

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


双击右边两个Spring包,点击OK

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

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


在该package下创建java class,取名“HiController”


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
}
}


修改web.xml,将“*.form” 修改为 “/


修改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+回车”,自动修复

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

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

附上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>


点右上角▶按钮运行程序,会自动弹出http://localhost:8080


这也是idea自动创建的index.jsp所显示的内容

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

最新文章

  1. 一个Tomcat多端口多项目部署
  2. Python数据分析之numpy学习
  3. [引用]SQLServer占CPU100%
  4. 支持单色条码图像生成的条形码控件Barcode Professional
  5. css之opacity
  6. Oracle RMAN备份恢复指导书
  7. windows下 更新 android studio SDK 到最新版本 解决方案
  8. 使用U盘安装win7系统,遇到“无法定位现有系统分区”问题
  9. 1 weekend110的复习 + hadoop中的序列化机制 + 流量求和mr程序开发
  10. CSU 1160 把十进制整数转换为十六进制,格式为0x开头,10~15由大写字母A~F表示
  11. 使用window.postMessage实现跨域通信
  12. hdu1881(贪心+dp)
  13. 【ARM】S5PV210芯片的启动流程
  14. Jobject 使用
  15. YARN 命令总结
  16. python3之微信文章爬虫
  17. ldap数据库--ODSEE--suffix
  18. oracle游标的使用
  19. socket系列之客户端socket——Socket类
  20. Oracle-02:SQL语言的分类或者说SQL语言的组成

热门文章

  1. Echo团队Alpha冲刺随笔 - 第九天
  2. clause
  3. PHP程序员最容易犯的Mysql错误
  4. 含-SH的ACE抑制药的青霉胺样反应
  5. 使用window.localStorage,window.localStorage记录点击次数
  6. vscode vue文件格式化没效果
  7. 开源项目 05 Dapper
  8. [RN] React Native 下实现底部标签(不支持滑动切换)
  9. 51 NOD 1239 欧拉函数之和(杜教筛)
  10. c++中关于堆和堆栈的区别