一、新建Dynamic Web Project 项目 (版本选择2.5不然没有web.xml文件 )html
二、配置web.xml文件
java
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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"> <display-name>springMVC1</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- SpringMVC的主控Servlet --> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatch-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!--SpringMVC处理的URL --> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
(1)DispatcherServletweb
使用Spring MVC,配置DispatcherServlet是第一步。spring
DispatcherServlet是一个Servlet,因此能够配置多个DispatcherServlet。spring-mvc
DispatcherServlet是前置控制器,配置在web.xml文件中的。mvc
拦截匹配的请求,Servlet拦截匹配规则要自已定 义,把拦截下来的请求,app
依据某某规则分发到目标Controller(咱们写的Action)来处理。框架
(2)init-paramjsp
指明了配置文件的文件名,不使用默认配置文件名,而使用springMVC.xml配置文件。测试
其中<param-value>**.xml</param-value> 这里可使用多种写法
一、不写,使用默认值:/WEB-INF/<servlet-name>-servlet.xml
二、<param-value>/WEB-INF/dispatch-servlet.xml</param-value> #当前上面代码的写法
三、<param-value>classpath*:springMVC-mvc.xml</param-value>
四、多个值用逗号分隔
(3)<load-on-startup>1</load-on-startup> 是启动顺序,让这个Servlet随Servletp容器一块儿启动。
<url-pattern>*.form</url-pattern> 会拦截*.form结尾的请求。
<servlet-name>example</servlet-name>这个Servlet的名字是example,能够有多个DispatcherServlet,
是经过名字来区分的。每个DispatcherServlet有本身的WebApplicationContext上下文对象。同时保存的 ServletContext中和Request对象中,关于key,之后说明。
在DispatcherServlet的初始化过程当中,框架会在web应用的 WEB-INF文件夹下寻找名为 [servletname]- servlet.xml 的配置文件,生成文件中定义的bean。也能够本身定义例如:dispatch-servlet.xml
3.WEB-INF下建立文件 dispatch-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context <!-- 对静态资源文件的访问 --> <mvc:resources mapping="/images/**" location="/images/" /> <!-- 配置视图解析器 把控制器返回的字符串解析为/WEB-INFO/views/下面的jsp页面 具体作法就是讲返回的字符串加上你指定的后缀.jsp --> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> <!--配置SpringMVC扫描路径 --> <context:component-scan base-package="com.yubai.springmvc.web" /> </beans:beans>
四、在src下面新建包 com.yubai.springmvc.web,在包中新建.java文件 helloWord.java
package com.yubai.springmvc.web; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class helloWord { @RequestMapping(value="/test", method = RequestMethod.GET) public String test(Model model){ //另外一种mapping 方法,返回字符串,这个字符串就是视图名字 model.addAttribute("serverTime", "1"); return "home"; //因为只是测试,因此懒得再写一个jsp了。。。 } }
(1) @RequestMapping:
请求处处理器功能方法的映射规则;value:路径 , method:请求方式
(2) model.addAttribute("serverTime", "1") 返回serverTime值为1的数据到 模板
(3) return "home" 对应模板为home.jsp
五、开发视图界面 home.jsp 在WEB-INF新建文件夹views ----- 新建home.jsp
<%@ 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> <h1> Hello world! </h1> <P> ${serverTime} </P> </body> </html>
(1)serverTime 便是Controller传来的参数
六、经过请求 http://localhost:8080/springMVC1/test 若是页面输出“Hello World! ”就代表咱们成功了!