SSM17.3【SSM框架整合--Spring+SpringMVC】
阿新 • • 發佈:2021-07-26
搭建和測試SpringMVC的開發環境
在web.xml中配置DispatcherServlet前端控制器和CharacterEncodingFilter編碼過濾器
1 <!DOCTYPE web-app PUBLIC 2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 3 "http://java.sun.com/dtd/web-app_2_3.dtd" > 4 5 <web-app> 6 <display-name>Archetype Created Web Application</display-name> 7 8 <!--配置springMVC的前端控制器,實質就是一個servlet--> 9 <servlet> 10 <servlet-name>dispatcherServlet</servlet-name> 11 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 12 <!--載入springMVC.xml配置檔案--> 13 <init-param> 14 <param-name>contextConfigLocation</param-name> 15 <param-value>classpath:springMVC.xml</param-value> 16 </init-param> 17 <!--期望啟動伺服器,就建立該servlet--> 18 <load-on-startup>1</load-on-startup> 19 </servlet> 20 <servlet-mapping> 21 <servlet-name>dispatcherServlet</servlet-name> 22 <url-pattern>/</url-pattern> 23 </servlet-mapping> 24 25 <!--配置springMVC中解決中文亂碼的過濾器--> 26 <filter> 27 <filter-name>characterEncodingFilter</filter-name> 28 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 29 <!--指定編碼集--> 30 <init-param> 31 <param-name>encoding</param-name> 32 <param-value>UTF-8</param-value> 33 </init-param> 34 </filter> 35 <filter-mapping> 36 <filter-name>characterEncodingFilter</filter-name> 37 <url-pattern>/*</url-pattern> 38 </filter-mapping> 39 40 </web-app>
建立和編寫springMVC.xml配置檔案
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:mvc="http://www.springframework.org/schema/mvc" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/mvc 10 http://www.springframework.org/schema/mvc/spring-mvc.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context.xsd 13 "> 14 15 <!--開啟註解掃描,只掃描Controller註解--> 16 <context:component-scan base-package="com.haifei"> 17 <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 18 </context:component-scan> 19 20 <!--配置檢視解析器物件,實質就是一個bean--> 21 <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 22 <property name="prefix" value="/WEB-INF/pages/"/> 23 <property name="suffix" value=".jsp"/> 24 </bean> 25 26 <!--過濾靜態資源--> 27 <mvc:resources location="/css/" mapping="/css/**" /> 28 <mvc:resources location="/images/" mapping="/images/**" /> 29 <mvc:resources location="/js/" mapping="/js/**" /> 30 31 <!--開啟springMVC的註解支援--> 32 <mvc:annotation-driven/> 33 34 </beans>
測試SpringMVC框架是否搭建成功
1 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 <html> 3 <head> 4 <title>index.jsp</title> 5 </head> 6 <body> 7 <h2>Hello World!</h2> 8 <a href="account/findAll">springMVC測試</a> 9 </body> 10 </html>
1 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 <html> 3 <head> 4 <title>list.jsp</title> 5 </head> 6 <body> 7 <h2>查詢到所有賬戶資訊,在此頁面展示</h2> 8 </body> 9 </html>
1 package com.haifei.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 6 /** 7 * web層-賬戶 8 */ 9 @Controller 10 @RequestMapping("/account") 11 public class AccountController { 12 13 @RequestMapping("/findAll") 14 public String findAll(){ 15 System.out.println("表現層:查詢所有賬戶資訊"); 16 return "list"; 17 } 18 19 }