---
# System prepended metadata

title: SpringMVC 3/2上課內容
tags: [SpringMVC]

---

# SpringMVC 3/2上課內容
###### tags: `SpringMVC`

[老師云端](https://1drv.ms/u/s!Ans47I9-PkivwH8KqgNszZ8OKcZe)

# 1 SpringMVC動態網站開發實務(目錄)
![](https://i.imgur.com/xX7sd5a.png)
![](https://i.imgur.com/aqsxjUv.png)

# 3 SpringMVC動態網站開發實務(框架)
![](https://i.imgur.com/Psu8Xxn.png)
***
![](https://i.imgur.com/jLjnas9.png)

# 4 SpringMVC動態網站開發實務
![](https://i.imgur.com/3E7FQGO.png)
![](https://i.imgur.com/f8VpC7h.png)
*** 
* 運作圖
![](https://i.imgur.com/ZbMVS2n.png)

# 5 SpringMVC動態網站開發實務
![](https://i.imgur.com/2S7WAhu.png)
***
![](https://i.imgur.com/X7PoTAQ.png)
* DispatcherServlet:總機的角色(處理MVC的請求)
* HandlerMapping:通訊錄(發出請求找哪個Controller處理)
* ModelAndView:傳輸(相關資訊傳給總機)
* ViewResolver:代碼 URL(網址)
* View:顯示Model結果畫面

# 6 SpringMVC動態網站開發實務(架構請求流程[DispatcherServlet:總機])
![](https://i.imgur.com/LocJ4WE.png)
[org.springframework.web.servlet](https://docs.spring.io/spring-framework/docs/current/javadoc-api/)
* 每個總機有每個通訊錄

# 7 SpringMVC動態網站開發實務
![](https://i.imgur.com/NKNtUac.png)
***
![](https://i.imgur.com/rbaav3J.png)

# 8 SpringMVC動態網站開發實務
![](https://i.imgur.com/JPEmPys.png)
[DispatcherServlet](https://docs.spring.io/spring-framework/docs/current/javadoc-api/)
* **load-on-startup**
    * 數字越小越優先 
***
![](https://i.imgur.com/n3BZbua.png)

# 9 SpringMVC動態網站開發實務
![](https://i.imgur.com/rETrXSN.png)
* 核心 DispatcherServlet:總機
***
![](https://i.imgur.com/xhRrOt6.png)
* 集中處理機制

# 10 SpringMVC動態網站開發實務(重要)
![](https://i.imgur.com/mki1dnG.png)
***
![](https://i.imgur.com/KZLiBkh.png)
[HandlerMapping](https://docs.spring.io/spring-framework/docs/current/javadoc-api/)

# 11 SpringMVC動態網站開發實務
![](https://i.imgur.com/Nqeg8fx.png)
[BeanNameUrlHandlerMapping](https://docs.spring.io/spring-framework/docs/current/javadoc-api/)
* **Context Root**
    * (專案)環境根目錄
    * ![](https://i.imgur.com/TLv8Uep.png)
***
![](https://i.imgur.com/pNQxaGz.png)

# 12 SpringMVC動態網站開發實務
![](https://i.imgur.com/vOuse7Y.png)
* 解析網址
***
![](https://i.imgur.com/QKhcanJ.png)

而外:[反組譯混淆氣](https://ithelp.ithome.com.tw/articles/10044266)

# 13 SpringMVC動態網站開發實務
![](https://i.imgur.com/Y6yXOS2.png)
* 大部分都用預設

# 14 SpringMVC動態網站開發實務(無xml網路應用系統)
![](https://i.imgur.com/3J98MoX.png)
***
![](https://i.imgur.com/NTXh0Ym.png)
* ServletContainer:jre環境

# 15 SpringMVC動態網站開發實務
![](https://i.imgur.com/BiEB9H6.png)
* web底下的package
***
![](https://i.imgur.com/BFPMaMr.png)
[AbstractAnnotationConfigDispatcherServletInitializer](https://docs.spring.io/spring-framework/docs/current/javadoc-api/)
## 抽象類別與介面的差別
* **抽象類別(Abstract Class)** 格局小
    * 定義框沒有實作
    * 實作有底下兒子去完成
    * 針對兒子定義
* **介面(interface)** 格局大
    * 定義框沒有實作
    * 有定義的就要實作
    * 有關係就要符合

# 16 SpringMVC動態網站開發實務
![](https://i.imgur.com/MHfGNhh.png)
![](https://i.imgur.com/biKknzE.png)
![](https://i.imgur.com/peO5rSw.png)
* getRootConfigClasses    大的組態設定檔
* getServletConfigClasses 小的組態設定檔
* getServeletMappings 掌控大小
![](https://i.imgur.com/rEdfKTP.png)
![](https://i.imgur.com/YgmJIyd.png)
***
![](https://i.imgur.com/cyLwtFC.png)
1. ? 大小
2. ? 不指定型別
3. "/" 網址都是自己控制的

# 17 SpringMVC動態網站開發實務
![](https://i.imgur.com/BatwP94.png)
***
![](https://i.imgur.com/vz03vYx.png)

# 18 SpringMVC動態網站開發實務
![](https://i.imgur.com/pxN81Oh.png)
1. 自動找
***
![](https://i.imgur.com/PcUgX8l.png)
1. 攔截器
2. 代理人是誰

# 19 SpringMVC動態網站開發實務
![](https://i.imgur.com/KQBI97M.png)
***
![](https://i.imgur.com/9bHJqFG.png)

# 20 SpringMVC動態網站開發實務
![](https://i.imgur.com/ykWqeHm.png)

# 21 SpringMVC動態網站開發實務(實作Spring MVC)
![](https://i.imgur.com/NPkFe2H.png)
***
![](https://i.imgur.com/KvH8qTI.png)

### 新專案
![](https://i.imgur.com/smvPNyW.png)
![](https://i.imgur.com/Ll6nIFH.png)
![](https://i.imgur.com/mQSWD3r.png)
![](https://i.imgur.com/4ubB7PO.png)
![](https://i.imgur.com/9sloU4w.png)
![](https://i.imgur.com/Zw40f1y.png)
![](https://i.imgur.com/A4Vai8J.png)
![](https://i.imgur.com/jIFOukW.png)
![](https://i.imgur.com/PyuGH85.png)
![](https://i.imgur.com/SiheWgV.png)
![](https://i.imgur.com/FnKiFjq.png)
![](https://i.imgur.com/YV6mQEe.png)
![](https://i.imgur.com/IATwqDD.png)
![](https://i.imgur.com/qtJqMBc.png)

#### pom.xml
```clike=
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>SpringMvcWebProject</groupId>
	<artifactId>SpringMvcWebProject</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<dependencies>
		<dependency>
			<groupId>com.microsoft.sqlserver</groupId>
			<artifactId>mssql-jdbc</artifactId>
			<version>8.4.1.jre11</version>
		</dependency>

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>5.4.28.Final</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>5.3.4</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>5.3.4</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>5.3.4</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>5.3.4</version>
		</dependency>

		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.2</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>5.3.4</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>5.3.4</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>5.3.4</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>5.3.4</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>5.3.4</version>
		</dependency>

	</dependencies>


	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
					<release>11</release>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>3.2.3</version>
				<configuration>
					<warSourceDirectory>WebContent</warSourceDirectory>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
```
#### web.xml
```clike=
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>SpringMvcWebProject</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>
  
  <servlet>
     <servlet-name>mvc</servlet-name>
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
     <servlet-name>mvc</servlet-name>
     <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>
```

#### HelloController
```clike=
package tw.leonchen.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloController extends AbstractController {

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String userName = request.getParameter("userName");
		Map<String, String> errors = new HashMap<String, String>();
		request.setAttribute("errors", errors);
		
		if(userName==null || userName.length()==0) {
			errors.put("myName", "name is required");
		}
		
		if(errors!=null && !errors.isEmpty()) {
			return new ModelAndView("/form.jsp"); 
		}
		
		HttpSession session = request.getSession();
		session.setAttribute("name", userName);
		return new ModelAndView("/success.jsp"); 
	}

}

```
# 4章
![](https://i.imgur.com/pO7M0Sp.png)
***
![](https://i.imgur.com/mQ1G7gj.png)
***
![](https://i.imgur.com/pXXZdnS.png)
***
![](https://i.imgur.com/5tfd1NM.png)
***
![](https://i.imgur.com/J5qoD4n.png)
***
![](https://i.imgur.com/HKY1arc.png)
***
![](https://i.imgur.com/POdk0gM.png)

#### mvc-servlet-xml.xml
![](https://i.imgur.com/tNeJ26x.png)
```clike=
<?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: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.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.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="tw.leonchen"/>
    <mvc:annotation-driven/>

    <bean id="helloControler" name="/hello.controller" class="tw.leonchen.controller.HelloController"/>

</beans>

```
#### form.jsp
![](https://i.imgur.com/qIxpYPZ.png)
```clike=
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style type="text/css">
  #errCode{color:red}
</style>
<title>Form</title>
</head>
<body>
<h3>Form</h3>
<form action="hello.controller" method="get">
  <table>
     <tr>
       <td>Name:</td>
       <td><input type="text" name="userName"/></td>
       <td id="errCode">${errors.myName}</td>
     </tr>
     <tr>
       <td><input type="submit" value="Send"/></td>
     </tr>
  </table>
</form>
</body>
</html>
```

#### success.jsp
![](https://i.imgur.com/njYoxm7.png)
```clike=
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome</title>
</head>
<body>
Success, ${name}<br/>
</body>
</html>
```

####  SpringMVCJavaConfig.java
![](https://i.imgur.com/3RTkW6p.png)
![](https://i.imgur.com/VhcZRUt.png)
![](https://i.imgur.com/X9xiwHV.png)
![](https://i.imgur.com/yeDPtiq.png)
```clike=
package tw.leonchen.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "tw.leonchen")
public class SpringMVCJavaConfig implements WebMvcConfigurer {

	@Override
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}
    
}

```
#### DemoDispatcherServletInitializer
![](https://i.imgur.com/gkCs5MN.png)
![](https://i.imgur.com/ZTkA2Xo.png)
```clike=
package tw.leonchen.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class DemoDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	@Override
	protected Class<?>[] getRootConfigClasses() {
		return null;
	}

	@Override
	protected Class<?>[] getServletConfigClasses() {
		return new Class[] {SpringMVCJavaConfig.class};
	}

	@Override
	protected String[] getServletMappings() {
		return new String[]{"/"};
	}

}

```

#### HelloController2
![](https://i.imgur.com/Mp75Hun.png)
```clike=
package tw.leonchen.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HelloController2 {

	@RequestMapping(path = "/hello.controller2", method = RequestMethod.GET)
	public String processAction(HttpServletRequest request, HttpServletResponse response, Model m) { //4.
		
		String userName = request.getParameter("userName");
		Map<String, String> errors = new HashMap<String, String>();
		m.addAttribute("errors", errors);  //1.
				
		if(userName==null || userName.length()==0) {
			errors.put("myName", "name is required");
		}
		
		if(errors!=null && !errors.isEmpty()) {
			return "/form.jsp";   //2.
		}
		
		HttpSession session = request.getSession();
		session.setAttribute("name", userName);
		return "/success.jsp";  //3.
	}
	
}

```

# 28 SpringMVC動態網站開發實務(框架如何處理請求)
![](https://i.imgur.com/hNFH2sm.jpg)
![](https://i.imgur.com/D1QuKcF.png)
***
![](https://i.imgur.com/EH3NuBS.png)

# 29 SpringMVC動態網站開發實務
![](https://i.imgur.com/jxGlQcW.png)
***
![](https://i.imgur.com/Z6pLixN.png)

# 30 SpringMVC動態網站開發實務
![](https://i.imgur.com/TZffAFp.png)
 ***
![](https://i.imgur.com/Z6tVXmw.png)
* **View**
    * ![](https://i.imgur.com/MJOkXSv.png)

# 31 SpringMVC動態網站開發實務
![](https://i.imgur.com/cbZjIIK.png)
***
![](https://i.imgur.com/mcxC7xp.png)

# 32 SpringMVC動態網站開發實務
![](https://i.imgur.com/xldYDWj.png)
***
![](https://i.imgur.com/0ZpyqEI.png)
![](https://i.imgur.com/nk4z4Z6.png)

# 33 SpringMVC動態網站開發實務
![](https://i.imgur.com/dxLddH5.png)
![](https://i.imgur.com/Orw2cEb.png)
![](https://i.imgur.com/zYY6SRF.png)
***
![](https://i.imgur.com/ONmwMBG.png)

# 34 SpringMVC動態網站開發實務
![](https://i.imgur.com/65Est6T.png)
***
![](https://i.imgur.com/TDipI56.png)

# 35 SpringMVC動態網站開發實務
![](https://i.imgur.com/4KWt18s.png)
[Model](https://docs.spring.io/spring-framework/docs/current/javadoc-api/)
***
![](https://i.imgur.com/TQ85PZB.png)

# 36 SpringMVC動態網站開發實務
![](https://i.imgur.com/iAbrXgc.png)

# 37 SpringMVC動態網站開發實務(控制器方法取出瀏覽器送來的資料)
![](https://i.imgur.com/gWbydap.png)
***
![](https://i.imgur.com/4RtwtIo.png)

# 38 SpringMVC動態網站開發實務
![](https://i.imgur.com/GBH9bmN.png)

#### profiles.jsp
![](https://i.imgur.com/KWk0k2b.png)
```clike=
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Profiles</title>
</head>
<body>
<h3>Profiles</h3>
<form action="xxxController" method="post">
    <p>
    Name:<input type="text" name="userName"/><br/>
    Age:<input type="text" name="userAge"/><br/>
    </p>
    <input type="submit" value="Send">
</form>
</body>
</html>
```

#### ProfilesController
![](https://i.imgur.com/29R1V6X.png)
```clike=
package tw.leonchen.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class ProfilesController {

	@RequestMapping(path = "/profiles.controller", method = RequestMethod.POST)
	public String processAction(@RequestParam(name = "userName") String user, @RequestParam(name = "userAge") String age, Model m) {
        
		m.addAttribute("myUser", user);
        m.addAttribute("myAge", age);
        
        return "/profilesResult.jsp";
	}
}
```
#### profilesResult.jsp
![](https://i.imgur.com/dX1e6UK.png)
```c=
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ProfilesResult</title>
</head>
<body>
Name:${myUser}<br/>
Age:${myAge}<br/>
</body>
</html>
```

### jsp位置
![](https://i.imgur.com/wbPhctC.png)
![](https://i.imgur.com/qxBUCnX.png)


# 38 SpringMVC動態網站開發實務(中文亂碼)
![](https://i.imgur.com/QJygqH1.png)
![](https://i.imgur.com/PKC2hwB.png)

#### web.xml
[CharacterEncodingFilter](https://docs.spring.io/spring-framework/docs/current/javadoc-api/)
```c=
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
	id="WebApp_ID" version="4.0">
	<display-name>SpringMvcWebProject</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>

	<filter>
		<filter-name>myCharacterEncoding</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>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>myCharacterEncoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<servlet>
		<servlet-name>mvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>mvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>
```



