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