当前位置: 首页 > 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>

 

 

 

 

相关文章:

  • IP如何异地共享文件?
  • Spring实战:采用Spring配置文件管理Bean
  • 项目搭建之统一返回值
  • 【机器学习】包裹式特征选择之序列前向选择法
  • HCIP作业
  • ES6(一)箭头函数,解构赋值,模板字符串,let 和 const,类和继承
  • 数据结构——二叉搜索树详解
  • XUbuntu22.04之激活Linux最新Typora版本(二百二十五)
  • 以太网与数据链路层
  • 两个有序序列的中位数(全网首篇递归、分治解决)
  • Glide
  • 使用IDEA的反编译插件 反编译jar包
  • 开源博客项目Blog .NET Core源码学习(12:App.Application项目结构分析)
  • 快速上手Spring Cloud 十:Spring Cloud与微前端
  • Nexpose v6.6.243 for Linux Windows 下载 - 漏洞扫描
  • 《Javascript高级程序设计 (第三版)》第五章 引用类型
  • jquery ajax学习笔记
  • OpenStack安装流程(juno版)- 添加网络服务(neutron)- controller节点
  • Python爬虫--- 1.3 BS4库的解析器
  • python学习笔记-类对象的信息
  • SpringCloud(第 039 篇)链接Mysql数据库,通过JpaRepository编写数据库访问
  • Storybook 5.0正式发布:有史以来变化最大的版本\n
  • Webpack入门之遇到的那些坑,系列示例Demo
  • 关于for循环的简单归纳
  • 基于Mobx的多页面小程序的全局共享状态管理实践
  • 我的zsh配置, 2019最新方案
  • 再谈express与koa的对比
  • 深度学习之轻量级神经网络在TWS蓝牙音频处理器上的部署
  • [地铁译]使用SSD缓存应用数据——Moneta项目: 低成本优化的下一代EVCache ...
  • 关于Kubernetes Dashboard漏洞CVE-2018-18264的修复公告
  • #{}和${}的区别是什么 -- java面试
  • #控制台大学课堂点名问题_课堂随机点名
  • %3cli%3e连接html页面,html+canvas实现屏幕截取
  • (1)(1.9) MSP (version 4.2)
  • (51单片机)第五章-A/D和D/A工作原理-A/D
  • (libusb) usb口自动刷新
  • (附源码)springboot 智能停车场系统 毕业设计065415
  • (附源码)ssm基于微信小程序的疫苗管理系统 毕业设计 092354
  • (六)c52学习之旅-独立按键
  • (十五)Flask覆写wsgi_app函数实现自定义中间件
  • (四)图像的%2线性拉伸
  • (转)关于pipe()的详细解析
  • (转载)从 Java 代码到 Java 堆
  • ... 是什么 ?... 有什么用处?
  • ./configure,make,make install的作用(转)
  • .NET 编写一个可以异步等待循环中任何一个部分的 Awaiter
  • .net 打包工具_pyinstaller打包的exe太大?你需要站在巨人的肩膀上-VC++才是王道
  • .net安装_还在用第三方安装.NET?Win10自带.NET3.5安装
  • .NET开发不可不知、不可不用的辅助类(一)
  • .NET企业级应用架构设计系列之应用服务器
  • /usr/lib/mysql/plugin权限_给数据库增加密码策略遇到的权限问题
  • @Bean有哪些属性
  • @serverendpoint注解_SpringBoot 使用WebSocket打造在线聊天室(基于注解)
  • @Transaction注解失效的几种场景(附有示例代码)
  • [Avalon] Avalon中的Conditional Formatting.