SpringMVC-HelloWorld

1.创建Maven版的Web工程

BuildPath设置动态工程Dynamic Web Module

Build Path - >Add library->Server Runtime->Apache

2.pom.xml中的jar依赖配置

    <dependencies>
		<!-- context -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.0.0.RELEASE</version>
		</dependency>
		<!-- webmvc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.0.0.RELEASE</version>
		</dependency>
	</dependencies>

3.在web.xml中配置DispatcherServle(调度工servlet)

需要配置的两个地方:

1 配置 SpringMVC配置文件的路径为:类路径下的classpath:springmvc.xml(一般名称为springmvc.xml)

2.配置映射的请求地址 为 /

(注意删除web-app 标签的最后的/ 换行 alt + /)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
	<!-- 配置前端控制器:DispatcherServlet 快捷键 alt + / 倒数第二项 -->
	<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<!-- 配置 SpringMVC配置文件的路径-->
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<!-- 配置映射的请求地址 为 /  -->
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

4.加入SpringMVC的配置文件springmvc.xml

4.1 增加命名空间

4.2 配置组件扫描 和视图解析器

<?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-4.0.xsd">
	
	<!-- 设置自动扫描的包 -->
	<context:component-scan base-package="afei.springmvc"></context:component-scan>
	
	<!-- 配置视图解析器 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 配置前缀 -->
		<property name="prefix" value="/WEB-INF/views/"></property>		
		<!-- 配置后缀 -->
		<property name="suffix" value=".jsp"></property>
	</bean>
	
</beans>

5.创建入口界面

在页面中编写超链接:

<a href="${pageContext.request.contextPath }/helloWorld">Hello SpringMVC </a>

6.编写helloWorld请求处理器

注意@Controller和@RequestMapping("/helloWorld")

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

@Controller
public class HelloWorld {
	
	/*
	 * 方法的返回值会被SpringMVC配置文件中配置的解析器InternalResourceViewResolver解析为真实的物理视图
	 * 然后进行请求的转发
	 * 真实的物理视图=前缀+返回值+后缀
	 * 即:/WEB-INF/views/success.jsp
	 */
	@RequestMapping("/helloWorld")
	public String testHelloWorld() {
		System.out.println("Hello Spring MVC");
		return "success";
	}
}

7. 编写视图页面

在/WEB-INF/views下新建success.jsp页面

8. 部署测试

9.流程解析

index.jsp中的helloWorld+web.xml中的 / = /helloWorld

即helloWorld中的@RequestMapping("/helloWorld")

返回值的success+前缀+后缀=/WEB-INF/views/success.jsp

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yongfeicao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值