springMVC(二)-@RequestMapping映射及属性

一、传参

1、占位符传参

1.1、index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<a href="index/test/12/param">传参</a>
</body>
</html>

1.2、控制跳转类

@RequestMapping("/{id}/{name}") 通过占位符传参,并在方法的参数列表中,通过 @PathVariable("id")@PathVariable("name") 将占位符中所对应的值传入方法参数中


/**
 * Servlet implementation class Test
 */
@Controller
@RequestMapping("/index")
public class WelcomeServlet {
	
	/**
     * 传参
     * @param uid
     * @param uname
     * @return
     */
    @RequestMapping("/test/{id}/{name}")
    public String pathParam(@PathVariable("id")String uid,@PathVariable("name")String uname) {
    	System.out.println("---------------传参----------->"+uid+":"+uname);
    	return "welcome";
    }
    
}

执行输出结果:

---------------传参----------->12:param

2、@RequestParam 传参

2.1、index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="index/requestParams" method="get">
		<input type="text" name="uname" />
		<input type="text" name="uage" />
		<input type="submit" value="传参"/>
	</form>
</body>
</html>

2.2、控制跳转类

@RequestParam(value="uage",required = false,defaultValue = "12")
required = false:设置不是必须包含该属性,默认值为true
defaultValue = "12" :设置 uage 默认值为 12


/**
 * Servlet implementation class Test
 */
@Controller
@RequestMapping("/index")
public class WelcomeServlet {
	/**
     * 传参
     * @param uid
     * @param uname
     * @return
     */
    @RequestMapping("/requestParams")
    public String requestParams(@RequestParam("uname")String uname,@RequestParam(value="uage",required = false,defaultValue = "12")Integer uage) {
    	System.out.println("****************传参*******************"+uname+":"+uage);
    	return "welcome";
    }
}

二、@RequestMapping-method 设置请求方式

1、GET(查)

1.1、index.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="index/requestParamGet" method="get">
		<input type="submit" value="GET"/>
	</form>
</body>
</html>

1.2、控制器类

@RequestMapping(value={"/requestParamGet"},method = {RequestMethod.GET}) :通过 method = {RequestMethod.GET} 设置该方法的请求方式为 GET 请求


/**
 * Servlet implementation class Test
 */
@Controller
@RequestMapping("/index")
public class WelcomeServlet {
	/**
     * @RequestMapping参数--> get请求
     * @param uid
     * @param uname
     * @return
     */
    @RequestMapping(value={"/requestParamGet"},method = {RequestMethod.GET})
    public String requestParamGet() {
    	System.out.println("****************@RequestMapping参数GET*******************");
    	return "welcome";
    }
}

2、POST(增)

2.1、index.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="index/requestParamPOST" method="post">
		<input type="submit" value="POST"/>
	</form>
</body>
</html>

2.2、控制器类

@RequestMapping(value={"/requestParamPost"},method = {RequestMethod.POST}) :通过 method = {RequestMethod.POST} 设置该方法的请求方式为 POST 请求


/**
 * Servlet implementation class Test
 */
@Controller
@RequestMapping("/index")
public class WelcomeServlet {
	/**
     * @RequestMapping参数--> post请求
     * @param uid
     * @param uname
     * @return
     */
    @RequestMapping(value={"/requestParamPOST"},method = {RequestMethod.POST})
    public String requestParamPOST() {
    	System.out.println("****************@RequestMapping参数POST*******************");
    	return "welcome";
    }
}

2、DELETE(删) / PUT(改)

3.1、index.jsp

当请求的方法限制请求方式为 DELETE 时,前端需满足以下条件

1、请方式为 post

2、<input type="hidden" name="_method" value="DELETE"/> 请求中包含name=“_method” 和 value=“DELETE”


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="index/requestParamDelete" method="post">
		<input type="hidden"  name="_method" value="DELETE"/>
		<input type="submit" value="delete请求"/>
	</form>
	
	<form action="index/requestParamPut" method="post">
		<input type="hidden"  name="_method" value="PUT"/>
		<input type="submit" value="put请求"/>
	</form>
</body>
</html>

3.2、控制器类

