SpringMVC学习日志一
Posted kklb
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringMVC学习日志一相关的知识,希望对你有一定的参考价值。
一、MVC流程图
1.传统MVC
2.SpringMVC
二、一个简单的SpringMVC
1.导入相关的包
2.配置DispatcherServlet 在web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>SpringMVC-01</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-name】-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>*.do</url-pattern> </servlet-mapping> --> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- DispatcherServlet加载指定的springmvc的配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVC-annotation.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
3.配置springmvc的配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 包扫描:扫描 --> <context:component-scan base-package="com.zhiyou100.klb.annotation"></context:component-scan> <!-- 开启注解驱动 --> <mvc:annotation-driven /> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
4.建立Controller
package com.zhiyou100.klb.annotation; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller //等价于<bean class="com.zhiyou100.klb.annotation.MyController" /> public class MyController @RequestMapping("/list.do") //requestMapping:表示的就是你的访问地址 public ModelAndView list() System.out.println("查询所有"); ModelAndView mv = new ModelAndView(); mv.setViewName("login"); mv.addObject("userName", "查询所有"); return mv; @RequestMapping("/delete.do") public String delete() System.out.println("删除"); return "login"; @RequestMapping("/save.do") public String save() System.out.println("保存"); return "login";
三、SpringMVC的流程
- 客户发出请求。http://localhost:8080/springmvc-01/my.do
- 到达web.xml文件中 DispatcherServlet.查看是否符合url的要求
- DispatcherServlet就会查询springmvc的配置文件,找HandlerMapping,根据bean的名称查找相应的controller.
- 找到我的MyController类,执行该类中handleRequestInternal方法。
- 根据返回的modelAndView在找springmvc配置文件中视图解析器,把viewName和prefix以及suffix做了一个拼接,把拼接的页面展示给客户。
四、常见的HandlerMapping
1 BeanNameUrlHandlerMapping 根据bean标签的名称找到对应的Controller类
<!-- 配置一个handlerMapping 默认有一个, 可以不配,默认为name访问 --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- 注册自己的controller类 --> <bean name="/login.do" class="com.zhiyou100.klb.controller.MyController1"></bean>
2. SimpleUrlHandlerMapping 根据bean的id查找对应的Controller类
<!-- 根据ID查询相应的controller类 这里包含了ID与URL地址的映射关系 --> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/a.do">my1</prop> <prop key="/b.do">my2</prop> </props> </property> </bean> <bean id="my1" class="com.zhiyou100.klb.controller.MyController1"></bean> <bean id="my2" class="com.zhiyou100.klb.controller.MyController2"></bean>
3.ControllerClassNameHandlerMapping 根据controller类的名字找到对应的Controller
<!-- 配置一个handlerMapping,使用类名访问--> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean> <!-- 访问时,类中除了controller外其他全部小写,包括controller2 --> <bean class="com.zhiyou100.klb.controller.MyController2"></bean>
4.使用注解来替代配置信息。
- 额外aop的jar包
- 在springmvc配置文件中加入如下代码。
<!-- 包扫描:扫描 --> <context:component-scan base-package="com.zhiyou100.klb.annotation"></context:component-scan> <!-- 开启注解驱动 --> <mvc:annotation-driven /> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"></property> <property name="suffix" value=".jsp"></property> </bean>
3.创建Controller类
package com.zhiyou100.klb.annotation; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller //等价于<bean class="com.zhiyou100.klb.annotation.MyController" /> public class MyController @RequestMapping("/list.do") //requestMapping:表示的就是你的访问地址 public ModelAndView list() System.out.println("查询所有"); ModelAndView mv = new ModelAndView(); mv.setViewName("login"); mv.addObject("userName", "查询所有"); return mv; @RequestMapping("/delete.do") public String delete() System.out.println("删除"); return "login"; @RequestMapping("/save.do") public String save() System.out.println("保存"); return "login";
以上是关于SpringMVC学习日志一的主要内容,如果未能解决你的问题,请参考以下文章