标签:dex 一个 有一个 springmvc pattern div intern def handle
一、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的流程
四、常见的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.使用注解来替代配置信息。
<!-- 包扫描:扫描 --> <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"; } }
标签:dex 一个 有一个 springmvc pattern div intern def handle
原文地址:https://www.cnblogs.com/kklb/p/11455704.html