1、@RequestMapping(value={"/requestParamDelete"},method = {RequestMethod.DELETE}) :通过 method = {RequestMethod.DELETE} 设置该方法的请求方式为 DELETE 请求

2、@RequestMapping(value={"/requestParamPut"},method = {RequestMethod.PUT}) :通过 method = {RequestMethod.PUT} 设置该方法的请求方式为 PUT 请求


/**
 * Servlet implementation class Test
 */
@Controller
@RequestMapping("/index")
public class WelcomeServlet {
	/**
     * @RequestMapping参数--> delete
     * @param uid
     * @param uname
     * @return
     */
    @RequestMapping(value={"/requestParamDelete"},method = {RequestMethod.DELETE})
    public String requestParamDelete() {
    	System.out.println("****************@RequestMapping参数DELETE*******************");
    	return "welcome";
    }
	/**
     * @RequestMapping参数--> put
     * @param uid
     * @param uname
     * @return
     */
    @RequestMapping(value={"/requestParamPut"},method = {RequestMethod.PUT})
    public String requestParamPut() {
    	System.out.println("****************@RequestMapping参数PUT*******************");
    	return "welcome";
    }
}

3.3、web.xml 文件中配置过滤器通过过滤器将post请求增强为post+delete/put请求

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>springmvc</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
  	<servlet-name>springDispatcher</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- 若不配置spring配置文件路径,则默认文件名为 springDispatcher-servlet.xml (servlet的name-servlet.xml)
  		默认路径为 WEB-INF/springDispatcher-servlet.xml-->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springmvc.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>springDispatcher</servlet-name>
  	<!-- 拦截所有请求,并交由springDispatcher控制请求-->
  	<url-pattern>/</url-pattern>
  	<!-- 拦截/index开头的请求,并交由springDispatcher控制请求
  	<url-pattern>/index</url-pattern>	-->
  	
  	<!-- 只拦截该求情,并交由springDispatcher控制请求
  	<url-pattern>/index/xx.jsp</url-pattern>	-->
  	
  	<!-- 只拦截.jsp结尾的请求,并交由springDispatcher控制请求
  	<url-pattern>.jsp</url-pattern>	-->
  </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>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
  
</web-app>

3.4、跳转页面 welcome.jsp

当请求为 delete / put 时由于 tomcat 版本过高(tomcat8.0以上版本的jsp页面会有某种约束不允许除了get\post以外的请求方式) 出现 JSP 只允许 GET、POST 或 HEAD。Jasper 还允许 OPTIONS 错误
在page中加入 isErrorPage=“true”%

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
		欢迎进入
</body>
</html>

三、@RequestMapping-params 设置请求参数

@RequestMapping(value={"/requestParamParam"},params = {"age"})

params = {“age”} 必须包含属性 name=“age”
params = {“age=21”} 必须包含属性name=“age” 且age值为 21
params = {“age!=21”} 如果包含属性name=“age”,则age值不能为 21
params = {“!age”} 不能包含属性 name=“age”

四、@RequestMapping-headers 设置请求头信息

@RequestMapping(value={"/requestParamHeaders"},headers = {"Accept-Encoding=XXX"})

headers = {“Accept-Encoding=XXX”} 指定请求头中的Accept-Encoding的值 必须为 XXX

五、获取请求信息

1、@RequestHeader(“Accept-Encoding”): 获取请求头中 Accept-Encoding 信息
2、@CookieValue(“JSESSIONID”) :获取 Cookie 信息

/**
     * 获取请求头中 Accept-Encoding 信息
     * @param uid
     * @param uname
     * @return
     */
    @RequestMapping(value={"/requestGetHeaders"})
    public String requestGetHeaders(@RequestHeader("Accept-Encoding")String header) {
    	System.out.println("****************获取请求头信息*******************"+header);
    	return "welcome";
    }
    /**
     * 获取 Cookie 信息
     * @param uid
     * @param uname
     * @return
     */
    @RequestMapping(value={"/requestGetCookie"})
    public String requestGetCookie(@CookieValue("JSESSIONID")String jssionId) {
    	System.out.println("****************获取jssionId*******************"+jssionId);
    	return "welcome";
    }

