抖音清理低俗詞語用作帳號暱稱,共查處 2.75 萬個違規帳號
阿新 • • 發佈:2021-12-06
第一步
第一步:新建一個Moudle,新增web支援!建立包結構com.my.it.controller
第二步:由於Maven可能存在資源過濾問題,我們將配置完善
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main.resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
第三步:在pom.xml檔案引入相關依賴
主要有Spring框架核心庫、Servlet,jstl等。
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.9.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies>
第四步:配置web.xml
注意點:
-
注意web.xml版本問題,要最新版。
-
註冊DispatcherServlet
-
關聯SpringMVC的配置檔案
-
啟動路徑為1
-
對映路徑為 /[不要為/*,會404]
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--註冊DispatcherServlet--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!--關聯一個springmvc的配置檔案:[servlet-name]-servlet--> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <!--啟動級別-1--> <load-on-startup>1</load-on-startup> </servlet> <!-- / 匹配所有的請求(不包括.jsp)--> <!-- /* 匹配所有的請求(包括.jsp)--> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
第五步:關聯一個springmvc的配置檔案
<?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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--自動掃描包,讓指定包下的註解生效,由IOC容器統一管理-->
<context:component-scan base-package="com.my.it.controller"/>
<!--讓SpringMVC不處理靜態資源 .css .js .html .mp3 .mp4-->
<mvc:default-servlet-handler/>
<!--
支援mvc註解驅動
在spring中一般採用@RequestMapping註解來完成對映關係
要想使@RequestMapping註解生效
必須向上下文註冊DefaultAnnotationHandlerMapping
和要一個AnnotationMethodHandlerAdapter例項
這兩個例項分別在類級別和方法級別處理
而annotation-driven配置幫助我們自動完成以上例項的注入
-->
<mvc:annotation-driven/>
<!--檢視解析器-->
<!--檢視解析器:DispatcherServlet給他的ModelAndView
1.獲取了ModelAndView的資料
2.解析ModelAndView的檢視名字
3.拼接檢視的名字,找到對應的檢視/WEB-INF/jsp/hello.jsp
4.將資料渲染到檢視上
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!--字首-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--字尾-->
<property name="suffix" value=".jsp"/>
</bean>
<!--Handler-->
</beans>
注意在檢視解析器中我們把所有的檢視都把所有的檢視都存放在/WEB-INF/目錄下,這樣可以保證檢視安全,因為這個目錄下的檔案,客戶端不能直接訪問。
第六步:建立Controller
編寫一個java控制類:com.my.controller.HelloController.
package com.my.it.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
//真實訪問地址:專案名/HelloController/hello
@RequestMapping("/hello")
public String hello(Model model){
//封裝資料
/*向模型中新增msg與值,可以在JSP面中取出來並渲染*/
model.addAttribute("msg","Hello,SpringMVCAnnotation");
//web-info/jsp/hello.jsp
return "hello";//會被檢視解析器處理
}
}
- Controller是為了讓Spring IOC容器初始化時自動掃描到;
- RequestMapping是為了對映請求路徑,這裡因為類與方法都有對映所以訪問時應該時/HelloController/hello;
- 方法中宣告Model型別的引數時為了把Action中的資料帶到檢視中
- 方法返回的結果是檢視的名稱hello,加上配置檔案的前後綴WEB-INF/jsp/hello.jsp
第七步;建立檢視層
在WEB-INF/jsp/目錄中建立hello.jsp,檢視可以直接取出來展示從Controller帶回的資訊;
可以用EL表示取出Model中存放的值,或者物件:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
#{msg};
</body>
</html>
實現步驟其實非常的簡單:
1.新建一個web專案
2.匯入相關jar包
3.編寫web.xml,註冊DispatcherServlet
4.編寫springmvc配置檔案
5.接下來就是去建立對應的控制類,controller
6.最後完善前端檢視和controller之間的對應
7.測試執行
1.
使用springMVC必須配置的三大件:
處理器對映器、處理器介面卡、檢視解析器
通常,我們只需要手動配置檢視解析器,而處理對映器和處理介面卡只需要開啟註解驅動即可,而省去了大段的xml配置(而且只需要一種方法負責會失效)