第一篇Hello World(关于RequestMapping的使用)

@RequestMapping

RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。

用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

下面来一个简单的HelloWorld

第一步:添加需要的jar包

第二步:在web.xml中配置DispatchServlet前端控制器

<?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"
	version="2.5">

	<display-name>Archetype Created Web Application</display-name>
	<!-- 配置DispatchServlet -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 配置DispatchServlet初始化参数: 配置SpringMVC配置文件的位置和名称 -->
		<!-- 
			也可以不通过contextConfigLocation配置SpringMVC的配置文件,而使用
			默认的配置文件/WEB-INF/<servlet-name>-servlet.xml
		 -->
		 <!-- 
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<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>

 第三步:编写handler处理器

package com.hous.springmvc.controller;

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

@Controller
public class HelloWordController {
	
	/**
	 * 1.使用@RequestMapping注解映射请求的URL
	 * 2.返回值会通过视图解析器解析到实际的物理视图,对于InternalResourceViewResolver视图解析器,会做如下解析:
	 * 通过prefix + returnVal + 后缀 这样的方式得到实际的物理视图,然后做转发操作
	 *  /WEB-INF/views/success.jsp
	 *  
	 * @return
	 */
	@RequestMapping("helloworld")
	public String hello() {
		System.out.println("Hello shanshanbox.com");
		return "success";
	}
}

 第四步:编写视图解析器springmvc.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"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

	<!-- 配置自定义扫描包 -->
	<context:component-scan base-package="com.hous.springmvc.controller" />
	
	<!-- 配置视图解析器:把handler方法返回值解析到实际的物理视图 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>

 第五步:根据视图解析器解析的结果编写对应路径下的页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Hello shanshanbox.com</h1>
</body>
</html>

 第六步:编写一个index.html跳转链接

<html>
<body>
<h2><a href="helloworld">click</a></h2>
</body>
</html>

 最后,自己去测试吧

 

猜你喜欢

转载自shuizhongyue.iteye.com/blog/2293812