当前位置: 首页 > news >正文

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>

 

 

 

 

http://www.lryc.cn/news/326818.html

相关文章:

  • 【动手学深度学习】深入浅出深度学习之线性神经网络
  • 2024/3/26 C++作业
  • LinkedList讲解指南
  • IP如何异地共享文件?
  • HCIA-Datacom H12-811 题库补充(3/28)
  • 轻量级富文本编辑 Trumbowyg —— 基于 jQuery 插件配置
  • 那些王道书里的题目-----计算机网络篇
  • 【前端学习——js篇】 10.this指向
  • 项目搭建之统一返回值
  • 嵌入式和 Java 走哪条路?
  • C++ 控制语句(一)
  • mysql 用户管理-权限表
  • 【Postman如何进行接口测试简单详细操作实例】
  • docker搭建Project Calico环境
  • pyecharts操作一
  • 『Apisix进阶篇』动态负载均衡:APISIX的实战演练与策略应用
  • 【开发篇】十一、GC调优的分析工具
  • SpringCloudConfig 使用git搭建配置中心
  • c#基础-引用类型和值类型的区别
  • 面试题-3.20
  • glibc内存管理ptmalloc - 多线程内存管理
  • 区块链食品溯源案例实现(一)
  • 4S店车辆管理系统的设计与实现|Springboot+ Mysql+Java+ B/S结构(可运行源码+数据库+设计文档)
  • SpringBoot+Prometheus+Grafana实现应用监控和报警
  • 10 - Debian如何让特定用户切换root身份
  • HPT发布HyperGAI 多模态大模型:性能领先GPT-4V,全面胜过Gemini Pro
  • 汇春科技之MDT10F684
  • 【Vue3笔记01】如何使用Vue3和Vite搭建前端项目的基础开发环境
  • 软考高级架构师:信息安全概念和例题
  • Lilishop商城(windows)本地部署【docker版】