天天看點

JAVA之springMVC

POM

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>

    <groupId>com.zg</groupId>
    <artifactId>springdemon</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <!-- SpringMVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.1</version>
        </dependency>

        <!-- 日志 -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>

        <!-- ServletAPI -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- Spring5和Thymeleaf整合包 -->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
            <version>3.0.12.RELEASE</version>
        </dependency>
    </dependencies>

</project>
           

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
        SpringMVC的配置檔案預設的位置和名稱:
        位置:WEB-INF下
        名稱:<servlet-name>-servlet.xml,目前配置下的配置檔案名為SpringMVC-servlet.xml
        url-pattern中/和/*的差別:
        /:比對浏覽器向伺服器發送的所有請求(不包括.jsp)
        /*:比對浏覽器向伺服器發送的所有請求(包括.jsp)
    -->
    <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>

    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
           

spring-mvc.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: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>

    <!-- 配置Thymeleaf視圖解析器 -->
    <!--配置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.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";
    }

}

           

web:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>TTTTtest</title>
</head>
<body>
<h1>helloworld!!!</h1>
<a th:href="@{/hello}">測試SpringMVC</a>

<a th:href="@{/abc}">測試SpringMVC_abc</a>

<form th:action="@{/abc}" method="post">
    <input type="submit" value="測試@RequestMapping的注解Method屬性abc">
</form>

<!--?後面是parm參數,前面是路徑-->
<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}">測試!parameters</a>

<a th:href="@{/adc/test}">測試?_abc</a>

<a th:href="@{/test/rest/1/username}">測試restFul</a>

<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>
           
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>測試成功!!!!!!!!</h1>
</body>
</html>