Spring MVC文件配置以及參數(shù)傳遞示例詳解
web.xml文件配置
創(chuàng)建好一個(gè)SpringMVC項(xiàng)目后,需要在需要在WB-INF文件夾下配置web.xml文件
<?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_3_1.xsd" version="3.1"> <display-name>SpringMVCdemo</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!--加載springMVC的配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:springMVC.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <!--中央核心控制器--> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <!--請(qǐng)求--> <url-pattern>*.do</url-pattern> </servlet-mapping> <!--過(guò)濾器,編碼格式--> <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> </web-app>
springMVC.xml文件配置
在src文件夾下創(chuàng)建springMVC.xml文件
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--自動(dòng)掃描上下文包--> <context:component-scan base-package="cn.zhc.*"></context:component-scan> <!--自動(dòng)開(kāi)啟MVC模式注解--> <mvc:annotation-driven></mvc:annotation-driven> <!--將請(qǐng)求映射到標(biāo)注@RequestMapping注解的控制器和處理方法上--> <mvc:default-servlet-handler></mvc:default-servlet-handler> <!--視圖解析器--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--前綴后綴--> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
第一個(gè)SpringMVC實(shí)例
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> 哈哈哈哈哈 </body> </html>
測(cè)試類:
package cn.zhc.test; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class Test { @RequestMapping("/hello.do") public String hello(){ System.out.println("hhhhhhhhhhhh"); return "index"; } }
在項(xiàng)目運(yùn)行后,在前端頁(yè)面路徑后輸入/hello.do,控制臺(tái)會(huì)輸出hhhhhhhhhhhh
參數(shù)傳遞
view到controller 四種方式
@RequestMapping("/hello.do") public String hello(String name){ //路徑后加?name= 不加會(huì)傳null System.out.println(name); return "index"; } //Controller方法方法中參數(shù)前加@RequestParam進(jìn)行直接入?yún)? @RequestMapping("/hello.do") public String hello(@RequestParam String name){ //不傳參會(huì)請(qǐng)求錯(cuò)誤400 System.out.println(name); return "index"; } @RequestMapping("/hello.do") public String hello(@RequestParam(value = "name" ,required = false) String name){ //required是否需要傳參 System.out.println(name); return "index"; } @RequestMapping(value = "/hello.do",method = RequestMethod.GET,params = "name") public String hello(String name){ //不傳參會(huì)請(qǐng)求錯(cuò)誤400 System.out.println(name); return "index"; }
controller到view 三種方式
@RequestMapping("/hello.do") public ModelAndView hello(){ ModelAndView mv = new ModelAndView(); mv.addObject("name","zhu");//添加模型數(shù)據(jù) mv.setViewName("index");//設(shè)置視圖名稱 return mv; } @RequestMapping("/hello.do") public String hello(Model model){ model.addAttribute("name","huai"); model.addAttribute("chang"); //在model中若不指定key,則使用默認(rèn)對(duì)象的類型作為key return "index"; } @RequestMapping("/hello.do") public String hello(Map<String,Object> map){ map.put("name","lisa"); return "index"; }
總結(jié)
到此這篇關(guān)于Spring MVC文件配置以及參數(shù)傳遞的文章就介紹到這了,更多相關(guān)SpringMVC文件配置參數(shù)傳遞內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中定時(shí)任務(wù)的6種實(shí)現(xiàn)方式
這篇文章主要給大家分享的是Java中定時(shí)任務(wù)的6種實(shí)現(xiàn)方式,幾乎在所有的項(xiàng)目中,定時(shí)任務(wù)的使用都是不可或缺的,如果使用不當(dāng)甚至?xí)斐少Y損,下面文章我們就來(lái)看看Java中定時(shí)任務(wù)的具體使用方式吧2021-10-10通過(guò)實(shí)例解析spring環(huán)繞通知原理及用法
這篇文章主要介紹了通過(guò)實(shí)例解析spring環(huán)繞通知原理及用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10java+selenium實(shí)現(xiàn)滑塊驗(yàn)證
現(xiàn)在越來(lái)越多的網(wǎng)站都使用采用滑塊驗(yàn)證來(lái)作為驗(yàn)證機(jī)制,用于判斷用戶是否為人類而不是機(jī)器人,本文就將利用java和selenium實(shí)現(xiàn)滑塊驗(yàn)證,希望對(duì)大家有所幫助2023-12-12java必學(xué)必會(huì)之方法的重載(overload)
java必學(xué)必會(huì)之方法的重載,介紹了方法的重載、構(gòu)造方法的重載,想要學(xué)好java方法的重載的朋友一定要好好閱讀這篇文章2015-12-12Java中遍歷數(shù)組使用foreach循環(huán)還是for循環(huán)?
這篇文章主要介紹了Java中遍歷數(shù)組使用foreach循環(huán)還是for循環(huán)?本文著重講解for語(yǔ)句的語(yǔ)法并給出使用實(shí)例,同時(shí)總結(jié)出盡量使用foreach語(yǔ)句遍歷數(shù)組,需要的朋友可以參考下2015-06-06Spring Boot實(shí)現(xiàn)微信小程序登錄
這篇文章主要為大家詳細(xì)介紹了Spring Boot實(shí)現(xiàn)微信小程序登錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04Java8語(yǔ)法糖之Lambda表達(dá)式的深入講解
這篇文章主要給大家介紹了關(guān)于Java8語(yǔ)法糖之Lambda表達(dá)式的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02