1 左上角File-New-WebProject
2 项目名右键-MyEclipse-Add SpringCapabilities…,添加spring支持
3 WEB-INF右键-New-Other-搜索spring
4 目录
5 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置总调度器 开始 -->
<!-- 总调度器
作用1 总的调度
作用2 加载配置文件
默认加载servlet-name开头,后缀为-servlet.xml的同级目录的配置文件
springmvc-servlet.xml
DispatcherServlet默认使用WebApplicationContext作为上下文,
Spring默认配置文件为“/WEB-INF/[servlet名字]-servlet.xml”。
-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 拦截所有请求 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置总调度器 结束 -->
</web-app>
6 springmvc-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 映射处理器 匹配url 找firstDemo 地址栏地址-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- 适配处理器 去找HelloWordController-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀,相对目录为WebRoot -->
<property name="prefix" value="/jsp/"></property>
<!-- 后缀 视图名称以value配置结尾-->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 自定义的处理 -->
<bean name="/firstDemo" class="com.aaa.mvc.controller.HelloWordController"></bean>
</beans>
7 创建HelloWordController-包名右键New-Class
package com.aaa.mvc.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class HelloWordController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
ModelAndView modelAndView = new ModelAndView();
//该类两个作用 ,mode绑定数据
modelAndView.addObject("printInfo", "hello springmvc!");
//View设置逻辑视图名
modelAndView.setViewName("hello");
//返回一个逻辑视图 总调度器去找视图解析器将其转化为真正的视图View
return modelAndView;
}
}
8 创建hello.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'hello.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
${printInfo}
<!-- 默认找request对象
视图渲染 隐藏
从modelAndView里取值 -->
</body>
</html>