HelloSpringMVC注解版實(shí)現(xiàn)步驟解析
注解版步驟
新建一個(gè)module,添加web的支持
由于Maven可能存在資源過濾的問題,我們將配置完善pom.xml
<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文件引入相關(guān)的依賴
主要有Spring框架核心庫(kù)、SpringMVC、servlet、JSTL等,我們?cè)诟敢蕾囍幸呀?jīng)引入了!
配置web.xml
注意點(diǎn):
- 注意web.xml版本問題,要最新版;
- 注冊(cè)DispatcherServlet
- 關(guān)聯(lián)SpringMVC的配置文件
- 啟動(dòng)級(jí)別為1
- 映射路徑為/【不要用/*】
配置springmvc配置文件和視圖解析器
我們把所有視圖都存放在/WEB-INF/目錄下,可以保證視圖安全,因?yàn)檫@個(gè)目錄下的文件,客戶端不能直接訪問。
<?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
https://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">
<!--自動(dòng)掃描包 讓指定包下的注解生效 由IOC容器統(tǒng)一管理-->
<context:component-scan base-package="com.kuang.controller"/>
<!--讓spring mvc不處理靜態(tài)資源 .css .js -->
<mvc:default-servlet-handler/>
<!--annotation-driven幫助我們自動(dòng)完成handlermapper和adapter實(shí)例的注入-->
<mvc:annotation-driven/>
<!--視圖解析器: 模版引擎 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
創(chuàng)建Controller
@Controller
@RequestMapping("/hello")
public class HelloController {
//真實(shí)訪問地址:項(xiàng)目名/hello/h1
@RequestMapping("/h1")
public String Hello(Model model){
model.addAttribute("msg","Hello SpringMVC annotation!");
return "hello"; //會(huì)被視圖解析器處理
}
}
創(chuàng)建視圖層
視圖可以直接取出并展示從Controller帶回的信息,可以通過EL表示取出Model中存放的值或者對(duì)象;
配置Tomcat運(yùn)行
小結(jié)
實(shí)現(xiàn)步驟其實(shí)很簡(jiǎn)單:
- 新建一個(gè)web項(xiàng)目
- 導(dǎo)入相關(guān)jar包
- 編寫web.xml,注冊(cè)DispatcherServlet
- 編寫springmvc配置文件
- 創(chuàng)建對(duì)應(yīng)的控制類,controller
- 完善前端視圖和controller之前的對(duì)應(yīng)
- 配置tomcat,測(cè)試運(yùn)行調(diào)試。
springMVC必須配置的三大件:
- 處理器映射器
- 處理器適配器
- 視圖解析器
通常只需要手動(dòng)配置視圖解析器,而處理器映射器和處理器適配器只需要開啟注解驅(qū)動(dòng)即可,省去了大段的xml配置。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringMVC 如何使用注解完成登錄攔截
- SpringMVC中RequestMapping注解(作用、出現(xiàn)的位置、屬性)
- 解決SpringMVC使用@RequestBody注解報(bào)400錯(cuò)誤的問題
- 使用注解開發(fā)SpringMVC詳細(xì)配置教程
- SpringMvc自定義攔截器(注解)代碼實(shí)例
- SpringMVC注解@RequestParam方法原理解析
- 使用SpringMVC的@Validated注解驗(yàn)證的實(shí)現(xiàn)
- 詳解springmvc常用5種注解
- 詳解SpringMVC注解@initbinder解決類型轉(zhuǎn)換問題
- springMVC自定義注解,用AOP來實(shí)現(xiàn)日志記錄的方法
- 聊聊springmvc中controller的方法的參數(shù)注解方式
- SpringMVC結(jié)構(gòu)簡(jiǎn)介及常用注解匯總
相關(guān)文章
mybatis 根據(jù)id批量刪除的實(shí)現(xiàn)操作
這篇文章主要介紹了mybatis 根據(jù)id批量刪除的實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
基于Java class對(duì)象說明、Java 靜態(tài)變量聲明和賦值說明(詳解)
下面小編就為大家?guī)硪黄贘ava class對(duì)象說明、Java 靜態(tài)變量聲明和賦值說明(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
SpringBoot整合jasypt實(shí)現(xiàn)敏感信息的加密詳解
一般公司的核心業(yè)務(wù)代碼中,都會(huì)存在與數(shù)據(jù)庫(kù)、第三方通信的secret key等敏感信息,如果以明文的方式存儲(chǔ),一旦泄露,那將會(huì)給公司帶來巨大的損失。本篇文章通過講解:Springboot集成Jasypt對(duì)項(xiàng)目敏感信息進(jìn)行加密,提高系統(tǒng)的安全性2022-09-09
Spring Shell 命令行實(shí)現(xiàn)交互式Shell應(yīng)用開發(fā)
本文主要介紹了Spring Shell 命令行實(shí)現(xiàn)交互式Shell應(yīng)用開發(fā),能夠幫助開發(fā)者快速構(gòu)建功能豐富的命令行應(yīng)用程序,具有一定的參考價(jià)值,感興趣的可以了解一下2025-04-04
使用Runnable實(shí)現(xiàn)數(shù)據(jù)共享
這篇文章主要為大家詳細(xì)介紹了如何使用Runnable實(shí)現(xiàn)數(shù)據(jù)共享,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Spring Data JPA的Audit功能審計(jì)數(shù)據(jù)庫(kù)的變更
數(shù)據(jù)庫(kù)審計(jì)是指當(dāng)數(shù)據(jù)庫(kù)有記錄變更時(shí),可以記錄數(shù)據(jù)庫(kù)的變更時(shí)間和變更人等,這樣以后出問題回溯問責(zé)也比較方便,本文討論Spring Data JPA審計(jì)數(shù)據(jù)庫(kù)變更問題,感興趣的朋友一起看看吧2021-06-06

