SSM學習筆記3——整合 SpringMVC、整合SSM

等等waiting_發表於2020-10-10

6. 整合 SpringMVC

6.1.匯入 spring-mvc 包

注意:spring-webmvc這個jar包的版本必須跟spring的版本一致

6.2.配置 web.xml

  1. 啟動 spring,載入 applicationContext.xml

通過spring-web這個jar包裡的contextConfigLocation這個類來啟動spring,但這個類預設載入的是WEB-INF下的applicationContext檔案,但這個檔案我們其實放在了類路徑下面,所以要修改路徑

  1. 啟動 springMVC,載入 spring-mvc.xml

通過spring-webmvc這個jar包裡的DispatcherServlet這個類來啟動

完整的web.xml:(有些檔案後面會補)

<?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>01.mybatis</display-name>

	<!-- 啟動SpringMVC -->
	<servlet>
		<servlet-name>DispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 引數:讀取spring-mvc.xml -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>DispatcherServlet</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>

  
  	<!-- 啟動spring -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 修改路徑 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

  
  <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>
</web-app>

6.3.配置 spring-mvc.xml

參照模板寫
放在config目錄下
在WEB-INF下建一個jsp目錄

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:contenxt="http://www.springframework.org/schema/context"
    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
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 掃描Controller所在的包 -->
	<contenxt:component-scan base-package="cn.sm1234.controller"/>

	<!-- 註解驅動 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!-- 檢視解析器:簡化在Controller類編寫的檢視路徑 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 字首 -->
		<property name="prefix" value="/WEB-INF/jsp/"/>
		<!-- 字尾 -->
		<property name="suffix" value=".jsp"/>
	</bean>

</beans>

6.4.編寫 Controller

在src下新建包:com.controller
在包裡新建class:CustomerController.java

@RequestMapping("/customer")

表示制定訪問路徑,這個表示訪問的是customer

CustomerController.java:

package cn.sm1234.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/customer")
public class CustomerController {

	@RequestMapping("/test")
	public String test(){
		return "test";
	}
}

這樣子執行起來的訪問路徑就是/customer+/test

6.5.編寫頁面

在jsp目錄下新建:test.jsp:

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'test.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    測試SpringMVC是否可用
  </body>
</html>

部署到Tomcat之後,開啟瀏覽器,輸入:
http://localhost:8080/06.mybatis-spring-springmvc/customer/test.action
回車,顯示:
在這裡插入圖片描述

7. SSM 整合-客戶新增

7.1.在 CustomerController 裡面新增方法

新增了兩個方法:跳轉到新增頁面,和儲存方法

package cn.sm1234.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.sm1234.domain.Customer;
import cn.sm1234.service.CustomerService;

@Controller
@RequestMapping("/customer")
public class CustomerController {
	
	//注入業務物件
	@Resource
	private CustomerService customerService;

	/*@RequestMapping("/test")
	public String test(){
		return "test";
	}*/
	
	/**
	 * 跳轉到input.jsp
	 */
	@RequestMapping("/input")
	public String input(){
		return "input";
	}
	
	/**
	 *儲存客戶
	 */
	@RequestMapping("/save")
	public String save(Customer customer){
		System.out.println("======"+customer);
		customerService.saveCustomer(customer);
		return "succ";
	}
	
}

7.2.編寫 input.jsp 錄入客戶頁面

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>客戶錄入頁面</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  <form action="${pageContext.request.contextPath}/customer/save.action" method="post">
  	客戶姓名:<input type="text" name="name"/><br/>
  	客戶性別:
  	<input type="radio" name="gender" value=""/><input type="radio" name="gender" value=""/><br/>
  	客戶手機:<input type="text" name="telephone"/><br/>
  	客戶住址:<input type="text" name="address"/><br/>
  	<input type="submit" value="儲存">
  </form>
  
  </body>
</html>

還有儲存成功的提示頁面,succ.jsp:

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'succ.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    儲存成功啦!
  </body>
</html>

部署到Tomcat之後,開啟瀏覽器,輸入:
http://localhost:8080/07.mybatis-spring-springmvc/customer/input.action
回車,顯示:
在這裡插入圖片描述

在輸入框輸入中文,提交之後發現頁面傳參到 Controller,中文資料亂碼,這時可以在 web.xml 加多編碼過濾器:

  	<!-- 配置SpringMVC編碼過濾器 -->
  	<filter>
  		<filter-name>CharacterEncodingFilter</filter-name>
  		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  		<init-param>
  			<param-name>encoding</param-name>
  			<param-value>utf-8</param-value>
  		</init-param>
  	</filter>
  	<filter-mapping>
  		<filter-name>CharacterEncodingFilter</filter-name>
  		<url-pattern>/*</url-pattern>
  	</filter-mapping>

再次提交資料:
在這裡插入圖片描述
跳轉到儲存成功頁面:
在這裡插入圖片描述
檢視資料庫,已經儲存:
在這裡插入圖片描述

最後附上用到的相關jar包:
連結:https://pan.baidu.com/s/1b-HTwVCPnPxeH-gheT_uFw
提取碼:qyd2

相關文章