六、ant

1、index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<!-- 单个字符(/requestParamAnt/?/single) -->
	<form action="index/requestAnt/A/single" method="get">
		<input type="submit" value="requestAnt"/>
	</form>
	<!-- 任意字符(/requestParamAnt/*/single) -->
	<form action="index/requestAnt/Asddwdwd/single" method="get">
		<input type="submit" value="requestAnt"/>
	</form>
	<!-- 任意目录(/requestParamAnt/**/single) -->
	<form action="index/requestAnt/A/asda/Awd/sd/single" method="get">
		<input type="submit" value="requestAnt"/>
	</form>
</body>
</html>

2、控制器 类


/**
 * Servlet implementation class Test
 */
@Controller
@RequestMapping("/index")
public class WelcomeServlet {
	/**
     * @RequestMapping参数--> ant
     * ?	表示单个字符(/requestParamAnt/?/single)
     * *  	表示任意字符(/requestParamAnt/* /single)
     * ** 	表示任意目录(/requestParamAnt/** /single)
     * @param uid
     * @param uname
     * @return
     */
    @RequestMapping(value={"/requestAnt/?/single"})
    public String requestParamAnt1() {
    	System.out.println("****************Ant1*******************");
    	return "welcome";
    }
    
    @RequestMapping(value={"/requestAnt/*/single"})
    public String requestParamAnt2() {
    	System.out.println("****************Ant2*******************");
    	return "welcome";
    }
    
    @RequestMapping(value={"/requestAnt/**/single"})
    public String requestParamAnt3() {
    	System.out.println("****************Ant3*******************");
    	return "welcome";
    }
}

七、传递对象

1、index,jsp
若传递的是对象,在提交的表单中的name属性值需与对象中的属性名保持一致,若对象中还 引用了其他对象,可通过级联关联该引用对象中的属性;如stucard.cardId、stucard.cardName

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="index/requestStuInfo" method="post">
		<input type="text" name="name" />
		<input type="text" name="age" />
		<input type="text" name="stucard.cardId" />
		<input type="text" name="stucard.cardName" />
		<input type="submit" value="传递对象"/>
	</form>
</body>
</html>

2、控制器类

/**
     * 获取学生信息
     * 前台中的属性名必须与类中的属性名一致,自动注入到方法参数的对象中
     * 该类必须包含 setter与getter方法
     * @param uid
     * @param uname
     * @return
     */
    @RequestMapping(value={"/requestStuInfo"})
    public String requestStuInfo(Student student) {
    	System.out.println("****************获取学生信息*******************"+student.toString());
    	return "welcome";
    }

3、实体类

Student

public class Student {
	private String id;
	private String name;
	private StudentCard stucard;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public StudentCard getStucard() {
		return stucard;
	}
	public void setStucard(StudentCard stucard) {
		this.stucard = stucard;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", stucard=" + stucard + "]";
	}
}

StudentCard

public class StudentCard {
	private String cardId;
	private String cardName;
	public String getCardId() {
		return cardId;
	}
	public void setCardId(String cardId) {
		this.cardId = cardId;
	}
	public String getCardName() {
		return cardName;
	}
	public void setCardName(String cardName) {
		this.cardName = cardName;
	}
	@Override
	public String toString() {
		return "StudentCard [cardId=" + cardId + ", cardName=" + cardName + "]";
	}
}

八、使用原生 servlet

1、index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="index/requestAPI" method="post">
		<input type="submit" value="使用原生servlet"/>
	</form>
</body>
</html>

2、控制器类

在对应的方法中包含 HttpServletRequest 或 HttpServletResponse 即可

/**
 * Servlet implementation class Test
 */
@Controller
@RequestMapping("/index")
public class WelcomeServlet {
	/**
     * 使用原生servlet中的HttpServletRequest/HttpServletResponse
     * @param uid
     * @param uname
     * @return
     */
    @RequestMapping(value={"/requestAPI"})
    public String requestAPI(HttpServletRequest request,HttpServletResponse response) {
    	System.out.println("****************request*******************"+request);
    	System.out.println("****************response*******************"+response);
    	return "welcome";
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值