Spring MVC框架
是Spring
提供的一個強大而靈活的WEB框架(Struts2框架也是WEB框架)。藉助於註解,Spring MVC
提供了POJO
(又稱爲enitity
實體、model
模型)的開發模式,使得controller
控制器的開發和測試更加簡單。這些controller
控制器一般不直接處理請求,而是將其委託給Spring
上下文中的其他bean
,通過Spring
的依賴注入功能,這些bean
被注入到控制器中。
Spring MVC
主要由DispatcherServlet
、處理器映射【找控制器】、適配器【調用控制器的方法】、控制器【業務】、視圖解析器、視圖組成。
下面將演示如何在項目中配置SpringMVC
框架,但是由於SpringMVC
一般不單獨使用,如上介紹,SpringMVC
需要配合Spring
框架,因此你首先需要掌握一定的Spring
框架的使用。
1、創建一個普通的Java Web項目
2、導入Spring、SpringMVC框架依賴
①、在web/WEB-INF
目錄下創建lib文件夾
②、複製Spring、SpringMVC的依賴到項目lib文件夾
Spring框架支持包下載地址:https://repo.spring.io/release/org/springframework/spring/
③、複製Spring AOP依賴到項目lib文件夾
④、複製log4j依賴到項目lib文件夾
⑤、複製Junit測試依賴到項目lib文件夾
3、編寫配置文件
①、在web/WEB-INF
目錄下創建文件DispatcherServlet-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 1. 配置處理器映射,spring mvc默認的處理器映射器
BeanNameUrlHandlerMapping:根據bean的name屬性的url去找handlerController -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!-- 2. 配置處理器適配置器執行Controller -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!-- 3.配置一個控制器 -->
<bean name="/user.do" class="cn.hestyle.demo.web.controller.UserController"/>
<!-- 4.配置spring mvc視圖解析器
視圖解析器解析的視圖路徑爲:前綴 + modelAndView + 後綴 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
②、在src目錄下創建文件log4j.properties
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
③、修改web.xml
文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 配置spring mvc DispatcherServlet 攔截器 DispatcherServlet -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
4、編寫Controller
控制器
package cn.hestyle.demo.web.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* description: UserController控制器
*
* @author hestyle
* @version 1.0
* @className springmvc_demo_01->UserController
* @date 2020-01-27 16:39
**/
public class UserController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
//新建一個視圖,關聯/WEB-INF/views(前綴) /user/userList .jsp後綴
ModelAndView modelAndView = new ModelAndView("/user/userList");
//在視圖中放置一個object
modelAndView.addObject("username", "hestyle");
return modelAndView;
}
}
5、編寫視圖(Jsp)
<%--
Created by IntelliJ IDEA.
User: hestyle
Date: 2020/1/27
Time: 16:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用戶列表</title>
</head>
<body>
用戶列表:<br>
<!-- 使用el表達式取出username -->
${username}
</body>
</html>
6、運行項目,訪問/user.do
控制檯輸出的日誌信息:
響應過程大致如下:
上面是通過DispatcherServlet-servlet.xml
文件配置bean,配置起來比較複雜,目前比較普遍使用的方法是註解配置,比較便捷。
①、修改DispatcherServlet-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 1.配置註解掃描位置 -->
<context:component-scan base-package="cn.hestyle.demo.web.controller"/>
<!-- 2. 配置處理器映射,通過註解來查找 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!-- 3.配置註解處理適配器來執行控制器的方法 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!-- 配置spring mvc視圖解析器
視圖解析器解析的視圖路徑爲:前綴 + modelAndView + 後綴 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
②、修改UserController類文件
package cn.hestyle.demo.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* description: UserController控制器
*
* @author hestyle
* @version 1.0
* @className springmvc_demo_01->UserController
* @date 2020-01-27 16:39
**/
@Controller
@RequestMapping("user")
public class UserController {
//@Controller註解的作用是聲明UserController是一個控制器
//@RequestMapping("user")註解的作用是,設置控制器的根路徑
@RequestMapping("list1.do")
public String list1(Model model){
//@RequestMapping("list1.do")註解的作用是申明list1方法處理 /user/list1.do的訪問請求
//model的作用是往jsp傳值
model.addAttribute("username", "hestyle_list1");
return "/user/userList";
}
@RequestMapping("list2.do")
public String list2(Model model){
//@RequestMapping("list2.do")註解的作用是申明list2方法處理 /user/list2.do的訪問請求
//model的作用是往jsp傳值
model.addAttribute("username", "hestyle_list2");
return "/user/userList";
}
}
③、重啓項目,分別訪問/user/list1.do
、/user/list2.do
cn.hestyle.demo.web.controller
包中可以放置多個controller
,每個controller
可以聲明多個方法,而不用去修改DispatcherServlet-servlet.xml
配置文件,只需要在controller頭部添加幾個註解即可,可以說是簡化了配置過程。
以上就是SpringMVC
框架之創建第一個項目的主要內容,喜歡的可以點個贊喲~