一、SpringMVC介紹
1.MVC介紹
模型-視圖-控制器(MVC 是一個衆所周知的以設計界面應用程式為基礎的設計模式。它主要通過分離模型、視圖及控制器在應用程式中的角色将業務邏輯從界面中解耦。通常,模型負責封裝應用程式資料在視圖層展示。視圖僅僅隻是展示這些資料,不包含任何業務邏輯。控制器負責接收來自使用者的請求,并調用背景服務(manager或者dao)來處理業務邏輯。處理後,背景業務層可能會傳回了一些資料在視圖層展示。控制器收集這些資料及準備模型在視圖層展示。MVC模式的核心思想是将業務邏輯從界面中分離出來,允許它們單獨改變而不會互相影響。

springmvc介紹
概念
二、第一個案例HelloWorld
1.建立web項目
<?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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
4.設定處理器和映射器
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 處理器映射器 将bean的name作為url進行查找 ,
需要在配置Handler時指定beanname(就是url) 所有的映射器都實作
HandlerMapping接口。
-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<!-- 配置 Controller擴充卡 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
</beans>
5.配置前端控制器
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>test</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>
<!-- 配置前端控制器 -->
<!-- contextConfigLocation配置springmvc加載的配置檔案(配置處理器映射器、擴充卡等等)
如果不配置contextConfigLocation,
預設加載的是/WEB-INF/servlet名稱-serlvet.xml(springmvc-servlet.xml)
-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
6.建立自定義的Controller
/**
* 自定義控制器
* 必須實作Controller接口
* @author dpb【波波烤鴨】
*
*/
public class UserController implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("本方法被調用了...");
ModelAndView view = new ModelAndView();
view.setViewName("/index.jsp");
return view;
}
}
7.測試效果
三、注解方式的使用
1.修改配置檔案開啟注解方式
<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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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-4.3.xsd">
<!-- 開啟注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 開啟掃描 -->
<context:component-scan base-package="com.dpb.controller"></context:component-scan>
</beans>
2.controller中通過注解實作
/**
* 自定義controller
* @author dpb【波波烤鴨】
*
*/
@Controller // 交給Spring容器管理
@RequestMapping("/user") // 設定請求的路徑
public class UserController {
/**
* 查詢方法
* 請求位址是:
* http://localhost:8080/SpringMVC-03-hellowordAnnation/user/query
* @return
*/
@RequestMapping("/query")
public ModelAndView query(){
System.out.println("波波烤鴨:query");
ModelAndView mv = new ModelAndView();
mv.setViewName("/index.jsp");
return mv;
}
/**
* 添加方法
* 請求位址是:
* http://localhost:8080/SpringMVC-03-hellowordAnnation/user/add
* @return
*/
@RequestMapping("/add")
public ModelAndView add(){
System.out.println("波波烤鴨:add");
ModelAndView mv = new ModelAndView();
mv.setViewName("/index.jsp");
return mv;
}
}
3.測試
四、SpringMVC工作原理的介紹
1.原理圖
springmvc工作原理
2.流程文字說明
3.元件說明