• JAVA之springMVC


    POM

    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
    
        <groupId>com.zggroupId>
        <artifactId>springdemonartifactId>
        <version>1.0-SNAPSHOTversion>
        <packaging>warpackaging>
    
        <dependencies>
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webmvcartifactId>
                <version>5.3.1version>
            dependency>
    
            
            <dependency>
                <groupId>ch.qos.logbackgroupId>
                <artifactId>logback-classicartifactId>
                <version>1.2.3version>
            dependency>
    
            
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>javax.servlet-apiartifactId>
                <version>3.1.0version>
                <scope>providedscope>
            dependency>
    
            
            <dependency>
                <groupId>org.thymeleafgroupId>
                <artifactId>thymeleaf-spring5artifactId>
                <version>3.0.12.RELEASEversion>
            dependency>
        dependencies>
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    web.xml

    
    <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">
    
        
        <servlet>
            <servlet-name>SpringMVCservlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
            
            <init-param>
                <param-name>contextConfigLocationparam-name>
                <param-value>classpath:springmvc.xmlparam-value>
            init-param>
    
        servlet>
        <servlet-mapping>
            <servlet-name>SpringMVCservlet-name>
            <url-pattern>/url-pattern>
        servlet-mapping>
    
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    spring-mvc.xml

    
    <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.zg.springtest.controller">context:component-scan>
    
        
        
        <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    Controller

    package com.zg.springtest.controller;
    
    import com.sun.org.glassfish.gmbal.ParameterNames;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.*;
    
    import javax.servlet.http.HttpServletRequest;
    
    /**
     * @Auther: zhaoss
     * @Date: 2022/8/6 - 08 - 06 - 2:31
     * @Description: com.zg.springtest.controller
     * @version: 1.0
     */
    
    @Controller
    public class MyController {
    
        @RequestMapping("/")
        public String Protal()
        {
            return "index";
        }
    
        //p127
        @RequestMapping(value = {"/hello","/abc"},method = RequestMethod.POST)
        public String ToSuccess()
        {
            return "success";
        }
    
        @PostMapping(
                value = {"/hello","/abc"},
                params = {"username"}, //必须有username才能访问
                headers = {"reference"} //必须有来源页
                )
    
        public String ToSuccess2()
        {
            return "success";
        }
    
        @GetMapping(value = {"/hello","/abc"},params = {"username","!password","age=20"})
        public String ToSuccess3()
        {
            return "success";
        }
    
        //路径里面有? 代表可以用任意字符替代
        @RequestMapping("/a?c/test")
        public String ToSucccess4()
        {
            return "success";
        }
    
        //RestFul
    
        @RequestMapping("test/rest/{id}/{username}")
        public String testRest(@PathVariable("id") Integer id, @PathVariable("username") String username)
        {
            System.out.println("id, username"+ id+username);
            return "success";
        }
    
        //ServletAPI
        @RequestMapping("/param/ServletAPI")
        public String getParamByServletAPI(HttpServletRequest request)
        {
            String username = request.getParameter("username");
            String password = request.getParameter("password");
    
            System.out.println("username:"+username+",password:"+password);
    
            return "success";
        }
    
        //通过控制器获得
    
        @RequestMapping("/param")
        public String getParamByController(String username, String password)
        {
            System.out.println("username:"+username+",password:"+password);
    
            return "success";
        }
    
        //通过RequestParam绑定
        @RequestMapping("/param/RequestParam")
        public String getParamByRequestParam
                (
                        @RequestParam(value = "username",required = false) String username, String password
                )
        {
            System.out.println("username:"+username+",password:"+password);
    
            return "success";
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100

    web:

    DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>TTTTtesttitle>
    head>
    <body>
    <h1>helloworld!!!h1>
    <a th:href="@{/hello}">测试SpringMVCa>
    
    <a th:href="@{/abc}">测试SpringMVC_abca>
    
    <form th:action="@{/abc}" method="post">
        <input type="submit" value="测试@RequestMapping的注解Method属性abc">
    form>
    
    
    <form th:action="@{/abc?username=admin}" method="post">
        <input type="submit" value="测试带有参数parameter属性hello">
    form>
    
    <form th:action="@{/hello(username='admin')}" method="post">
        <input type="submit" value="测试带有参数parameter属性hello222">
    form>
    
    <a href="/hello">测试绝对路径a>
    
    <a th:href="@{/abc}">测试!parametersa>
    
    <a th:href="@{/adc/test}">测试?_abca>
    
    <a th:href="@{/test/rest/1/username}">测试restFula>
    
    <form th:action="@{/param/ServletAPI}" method="post">
        用户名:<input type="text" name="username"> <br>
        密码:<input type="password" name="password"> <br>
        <input type="submit" value="登录"> <br>
    
    <form th:action="@{/param}" method="post">
        用户名1:<input type="text" name="username"> <br>
        密码1:<input type="password" name="password"> <br>
        <input type="submit" value="登录1"> <br>
    form>
    
    <form th:action="@{/param/RequestParam}" method="post">
        用户名2:<input type="text" name="username"> <br>
        密码2:<input type="password" name="password"> <br>
        <input type="submit" value="登录2"> <br>
    form>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
    <h1>测试成功!!!!!!!!h1>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    游泳这项技术怎么学?
    ARM开发(7)系统移植初步(bootloader的选择和移植)基于cortex-A9的fs4412
    Ubuntu环境下安装OWT (Open WebRTC Toolkit)
    谷歌云 | 零售行业的生成式 AI:如何跟上步伐并取得领先
    基于Keras搭建CNN、TextCNN文本分类模型
    Android studio实现登录验证后返回token及用户信息,使用token获取用户信息并生成列表展示
    python 中import的用法:
    每次审查 OKR时,团队要讨论的12个启发性问题
    造车先做三蹦子-之三:自制数据集(6x6数据集)230103
    双十一“静悄悄”?VR购物拉满沉浸式购物体验
  • 原文地址:https://blog.csdn.net/helldoger/article/details/126221877