1、配置文件的web.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <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">
3 <display-name>springmvc1</display-name>
4
5 <servlet>
6 <servlet-name>springmvc</servlet-name>
7 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
8 <!-- 默认加载方式必须规范:
9 * 文件命名:servlet-name-servlet.xml====springmvc-servlet.xml
10 * 路径规范:必须在WEB-INF目录下面 -->
11 <init-param>
12 <param-name>contextConfigLocation</param-name>
13 <param-value>classpath:springmvc.xml</param-value>
14 </init-param>
15 </servlet>
16
17 <servlet-mapping>
18 <servlet-name>springmvc</servlet-name>
19 <url-pattern>*.do</url-pattern>
20 </servlet-mapping>
21
22 <welcome-file-list>
23 <welcome-file>index.html</welcome-file>
24 <welcome-file>index.htm</welcome-file>
25 <welcome-file>index.jsp</welcome-file>
26 <welcome-file>default.html</welcome-file>
27 <welcome-file>default.htm</welcome-file>
28 <welcome-file>default.jsp</welcome-file>
29 </welcome-file-list>
30 </web-app>
2、springMVC.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:mvc="http://www.springframework.org/schema/mvc"
5 xmlns:context="http://www.springframework.org/schema/context"
6 xmlns:aop="http://www.springframework.org/schema/aop"
7 xmlns:tx="http://www.springframework.org/schema/tx"
8 xsi:schemaLocation="http://www.springframework.org/schema/beans
9 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
10 http://www.springframework.org/schema/mvc
11 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
12 http://www.springframework.org/schema/context
13 http://www.springframework.org/schema/context/spring-context-3.2.xsd
14 http://www.springframework.org/schema/aop
15 http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
16 http://www.springframework.org/schema/tx
17 http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
18
19 <!-- 配置处理器映射器,springmvc默认的处理器映射器
20 BeanNameUrlHandlerMapping:根据bean(自定义Controler)的name属性的url去寻找hanler(Action:Controller) -->
21 <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
22
23 <!-- 配置处理器适配器执行Controlelr ,springmvc默认的SimpleControllerHandlerAdapter:执行Controller -->
24 <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
25
26 <!-- 配置自定义Controler -->
27 <bean id="myController" name="/hello.do" class="com.xiaostudy.MyController"/>
28
29 <!-- 配置sprigmvc视图解析器:解析逻辑试图
30 后台返回逻辑试图:index
31 视图解析器解析出真正物理视图:前缀+逻辑试图+后缀====/WEB-INF/index.jsp -->
32 <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
33 <property name="prefix" value="/WEB-INF/"/>
34 <property name="suffix" value=".jsp"/>
35 </bean> -->
36 </beans>
3、Controller.java
1 package com.xiaostudy;
2
3 import javax.servlet.http.HttpServletRequest;
4 import javax.servlet.http.HttpServletResponse;
5
6 import org.springframework.web.servlet.ModelAndView;
7 import org.springframework.web.servlet.mvc.Controller;
8
9 public class MyController implements Controller{
10
11 public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
12 //创建视图
13 ModelAndView mv = new ModelAndView();
14
15 //设置页面回显数据
16 mv.addObject("hello", "xiaostudy");
17
18 //返回物理视图,指定跳转的视图
19 // mv.setViewName("/WEB-INF/index.jsp");
20
21 //返回逻辑视图
22 mv.setViewName("index");
23
24 return mv;
25 }
26
27 }
4、index.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
3 "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7 <title>springMVC_demo</title>
8 </head>
9 <body>
10 ${hello }
11 </body>
12 </html>
5、所需jar包
6、项目文件位置