1.编写一个请求页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="hello">Hello</a>
</body>
</html>
2.编写一个普通的类,POJO作为Controller
package com.spring.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
//pojo一个普通类 作为 控制器
@Controller
public class Helloworld {
/*
* 映射一个叫hello的请求,同一个请求要唯一,@RequestMapping("/hello")不能出现两个
* 可以不同的请求转发到同一个页面
*/
@RequestMapping("/hello")
public String hello(){
System.out.println("fds");
/*
* 返回hello给DispatcherServlet作为前缀名转发
*/
return "hello";
}
}
3.配置一个servlet来转发请求,spring中已经有一个核心类springDispatcherServlet来处理了
<!--
配置DispatcherServlet
-->
<!-- 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>
<!--
需要转发的请求来自spring.mvc.xml
-->
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!--
web容器启动的时候加载配置
-->
<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>
4.配置spring.mvc.ml
<!--
自动扫描包
-->
<context:component-scan base-package="com.spring.mvc"></context:component-scan>
<!--
视图解析器
-->
<bean id="" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>