最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring MVC文件配置以及參數(shù)傳遞示例詳解

 更新時(shí)間:2021年03月23日 09:05:40   作者:朱懷昌  
這篇文章主要給大家介紹了關(guān)于Spring MVC文件配置以及參數(shù)傳遞的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

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>
    <!--請求-->
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <!--過濾器,編碼格式-->
  <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)開啟MVC模式注解-->
  <mvc:annotation-driven></mvc:annotation-driven>
  <!--將請求映射到標(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>

測試類:

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)行后,在前端頁面路徑后輸入/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ì)請求錯(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ì)請求錯(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)對象的類型作為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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • spring事務(wù)@Transactional失效原因及解決辦法小結(jié)

    spring事務(wù)@Transactional失效原因及解決辦法小結(jié)

    今天就跟大家聊聊有關(guān)spring中@Transactional失效原因及解決辦法小結(jié),主要從三個(gè)方面考慮,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-08-08
  • Java中IO流使用FileWriter寫數(shù)據(jù)基本操作詳解

    Java中IO流使用FileWriter寫數(shù)據(jù)基本操作詳解

    這篇文章主要介紹了Java中IO流FileWriter寫數(shù)據(jù)操作,FileWriter類提供了多種寫入字符的方法,包括寫入單個(gè)字符、寫入字符數(shù)組和寫入字符串等,它還提供了一些其他的方法,如刷新緩沖區(qū)、關(guān)閉文件等,需要的朋友可以參考下
    2023-10-10
  • springboot如何配置上傳文件的maxRequestSize

    springboot如何配置上傳文件的maxRequestSize

    這篇文章主要介紹了springboot如何配置上傳文件的maxRequestSize,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • springboot-jpa的實(shí)現(xiàn)操作

    springboot-jpa的實(shí)現(xiàn)操作

    這篇文章主要介紹了springboot-jpa的實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • 詳解IntelliJ IDEA2020.1和JDK14體驗(yàn)

    詳解IntelliJ IDEA2020.1和JDK14體驗(yàn)

    這篇文章主要介紹了詳解IntelliJ IDEA2020.1和JDK14體驗(yàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • SpringBoot基于Swagger2構(gòu)建API文檔過程解析

    SpringBoot基于Swagger2構(gòu)建API文檔過程解析

    這篇文章主要介紹了SpringBoot基于Swagger2構(gòu)建API文檔過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Mybatis-Plus讀寫Mysql的Json字段的操作代碼

    Mybatis-Plus讀寫Mysql的Json字段的操作代碼

    這篇文章主要介紹了Mybatis-Plus讀寫Mysql的Json字段的操作代碼,文中通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • SpringBoot集成Redis—使用RedisRepositories詳解

    SpringBoot集成Redis—使用RedisRepositories詳解

    這篇文章主要介紹了SpringBoot集成Redis—使用RedisRepositories詳解,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Nacos docker單機(jī)模式部署實(shí)現(xiàn)過程詳解

    Nacos docker單機(jī)模式部署實(shí)現(xiàn)過程詳解

    這篇文章主要介紹了Nacos docker單機(jī)模式部署實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • JAVA設(shè)計(jì)模式之責(zé)任鏈模式詳解

    JAVA設(shè)計(jì)模式之責(zé)任鏈模式詳解

    這篇文章主要介紹了JAVA設(shè)計(jì)模式之責(zé)任鏈模式詳解,需要的朋友可以參考下
    2015-04-04

最新評論

禹城市| 延川县| 天祝| 阿勒泰市| 桂阳县| 依兰县| 紫金县| 资源县| 奉化市| 乌恰县| 垣曲县| 九龙坡区| 宁安市| 普陀区| 梅州市| 泸水县| 东乡族自治县| 庄浪县| 高邮市| 四子王旗| 沙湾县| 静海县| 阿克陶县| 布尔津县| 准格尔旗| 五常市| 吴江市| 出国| 柯坪县| 延安市| 甘德县| 东方市| 龙游县| 广元市| 台湾省| 泸溪县| 汪清县| 通道| 巫山县| 奈曼旗| 上饶县|