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)文章
spring事務(wù)@Transactional失效原因及解決辦法小結(jié)
今天就跟大家聊聊有關(guān)spring中@Transactional失效原因及解決辦法小結(jié),主要從三個(gè)方面考慮,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08
Java中IO流使用FileWriter寫數(shù)據(jù)基本操作詳解
這篇文章主要介紹了Java中IO流FileWriter寫數(shù)據(jù)操作,FileWriter類提供了多種寫入字符的方法,包括寫入單個(gè)字符、寫入字符數(shù)組和寫入字符串等,它還提供了一些其他的方法,如刷新緩沖區(qū)、關(guān)閉文件等,需要的朋友可以參考下2023-10-10
springboot如何配置上傳文件的maxRequestSize
這篇文章主要介紹了springboot如何配置上傳文件的maxRequestSize,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
springboot-jpa的實(shí)現(xiàn)操作
這篇文章主要介紹了springboot-jpa的實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
詳解IntelliJ IDEA2020.1和JDK14體驗(yàn)
這篇文章主要介紹了詳解IntelliJ IDEA2020.1和JDK14體驗(yàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
SpringBoot基于Swagger2構(gòu)建API文檔過(guò)程解析
這篇文章主要介紹了SpringBoot基于Swagger2構(gòu)建API文檔過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
Mybatis-Plus讀寫Mysql的Json字段的操作代碼
這篇文章主要介紹了Mybatis-Plus讀寫Mysql的Json字段的操作代碼,文中通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
SpringBoot集成Redis—使用RedisRepositories詳解
這篇文章主要介紹了SpringBoot集成Redis—使用RedisRepositories詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Nacos docker單機(jī)模式部署實(shí)現(xiàn)過(guò)程詳解
這篇文章主要介紹了Nacos docker單機(jī)模式部署實(shí)現(xiàn)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09

