SpringMVC框架|搭建SpringMVC環境


一、搭建SpringMVC環境

1.導入jar包

由於SpringMVC無縫管銜Spring,直接導入之前Spring框架的jar包即可。

在這裏插入圖片描述

4.前端控制器

  • *.do*.action : 請求的url以.do或.action結尾,都會被SpringMVC框架所解析。
  • / :所有的請求都會被pringmvc所解析,會造成靜態資源無法訪問.支持RestFul開發風格。
  • /* :攔截所有請求,JSP也會被springmvc解析,造成JSP無法訪問.不要使用這種方式。

如果訪問瀏覽器時不使用前端控制器中配置的的xx.do,就不會被DispatcherServlet攔截,相當於沒有使用SpringMVC框架,會直接404。

<?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_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>SpringMVC01</display-name>
	<!-- 前端控制器 -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 加載SpringMVC配置文件 -->
		<init-param>
				<param-name>contextConfigLocation</param-name>
				<param-value>classpath:springmvc.xml</param-value>
		</init-param>
	</servlet>

	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
</web-app>

3.配置SpringMVC

  • 處理器(手寫)。
  • 處理器映射器。(註解方式下,處理器映射器與@Controller註解匹配;@RequestMapper與請求url匹配。)
  • 處理器適配器。(處理器適配器與處理器映射器成對出現。)
  • 視圖解析器。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	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.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd 
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd ">
	
	<!-- 處理器(手寫) -->
	<bean class="com.gql.springmvc.UserController"></bean>
	
	<!-- 處理器映射器 -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
	
	
	<!-- 處理器適配器 -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>		
	
	
	<!-- 視圖解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
	
</beans>

4.手寫處理器

SpringMVC與servlet語法比較

SpringMVC servlet
mv.addObject("msg","hello world"); request.setAttribute("msg","hello word!");
mv.setViewName("index.jsp"); request.getRequestDispatcher(路徑).forward(request,response);
  • @RequestMapping("/hello"):就是匹配的url路徑。
package com.gql.springmvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
 * 類說明:
 *		處理器
 * @guoqianliang1998.
 */
@Controller
public class UserController {
	
	@RequestMapping("/hello")
	public ModelAndView hello(){
		ModelAndView mv = new ModelAndView();
		mv.addObject("msg","hello world!");
		mv.setViewName("index.jsp");
		return mv;
	}
}

5.跳轉頁面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<html>
  <head>
	<meta http-equive="content-type" content="text/html,charset=utf-8"/>
  </head>
  
  <body>
    ${msg}
  </body>
</html>

開啓tomcat後成功訪問:
在這裏插入圖片描述
第一個SpringMVC程序測試成功。

二、幾個細節問題

1.前端控制器默認加載路徑

<?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_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>SpringMVC01</display-name>
	<!-- 前端控制器 -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 加載SpringMVC配置文件 -->
		<init-param>
				<param-name>contextConfigLocation</param-name>
				<param-value>classpath:springmvc.xml</param-value>
		</init-param>
	</servlet>

	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
</web-app>

上面的前端控制器使用了<init-param>標籤 指定的路徑(classpath:springmvc.xml)加載springmvc.xml文件。如果不指定的話,SpringMVC會去WEB-INF目錄下找一個名爲"servlet-name"+"-servlet.xml"的文件。把springMVC的配置放在該文件中就可以了。

2.簡化SpringMVC配置

SpringMVC的配置可以簡寫,這需要增加schema約束,然後使用<mvc:annotation-driven />處理器映射器處理器適配器合併。
另外,視圖解析器使用prefix與suffix配合處理器也可以使得Java代碼更加規範。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd 
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc.xsd ">
	
	<!-- 處理器(手寫) -->
	<context:component-scan base-package="com.gql.springmvc02"></context:component-scan>
	
	<!-- 代替處理器映射器和處理器適配器 -->
	<mvc:annotation-driven />
 
	<!-- 視圖解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 
			prefix	前綴
			suffix	後綴
			物理視圖地址 = prefix + 邏輯視圖名(handler的返回值) + suffix 
		 -->
		<property name="prefix" value="/WEB-INF/user/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>
發佈了422 篇原創文章 · 獲贊 1121 · 訪問量 25萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章