JAVA之springMVC

本文展示了如何在Spring MVC项目中配置Thymeleaf视图解析器,包括POM.xml、web.xml、spring-mvc.xml的配置,以及Controller的编写,涉及到@RequestMapping、@PathVariable等注解的使用,同时提供了Thymeleaf模板的简单示例。
摘要由CSDN通过智能技术生成

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘诺西亚的火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值