Spring_MVC

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_4_0.xsd"
         version="4.0">
<!--    配置SpringMVC前端控制器DispatcherServlet-->
    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--        设置SpringMVC配置文件的位置和名称-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
<!--        将DispatcherServlet的初始化时间提前到服务器启动时-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

 spring配置文件

<?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"
       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">
<!--    扫描控制层组件-->
    <context:component-scan base-package="com.zmx.controller"></context:component-scan>
<!--    配置Thymeleaf视图解析器-->
    <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="order" value="1"/>
        <property name="characterEncoding" value="UTF-8"/>
        <property name="templateEngine">
            <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                <property name="templateResolver">
                    <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<!--                        视图前缀-->
                        <property name="prefix" value="/WEB-INF/templates/"/>
<!--                        视图后缀-->
                        <property name="suffix" value=".html"/>
                        <property name="templateMode" value="HTML5"/>
                        <property name="characterEncoding" value="UTF-8"/>
                    </bean>
                </property>
            </bean>
        </property>
    </bean>
</beans>

 controller控制层

package com.zmx.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
//@RequestMapping(value = {"/"})
public class HelloController {
    //@GetMapping()
    //@PostMapping()
    //@DeleteMapping()
    //PutMapping()
    @RequestMapping("/")
    public String index(){
        //将逻辑视图返回
        return "index";
    }
    @RequestMapping(
            value = {"/hello","/world"},
            method = {RequestMethod.POST,RequestMethod.GET},
            params = {"username","!password","age=20","gender!=女"},
            headers = {"referer"}

    )

    public String hello(){
        return "success";
    }
//    ?任意单个字符
//    /he?lo/world/ant
//    *任意个数的任意字符
//    /he*lo/world/ant
//    **任意层数的任意目录,写在双斜线之间,两侧不能有任意字符
//    /**/world/ant
    @RequestMapping(value = {"/he?lo/wo*ld/**/"})
    public String testAnt(){
        return "success";
    }
    @RequestMapping(value = {"hello/world/{username}/{password}"})
    public String testRest(@PathVariable("username") String username,@PathVariable("password") Integer password){
        System.out.println("username = " + username + ", password = " + password);
        return "success";
    }
}

view层 

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>hello world!</h1>
<a th:href="@{/hello}">hello</a>
<a th:href="@{/world}">hello</a>
<form th:action="@{/hello?username=admin&age=20&gender='男'}" method="post">
    <input name="hello" type="submit" value="访问">
</form>
<a th:href="@{hello/world/ant}">ant</a>
<a th:href="@{hello/world/zmx/2213}">admin</a>
<!--<form th:action="@{/hello(username='admin'&age='20'&gender='男')}" method="post">-->
<!--    <input name="hello" type="submit" value="访问">-->
<!--</form>-->
</body>
</html>

 

 

 

 

相关推荐

  1. <span style='color:red;'>SpringMVC</span>

    SpringMVC

    2024-03-28 05:04:02      62 阅读
  2. <span style='color:red;'>SpringMVC</span>

    SpringMVC

    2024-03-28 05:04:02      54 阅读
  3. <span style='color:red;'>SpringMVC</span>

    SpringMVC

    2024-03-28 05:04:02      53 阅读
  4. <span style='color:red;'>SpringMVC</span>

    SpringMVC

    2024-03-28 05:04:02      61 阅读
  5. <span style='color:red;'>springMVC</span>

    springMVC

    2024-03-28 05:04:02      47 阅读
  6. <span style='color:red;'>SpringMVC</span>

    SpringMVC

    2024-03-28 05:04:02      39 阅读
  7. <span style='color:red;'>SpringMVC</span>

    SpringMVC

    2024-03-28 05:04:02      42 阅读
  8. <span style='color:red;'>SpringMVC</span>

    SpringMVC

    2024-03-28 05:04:02      41 阅读
  9. SpringMVC

    2024-03-28 05:04:02       43 阅读
  10. <span style='color:red;'>springMVC</span>

    springMVC

    2024-03-28 05:04:02      40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-28 05:04:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-28 05:04:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-28 05:04:02       87 阅读
  4. Python语言-面向对象

    2024-03-28 05:04:02       96 阅读

热门阅读

  1. BaseDao封装增删改查(超详解!)

    2024-03-28 05:04:02       44 阅读
  2. docker初识

    2024-03-28 05:04:02       47 阅读
  3. RoCE v2中UDP的源端口和目的端口

    2024-03-28 05:04:02       46 阅读
  4. 【QT】QT的事件机制及其与信号机制的区别

    2024-03-28 05:04:02       35 阅读
  5. uniapp 返回上一页再进入当前页mounted不执行

    2024-03-28 05:04:02       46 阅读
  6. TCP/IP:互联网通信的核心协议

    2024-03-28 05:04:02       41 阅读
  7. 老项目接入kafka消费信息另一种方式

    2024-03-28 05:04:02       39 阅读
  8. 记录一次ubuntu网络传输大文件时出现断网现象

    2024-03-28 05:04:02       45 阅读
  9. centos 安装wget

    2024-03-28 05:04:02       36 阅读
  10. 关于对postcss安装和使用比较详细

    2024-03-28 05:04:02       40 阅读