程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

SpringMVC(一)——核心类和注解

发布于2021-05-29 19:43     阅读(784)     评论(0)     点赞(8)     收藏(4)


RequestMapping

在这里插入图片描述

@RequestMapping(value="welcome",method=RequestMethod.POST,params= {"name=zs","age!=23","!height"})

要求请求链接:
method规定请求方式必须是:post
params参数规定:name属性值必须为zs,可以没有age属性,如果有age属性,则其值一定不能为23。不能出现height属性

合法请求:

localhost:8080/SPringMVCProject/welcome
name="zs" 
	@RequestMapping(value="welcome2",headers= {"Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Accept-Encoding=gzip, deflate"})
要求请求头:必须有上述headers属性值
	

@RequestHeader 和 @CookieValue 注解

	//获取请求头的指定属性
	@RequestMapping(value = "testRequestHeader", method=RequestMethod.GET)
	public String testRequestHeader(@RequestHeader("Accept") String ac) {
		System.out.println("请求头accept:"+ac);//请求头accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9

		return "success"; //返回 success页面
	}
	//获取cookieValue的值
	@RequestMapping(value = "testCookieValue", method=RequestMethod.GET)
	public String testCookieValue(@CookieValue("JSESSIONID") String js) {
		System.out.println("jseesionid:"+js); //jseesionid:FB9258C7DA7A98699C2ABC9491AC6146
		return "success"; //返回 success页面
	}

RequestParam 注解

@RequestParam(“uname”):接受前台传递的值,uname 属性不能没有,(但是可以通过require=false 来设置允许没有)等价于request.getParameter(“uname”);

@RequestParam(“uname”) String name,@RequestParam(value=“uage”,required=false,defaultValue=“23”)
:接受前台只传递uname属性,uage可以没有,如果没有有默认值为23.
required=false:该属性 不是必须的。
defaultValue=“23”:默认值23

ant风格

ant风格的请求路径

?  单字符
*  任意个字符(0或多个)
** 任意目录
@RequestMapping(value="welcome3/**/test") 
正确链接请求如:localhost:8080/SPringMVCProject/welcome3/xyz/abcz/asb/test


@RequestMapping(value="welcome4/a?c/test")
正确链接请求如:localhost:8080/SPringMVCProject/welcome4/axc/test


前端请求中有name属性,该属性的值被@PathVariable注解按名(name)获取传递给参数String name
@RequestMapping(value="welcome5/{name}")
public String  welcome5(@PathVariable("name") String name ) {
	System.out.println(name);
	return "success" ;//  views/success.jsp,默认使用了 请求转发的 跳转方式
}

使用对象来接受请求参数

实体类Student.java

package com.johnny.entity;

public class Student {
	private int id;
	private String name;
	private Address address ;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	
}	

Address.java

package com.johnny.entity;

public class Address {
	private String homeAddress ; 
	private String schoolAddress ;
	public String getHomeAddress() {
		return homeAddress;
	}
	public void setHomeAddress(String homeAddress) {
		this.homeAddress = homeAddress;
	}
	public String getSchoolAddress() {
		return schoolAddress;
	}
	public void setSchoolAddress(String schoolAddress) {
		this.schoolAddress = schoolAddress;
	}
	
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "address:["+ this.homeAddress+" " +this.schoolAddress+" ]";
	}
}

要求前端属性值必须实体类的属性名一一对应

	<form action="testObject" method="get">
		id:<input type="text" name="id">
		name:<input type="text" name="name">
		homeAddress:<input type="text" name="address.homeAddress">
		schoolAddress:<input type="text" name="address.schoolAddress">
		<input type="submit" value="查">
	</form>

使用Servlet类

在SpringMVC中使用原生态的Servlet API :HttpServletRequest :直接将 servlet-api中的类、接口等 写在springMVC所映射的方法参数中即可:




		@RequestMapping(value="testServletAPI")
		public String testServletAPI(HttpServletRequest  request,HttpServletResponse response) {
//			request.getParameter("uname") ;
			System.out.println(request);
			return "success" ;
		}

原文链接:https://blog.csdn.net/weixin_44855907/article/details/117297911



所属网站分类: 技术文章 > 博客

作者:我是个大美女

链接:http://www.javaheidong.com/blog/article/207079/685ac57a467e8588650f/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

8 0
收藏该文
已收藏

评论内容:(最多支持255个字符)