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 

  

原文地址:https://www.cnblogs.com/luyuan-chen/p/11671098.html