JavaWeb学习之路——SSM框架之SpringMVC(七)
Spring?MVC:
簡介:SpringMVC是一種基于Java,實現了Web MVC設計模式,請求驅動類型的輕量級Web框架,即使用了MVC架構模式的思想,將Web層進行職責解耦。基于請求驅動指的就是使用請求-響應模型,框架的目的就是幫助我們簡化開發,SpringMVC也是要簡化我們日常Web開發
1.SpringMVC中重要組件
(1)DispatchServerlet:前端控制器,接受所有請求(如果配置“/”不包括jsp文件)
(2)HandlerMapping:解析請求格式,判斷具體執行何種方法
(3)HandlerAdapter:負責調用具體的方法
(4)ViewResovler:視圖解析器,解析結果,準備跳轉到具體的物理視圖
Req->DispatchServerlet->HandlerMapping->HandlerAdapter->Controller->ViewResovler->User
?
2.配置環境
(1)導入架包。大部分跟spring的配置一樣,多了springmvc部分
方法一:
(2)配置web.xml文件,因為tomacat不能啟用spring的功能,需要用web.xml來使tomacat加載springmvc,在這里面加載pring的配置文件。來將控制權移交給spring。即進入DispatcherServlet前端控制器中,找不到類名是,按F4可立即進入類所在的源碼
? ? ?若不配置<init-parm>會在/WEB-INF/<servlet-name>-servlet.xml中尋找
<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><servlet><servlet-name>springmvc123</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 修改配置文件路徑和名稱 --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param><!-- 自啟動 --><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springmvc123</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>(3)配置springmvc文件。此時前面已經加載了DispatcherServlet,進入解析器Handlermapping中了,在src下配置spring用到的controller控制器,adapter適配器,以及相應的view視圖來跳轉
此文件被web.xml中的DispatcherServlet所加載
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><bean id="demo123" class="com.likui.controller.DemoController"></bean><bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"><property name="urlMap"><map><!-- 解析控制器邏輯名字 --><entry key="demo" value-ref="demo123"></entry></map></property></bean><bean id="handlerAdaper" class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean><bean id="view" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 如果不加下面屬性,那么需要自己設置視圖中的跳轉界面語句 --><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean></beans>(4)在src下新建controller包,在這里面寫控制器執行的動作,下面為控制器執行適配器并用視圖實現跳轉
package com.likui.controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;public class DemoController implements Controller{@Overridepublic ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {// TODO Auto-generated method stubSystem.out.println("執行springmvc控制器!");ModelAndView mav=new ModelAndView("main");return mav;}}(5)新建main.jsp文件,這樣就可以用view視圖來跳轉到他了
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title></head><body>hello!</body></html>?
(6)執行效果圖
右鍵點擊運行在服務器上,在瀏覽器網址后面輸入demo控制器名字,之后就會跳轉到main.jsp界面
?
方法二:
3.使用注解方式來配置springmvc.xml文件
(1)springmvc.xml文件
<?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:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"??xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><!-- 掃描注解 --><context:component-scan base-package="com.likui.controller"></context:component-scan><!-- 注解驅動 --><mvc:annotation-driven></mvc:annotation-driven><!-- 靜態資源 --><mvc:resources location="/js/" mapping="/js/**"></mvc:resources><mvc:resources location="/css/" mapping="/css/**"></mvc:resources><mvc:resources location="/images/" mapping="/images/**"></mvc:resources></beans>?
(2)相應的注解具體使用方法DemoController
package com.likui.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class DemoController {@RequestMapping("demo")public String demo() {System.out.println("demo");return "main.jsp";}}?
(3)main.jsp內容
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title></head><body>hello springmvc!</body></html>(4)執行結果
?
?
總結
以上是生活随笔為你收集整理的JavaWeb学习之路——SSM框架之SpringMVC(七)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaWeb学习之路——SSM框架之S
- 下一篇: JavaWeb学习之路——SSM框架之S