# SpringMVC 3/2上課內容
###### tags: `SpringMVC`
[老師云端](https://1drv.ms/u/s!Ans47I9-PkivwH8KqgNszZ8OKcZe)
# 1 SpringMVC動態網站開發實務(目錄)


# 3 SpringMVC動態網站開發實務(框架)

***

# 4 SpringMVC動態網站開發實務


***
* 運作圖

# 5 SpringMVC動態網站開發實務

***

* DispatcherServlet:總機的角色(處理MVC的請求)
* HandlerMapping:通訊錄(發出請求找哪個Controller處理)
* ModelAndView:傳輸(相關資訊傳給總機)
* ViewResolver:代碼 URL(網址)
* View:顯示Model結果畫面
# 6 SpringMVC動態網站開發實務(架構請求流程[DispatcherServlet:總機])

[org.springframework.web.servlet](https://docs.spring.io/spring-framework/docs/current/javadoc-api/)
* 每個總機有每個通訊錄
# 7 SpringMVC動態網站開發實務

***

# 8 SpringMVC動態網站開發實務

[DispatcherServlet](https://docs.spring.io/spring-framework/docs/current/javadoc-api/)
* **load-on-startup**
* 數字越小越優先
***

# 9 SpringMVC動態網站開發實務

* 核心 DispatcherServlet:總機
***

* 集中處理機制
# 10 SpringMVC動態網站開發實務(重要)

***

[HandlerMapping](https://docs.spring.io/spring-framework/docs/current/javadoc-api/)
# 11 SpringMVC動態網站開發實務

[BeanNameUrlHandlerMapping](https://docs.spring.io/spring-framework/docs/current/javadoc-api/)
* **Context Root**
* (專案)環境根目錄
* 
***

# 12 SpringMVC動態網站開發實務

* 解析網址
***

而外:[反組譯混淆氣](https://ithelp.ithome.com.tw/articles/10044266)
# 13 SpringMVC動態網站開發實務

* 大部分都用預設
# 14 SpringMVC動態網站開發實務(無xml網路應用系統)

***

* ServletContainer:jre環境
# 15 SpringMVC動態網站開發實務

* web底下的package
***

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



* getRootConfigClasses 大的組態設定檔
* getServletConfigClasses 小的組態設定檔
* getServeletMappings 掌控大小


***

1. ? 大小
2. ? 不指定型別
3. "/" 網址都是自己控制的
# 17 SpringMVC動態網站開發實務

***

# 18 SpringMVC動態網站開發實務

1. 自動找
***

1. 攔截器
2. 代理人是誰
# 19 SpringMVC動態網站開發實務

***

# 20 SpringMVC動態網站開發實務

# 21 SpringMVC動態網站開發實務(實作Spring MVC)

***

### 新專案














#### 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章

***

***

***

***

***

***

#### mvc-servlet-xml.xml

```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

```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

```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




```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


```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

```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動態網站開發實務(框架如何處理請求)


***

# 29 SpringMVC動態網站開發實務

***

# 30 SpringMVC動態網站開發實務

***

* **View**
* 
# 31 SpringMVC動態網站開發實務

***

# 32 SpringMVC動態網站開發實務

***


# 33 SpringMVC動態網站開發實務



***

# 34 SpringMVC動態網站開發實務

***

# 35 SpringMVC動態網站開發實務

[Model](https://docs.spring.io/spring-framework/docs/current/javadoc-api/)
***

# 36 SpringMVC動態網站開發實務

# 37 SpringMVC動態網站開發實務(控制器方法取出瀏覽器送來的資料)

***

# 38 SpringMVC動態網站開發實務

#### profiles.jsp

```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

```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

```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位置


# 38 SpringMVC動態網站開發實務(中文亂碼)


#### 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>
```