SpringMVC的HelloWorld
参考链接:IDEA建立Spring MVC Hello World 详细入门教程
创建项目
勾选Spring-SpringMVC,JAVAEE-WebApplication,创建项目。在项目名称右键,Add Framework Support。添加maven。
pom内容如下:pom.xml
项目结构如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| . ├── mvc-hello.iml ├── pom.xml ├── src │ └── com │ └── sitech │ └── HiController.java └── web ├── WEB-INF │ ├── applicationContext.xml │ ├── dispatcher-servlet.xml │ ├── jsp │ │ └── say.jsp │ └── web.xml └── index.jsp
|
HiController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.sitech;
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping;
@Controller @RequestMapping("/hi") public class HiController {
@RequestMapping("/say") public String say(Model model) { model.addAttribute("name","wormday"); model.addAttribute("url","http://www.cnblogs.com/wormday/p/8435617.html"); return "say"; } }
|
applicationContext.xml
1 2 3 4 5 6
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
|
dispacher-servlet.xml
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?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.sitech"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <!-- 视图名称后缀 --> <property name="suffix" value=".jsp"/> </bean> </beans>
|
say.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?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.sitech"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <!-- 视图名称后缀 --> <property name="suffix" value=".jsp"/> </bean> </beans>
|
添加tomcat配置
添加artifact
选择“项目名-exploded”,因为是使用maven,所以不用手动添加依赖
运行
点击运行,访问路径:http://localhost:8080/hi/say