SpringMVC_Day01
项目结构


//SpringMVC配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- spring的配置文件 -->
<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
https://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.tz"></context:component-scan>
<!-- 配置视图解析器:能够帮助拼接页面地址 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans> //index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="hello">hello</a> <h1>requestMappring</h1>
<a href="coco/handle01">注解</a>
<h1>requestMappring属性</h1>
<a href="coco/handle02">method属性</a>
<a href="coco/handle03?userName=xixi">params属性</a> <form action="" ></form>
</body>
</html>
//web.xml <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>springmvc_Day01</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- <init-param>
<param-name>contextConfigLocation</param-name>
指定springmvc配置文件的位置
<param-value>classpath:springmvc.xml</param-value>
</init-param> -->
<!-- 如果在web.xml不指定文件位置,也会找默认的文件
/WEB-INF/xxx-servlet.xml
-->
<!-- 启动加载时期 -->
<!-- servlet是当服务器启动的时候加载创建对象,值越小,代表优先级越高,就越先创建对象 -->
<!-- <init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param> -->
<!-- 如果在web.xml中找不到文件位置,也会默认找文件
/WEB-INF/xxx-servlet.xml
-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
<!--
/和/*都是用来拦截所有请求,但是/*的范围更大,还会拦截到*.jsp请求,一旦拦截,则访问jsp页面就不会显示了
/也会拦截所有请求,但是不会拦截.jsp,可以保证jsp页面访问正常 *.do *.action *.hah
/:不拦截jsp *.jsp
/*:拦截jsp
-->
</servlet-mapping>
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<!-- 这里将.jsp也过滤掉 -->
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>
package com.tz.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
一个普通的java类想要变成一个控制器
就需要告诉springmvc,通过@Controller *
*/
@Controller
public class FirstController {
/**
* 定义方法接收请求
* 一个普通的方法怎么知道是处理什么样的请求?
* 通过@RequestMapping()来声明要处理什么样的请求
*
* @RequestMapping:
* 告诉springmvc这个方法用来处理什么样的请求,
*/
@RequestMapping("/hello")
public String fController(){
System.out.println("0.0");
// return "/WEB-INF/pages/success.jsp";//页面的地址
return "success";//转发操作
//最终 前缀 +返回值+后缀
// /WEB-INF/pages/success/.jsp
} }
package com.tz.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; @Controller
@RequestMapping("/coco")
public class RequestMappingController {
/*
method:限定方法请求方式:get post
,method=RequestMethod.POST,只接收这种类型的请求,默认是什么类型请求都可以接受
params:规定请求参数
headers:规定请求头
consumes:只接收内容类型是那种的请求
produces:告诉浏览器返回的内容类型是什么,
*/
@RequestMapping("/handle01")
public String handle01(){
System.out.println("RequestMappingController....handle01");
return "success";
} @RequestMapping(value="/handle02",method=RequestMethod.GET)
public String handle02(){
System.out.println("RequestMappingController....handle01");
return "success";
}
@RequestMapping(value="/handle03",params={"userName!=xixi","pwd","!age"})
public String handle03(){
System.out.println("RequestMappingController....handle03");
return "success";
}
//User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36
@RequestMapping(value="/handle04",headers={"User-Agent=Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36"})
public String handle04(){
System.out.println("RequestMappingController....handle03");
return "success";
}
}
package com.tz.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class RequestMappingController2 { /**
* ANT风格的url:将url地址可以写模糊的通配符
*
* ?:能够替代任意一个字符
* *:能够替代任意多个字符和一层路径
* **:能替代多层路径
*/
@RequestMapping("/antURL01")//精确url
public String antURL01(){
System.out.println("antURL01");
return "success";
}
//模糊和精确匹配多个情况下,精确优先
@RequestMapping("/antURL0?")
public String antURL02(){
System.out.println("antURL02");
return "success";
} @RequestMapping("/antURL0*")
public String antURL03(){
System.out.println("antURL03");
return "success";
}
@RequestMapping("/c*/antURL04")
public String antURL04(){
System.out.println("antURL04");
return "success";
}
@RequestMapping("/c/**/antURL05")// /c/x/x/x/x/x/x /antURL05
public String antURL05(){
System.out.println("antURL05");
return "success";
} /*
*@PathVariable:路径变量 /user/coco /user/admin
*
*路径上可以有占位符,可以在任意路径下写{变量名}
*/
@RequestMapping("/user/{userName}")
public String variable(@PathVariable("userName") String userName){
System.out.println(userName);
return "success";
} }
* /cource get请求 --->查询
* /cource put请求 --->修改
* /cource delete请求-->删除
* /cource post请求-->新增
package com.tz.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; @Controller
public class CourceController {
//rest:资源的状态转化,规定url地址,增删改查
/*
* 修改课程:
* updateCource?id=1&name=java
* 查询
* getCource?id=1
* 新增:
* insertCource
* 删除:
* deleteCource
*
* get post put delete
*
* /cource get请求 --->查询
* /cource put请求 --->修改
* /cource delete请求-->删除
* /cource post请求-->新增
*
* 希望以非常简洁的url地址来发送请求,怎么样表示对一个资源的增删改查
* 可以直接用请求方式来区分
*
* 以简洁的url地址提交请求,以请求方式来区分对资源的操作
*
*
*
* 页面无法直接发送delete或者put请求,
* 1.springmvc中有一个过滤器Filter,可以吧普通的请求转化为规定形式的请求
* 2.怎么发送其他形式的请求?
* 2.1 创建post类型的表单,
* 2.2 表单项中携带一个参数:_method
* 2.3 这个参数的值就是delete或者put
*/
//查询课程
@RequestMapping(value="/cource/{cid}",method=RequestMethod.GET)
public String getCource(@PathVariable("cid")Integer cid){
System.out.println("查询了课程");
return "success";
} @RequestMapping(value="/cource",method=RequestMethod.POST)
public String insertCource(){
System.out.println("新增了课程");
return "success";
} @RequestMapping(value="/cource/{cid}",method=RequestMethod.PUT)
public String updateCource(@PathVariable("cid")Integer cid){
System.out.println("修改了课程");
return "success";
} @RequestMapping(value="/cource/{cid}",method=RequestMethod.DELETE)
public String deleteCource(@PathVariable("cid")Integer cid){
System.out.println("删除了课程");
return "success";
}
}
//rest.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body> <a href="cource/1">查询课程</a> <form action="cource" method="post">
<input type="submit" value="新增课程">
</form> <form action="cource/1" method="post">
<input name="_method" value="put">
<input type="submit" value="修改课程">
</form> <form action="cource/1" method="post">
<input name="_method" value="delete">
<input type="submit" value="删除课程">
</form> </body>
</html>
总结:
前端控制器,拦截所有的请求,并且进行智能派发 1.导入依赖
2.在web.xml配置前端控制器DispatcherServlet
3.写控制器和处理请求方法 流程:
1.在浏览器点击超链接发送hello请求
http://localhost:8080/springmvcday01/hello
2.来到了tomcat服务器
3.前端控制器收到请求进行处理
4.前端控制器根据请求地址和控制器中@RequestMapping注解标注的地址哪个匹配,
就找到哪个类的哪个方法来进行处理
5.前端控制器找到了目标处理器类和目标方法,利用反射技术执行目标方法
6.方法执行完之后,有返回值,springmvc认为这个返回值就是要去的页面地址
7.拿到方法的返回值之后,用视图解析器进行拼接字 为什么直接访问html访问不到?
1.服务器的web.xml中有个默认配置
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>(父) 用来处理静态资源 如果我们项目中有html,交给tomcat进行处理的话,那么找到对应的资源
</servlet-mapping>
2.我们项目中的前端控制器 <url-pattern>/</url-pattern>(子) 子和父都有相同的配置情况下,最终指定的子,那么不会请求到tomcat中去,
前端控制器会去找哪个方法上的RequestMapping
注解上声明的 index.html 为什么jsp可以访问到
1.tomcat。web.xml配置
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>(父) 2.项目配置
<url-pattern>/</url-pattern>(子)
子没有覆盖父,子处理不了的,交给父进行处理,父找到相应的资源直接进行返回 而如果项目配置成这样:
<url-pattern>*.jsp</url-pattern>(子)
子和父都有相同的配置情况下,最终指定的子,那么不会请求到tomcat中去,
前端控制器会去找哪个方法上的RequestMapping注解上声明的 index.jsp
SpringMVC_Day01的更多相关文章
- SpringMVC--入门案例
一.SpringMVC介绍 SpringMVC和Struts都属于表现层框架, 是Spring的一部分,Spring的整体结构如下: 1.1 SpringMVC的处理流程 下图是SpringMVC的执 ...
- SpringMVC之springmvc原始api,请求中文乱码问题
先搞一波效果图 1.Controller package com.tz.controller; import javax.servlet.http.HttpServlet; import javax ...
随机推荐
- C++数组常用操作
1. 遍历数组 使用基于范围的for循环来遍历整个数组 用_countof()来得到数组中的元素个数 #include <iostream> #include <cstdio> ...
- ZJNU 1699 - Bits
可得应当优先寻找最大的2^n-1这个数 如果l的位数不等于r的位数,那么这个数 2^n-1 就是最优解(每一位全为1) 如果l和r的位数相同,先看r是否符合 2^n-1,符合直接返回,不符合的话拆除最 ...
- 时间API
1. 时间API 我们的时间在java里是long类型的整数,这个整数称之为时间戳(也叫格林威治时间),即从1970-01-01到现在为止所经过的毫秒数,单有这个时间戳是不能准确表达世界各地的时间,还 ...
- LeetCode——264. 丑数 II
编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 ...
- 4)PHP命名规则,传值方式
(1)命名规则: 包括变量名,类名,接口名函数名等等 ①基本规则: 只能使用小写字母,下划线或者数字 数字不能开头 不能跟环境和系统关键字重复(比如,if,else,function) ② 驼峰式 ...
- 17.3.16---python内建函数
内置函数,无需import,任何时候都可以直接被使用 1------ Python针对众多的类型,提供了众多的内建函数来处理(内建是相对于导入import来说的,后面学习到包package时,将会介绍 ...
- ZZJ_淘淘商城项目:day02(淘淘商城01 - 项目讲解、环境搭建)
在用Eclipse的开发中,可手动排除不必要的依赖坐标传递. <!-- JPA的1.0依赖 --> <dependency> <groupId>javax.pers ...
- [LC] 90. Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- netstat - 系统信息
netstat - 系统信息 注意:如果是勘验或者验证漏洞,需要验证netstat程序的完整性(netstat程序是否被修改过). # 老版本的CentOS中会自带这个软件包,新版的7有的时候需要单独 ...
- CentOS6与CentOS7的启动过程
Linux启动流程CentOS6的启动流程Systemd概述Systemd初始化进程Systemd目标名称systemd服务管理 linux系统的组成:内核+跟文件系统 内核可实现以下功能:进程管理. ...