《spring mvc教程.doc》由会员分享,可在线阅读,更多相关《spring mvc教程.doc(19页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、1、 spring mvc教程1.1 编写目的鉴于现在有很多公司在使用Spring MVC,为了大家进入公司后能很快上手,特编写此文档。1.2 教程说明Spring采用3.2Jdk采用1.7,1.8版本会出现一个问题,暂时还木找到解决方案。2、入门案例2.1 新建一个Dynamic web project2.2 添加jar包2.3 配置前端控制器及监听器在web.xml配置如下内容: contextConfigLocation /WEB-INF/helloworld-servlet.xml 告诉spring配置文件在哪里 org.springframework.web.context.Cont
2、extLoaderListener用来创建spring容器 helloworld org.springframework.web.servlet.DispatcherServlet 1 helloworld /表示是默认servlet 前端控制器2.4 在WEB-INF下创建spring配置文件helloworld-servlet.xml定义要扫描的注解组件所在包 /WEB-INF/pages/ .jsp 资源解析器2.5 创建控制器package com.javapassion.examples;import org.springframework.stereotype.Controller
3、;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;ControllerRequestMapping(value=/helloworld)请求映射public class MyController RequestMapping(method=RequestMethod.GET)默认调用方法,若木有指定value值 publi
4、c String helloWorld(ModelMap modelMap) modelMap.addAttribute(message1, oraclewdp);存储数据以便在页面访问到return mypage1;2.6 创建视图在WEB-INF下建立文件夹pages,然后创建mypage1.jsp2.7 在webcontent下新建index.jsp2.8 运行项目2.9在url添加/helloworld3、修改上面的项目3.1 添加更多的处理器package com.javapassion.examples;import java.util.Map;import org.springf
5、ramework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.ModelAndView;ControllerRequestMapping(value=/helloworld)public class
6、 MyController RequestMapping(method=RequestMethod.GET) public String helloWorld(ModelMap modelMap) modelMap.addAttribute(message1, oraclewdp);return mypage1; RequestMapping(value=/sayspring, method = RequestMethod.GET) public String helloWorld(Map map) map.put(message2, Spring!); return mypage2; Req
7、uestMapping(value=/sayspringmvc, method = RequestMethod.GET) public ModelAndView helloWorld() ModelAndView modelAndView = new ModelAndView(mypage3); modelAndView.addObject(message3, Spring MVC!); return modelAndView; 3.2 添加新视图mypage2.jsp在pages目录下Insert title here$message23.3 添加新视图mypage3.jsp在pages目录
8、下Insert title here$message33.4 刷新浏览器1、 /sayspring2、 /sayspringmvc4、请求映射,模型和逻辑视图4.1 关系图4.2 处理顺序1. DispatchServlet(前端控制器) 接收一个HTTP请求2. DispatchServlet根据控制器( Controller)和URL映射来选择一个控制器并把请求传递给控制器3. 控制器(实际控制器)执行业务逻辑并设置模型(Model)对象的值4. 控制器返回一个逻辑视图5. 一个 ViewResolver, 由它选中一个特定的视图(JSP, PDF, Excel等) 6. 使用选中的视图技
9、术把模型对象数据显示出来 5、URI 模板5.1 概念类似于URI字符串,里面包含变量5.2 案例-修改项目中MyControllerpackage com.javapassion.examples;import java.util.Map;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.PathVari
10、able;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.ModelAndView;ControllerRequestMapping(value=/helloworld)public class MyController RequestMapping(method=RequestMethod.GET) public Str
11、ing helloWorld(ModelMap modelMap) modelMap.addAttribute(message1, oraclewdp);return mypage1; RequestMapping(value=/sayspring, method = RequestMethod.GET) public String helloWorld(Map map) map.put(message2, Spring!); return mypage2; RequestMapping(value=/sayspringmvc, method = RequestMethod.GET) publ
12、ic ModelAndView helloWorld() ModelAndView modelAndView = new ModelAndView(mypage3); modelAndView.addObject(message3, Spring MVC!); return modelAndView; / 处理URI模板, 接收参数作为String类型 RequestMapping(value=/helloString/helloId, method = RequestMethod.GET) public String helloURITemplate(PathVariable String
13、helloId, Model model) model.addAttribute(message+helloId, message+helloId + is selected!); return mypage+helloId; /处理 URI模板, 接收参数作为 int类型 RequestMapping(value=/helloint/helloId, method = RequestMethod.GET) public String helloURITemplate2(PathVariable int helloId, Model model) model.addAttribute(mess
14、age+helloId, message+helloId + is selected!); return mypage+helloId; 5.3 运行6、处理参数6.1 编写新的控制器package com.javapassion.examples;import java.util.Locale;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springfram
15、ework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestHeader;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.spring
16、framework.web.bind.annotation.ResponseBody;ControllerRequestMapping(value = /hw)public class MyController2 RequestMapping(value=/hello/helloId, method = RequestMethod.GET) public String helloURI( PathVariable int helloId, Model model) model.addAttribute(message+helloId, message+helloId + is selected
17、!); return mypage+helloId; / 接收参数RequestMapping(value = /hello/helloId, method = RequestMethod.GET, params = myCity=Boston)public String helloURIparams(PathVariable int helloId,RequestParam(myCity) String city, Model model) model.addAttribute(message + helloId, I live in + city);return mypage + hell
18、oId;/ 接收 Locale和 HttpServletRequest作为参数RequestMapping(value = /mylocale, method = RequestMethod.GET)public String helloObjects(Model model, Locale locale,HttpServletRequest httpServletRequest) model.addAttribute(country, locale.getCountry();model.addAttribute(ContextPath, httpServletRequest.getConte
19、xtPath();return mylocale;/ 使用显式名字显示请求头RequestMapping(value = requestHeader1)public ResponseBody String withHeader1(RequestHeader(Accept) String Accept) return Obtained Accept header + Accept + ;/ 适用隐式名字显示请求头RequestMapping(value = requestHeader2)public ResponseBody String withHeader2(RequestHeader St
20、ring Accept) return Obtained Accept header + Accept + ;6.2 编写mylocale.jsp在WEB-INF/pages下My locale is $countryMy Context path is $ContextPath6.3 运行6.3.1 带请求参数跟设置一样6.3.2 带请求参数跟设置不一样6.3.3 请求mylocal6.3.4 requestHeader16.3.5 requestHeader27 拦截器7.1 编写拦截器7.1.1 时间拦截器package com.javapassion.examples;import j
21、avax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import mons.logging.Log;import mons.logging.LogFactory;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;public class ElapsedTimeInterceptor e
22、xtends HandlerInterceptorAdapter private static final Log logger = LogFactory.getLog(ElapsedTimeInterceptor.class); public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception logger.info(ElapsedTimeInterceptor: preHandle() entered); long start
23、Time = System.currentTimeMillis(); request.setAttribute(startTime, startTime); return true; public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, /Model model) throws Exception ModelAndView modelAndView) throws Exception long startTime = (Long) request.getA
24、ttribute(startTime); request.removeAttribute(startTime); long endTime = System.currentTimeMillis(); /model.addAttribute(handlingTime, endTime - startTime); modelAndView.addObject(handlingTime, endTime - startTime); logger.info(ElapsedTimeInterceptor: postHandle() exiting); 7.1.2 日志拦截器package com.jav
25、apassion.examples;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import mons.logging.Log;import mons.logging.LogFactory;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;public clas
26、s LoggingInterceptor extends HandlerInterceptorAdapter private static final Log logger = LogFactory.getLog(LoggingInterceptor.class); public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception logger.info(*LoggingInterceptor: preHandle() enter
27、ed); return true; public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, /Model model) throws Exception ModelAndView modelAndView) throws Exception logger.info(*LoggingInterceptor: postHandle() exiting); 7.2 拦截器配置/WEB-INF/pages/.jsp 7.3 运行测试7.3.1 在浏览器中请求7.3.2 参看Eclipse控制台