初识 Spring(10)---(SpringMVC_requestMapping)

SpringMVC_requestMapping

项目搭建见上篇《初识 Spring(09)---(搭建SpringMVC项目)》

1.requestMapping既可以用在方法上,也可以用在类上

文件目录:

代码:新建SpringmvcTest.java

package com.neuedu.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/test")
/*这是类*/
public class SpringmvcTest {
	/*既可以用在方法上,也可以用在类上,这是方法*/
	@RequestMapping("/testRequestMapping")
	public String testRequestMapping() {
		System.out.println("testRequestMapping...");
		return "success";
	}
	
}

修改 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 Index</a>
	<br></br>
	<a href="test/testRequestMapping"> Hello testRequestMapping</a>  //修改代码
</body>
</html>

输出:

    

 

2.method=RequestMethod.POST 限定接收用什么方式的请求

修改代码:SpringmvcTest.java

package com.neuedu.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
//@RequestMapping("/test")
/*这是类*/

public class SpringmvcTest {

	@RequestMapping(value= {"/testUrl1","testUrl2"})   //新增代码(value可设置多个值)
	public String testUrl() {
		System.out.println("testUrl");
		return "success";
	}
	/*	method=RequestMethod.POST 限定接收用什么方式的请求*/           
	@RequestMapping(value="/testMethod",method=RequestMethod.POST)      //新增代码
	public String testMethod() {
		System.out.print("testMethod...");
		return "success";
	}
	/*既可以用在方法上,也可以用在类上,这是方法*/
	@RequestMapping("/testRequestMapping")
	public String testRequestMapping() {
		System.out.println("testRequestMapping...");
		return "success";
	}
	
}

修改 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 Index</a>
	<br></br>
	<a href="testRequestMapping"> Hello testRequestMapping</a>
	<br></br>
	
	<a href="testMethod"> Hello testMethod</a>
	<br></br>
	<form action="testMethod" method="post">       //修改代码
		<input type="submit" value="testMethod">   //修改代码
	</form>
	<br></br>
	<a href="testUrl1"> Hello testUrl1</a>         //修改代码
	<br></br>
	<a href="testUrl2"> Hello testUrl2</a>         //修改代码
</body>
</html>

输出:点击 Hello testMethod 超链接

                                                                         

点击 testMethod 按钮
      

修改代码:@RequestMapping(value="/testMethod",method=RequestMethod.GET

输出:

点击 Hello testMethod 超链接

                                                                         

点击 testMethod 按钮
      

  3.@RequestMapping:用来接收前端传递过来的数据

修改代码:SpringmvcTest.java

package com.neuedu.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
//@RequestMapping("/test")
/*这是类*/

public class SpringmvcTest {
  //RequestParam注解     新增代码
	@RequestMapping("/testParams")
	public String testParams(@RequestParam("name") String username,
			@RequestParam("pwd") String password) {          //新增代码
		System.out.println("username:" + username);
		System.out.println("password:" + password);
		return "success";                                     //新增代码
	}
	
	@RequestMapping(value= {"/testUrl1","testUrl2"})
	public String testUrl() {
		System.out.println("testUrl");
		return "success";
	}
	/*	method=RequestMethod.POST 限定接收用什么方式的请求*/
	@RequestMapping(value="/testMethod",method=RequestMethod.GET)
	public String testMethod() {
		System.out.print("testMethod...");
		return "success";
	}
	/*既可以用在方法上,也可以用在类上,这是方法*/
	@RequestMapping("/testRequestMapping")
	public String testRequestMapping() {
		System.out.println("testRequestMapping...");
		return "success";
	}
	
}

修改 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 Index</a>
	<br></br>
	<a href="testRequestMapping"> Hello testRequestMapping</a>
	<br></br>
	
	<a href="testMethod"> Hello testMethod</a>
	<br></br>
	<form action="testMethod" method="post"> 
		<input type="submit" value="testMethod">
	</form>
	<br></br>
	<a href="testUrl1"> Hello testUrl1</a>
	<br></br>
	<a href="testUrl2"> Hello testUrl2</a>              
	<br></br>
	<a href="testParams?name=zhang&pwd=123"> Hello testParams</a>     //修改代码
</body>
</html>

输出:点击 Hello testParams    控制台打印 username 和 password

   

补充:如果前端传来的 key 和方法中的参数名相同可以省略该注解

即 public String testParams(@RequestParam("name") String username, @RequestParam("pwd") String password)

可写为:public String testParams(String username,String password)

修改代码:SpringmvcTest.java

package com.neuedu.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
//@RequestMapping("/test")
/*这是类*/

public class SpringmvcTest {

	@RequestMapping("/testParamsRequired")     //修改代码
	public String testParamsRequired( @RequestParam (value="username",required=true) String username,Integer age) {
		System.out.println("testParamsRequired,username:" + username);
		System.out.println("testParamsRequired,age:" + age);
		return "success";                     //修改代码
	}
	
	
	@RequestMapping("/testParams")
	public String testParams(@RequestParam("name") String username,
			@RequestParam("pwd") String password) {
		System.out.println("username:" + username);
		System.out.println("password:" + password);
		return "success";
	}
	
	@RequestMapping(value= {"/testUrl1","testUrl2"})
	public String testUrl() {
		System.out.println("testUrl");
		return "success";
	}
	/*	method=RequestMethod.POST 限定接收用什么方式的请求*/
	@RequestMapping(value="/testMethod",method=RequestMethod.GET)
	public String testMethod() {
		System.out.print("testMethod...");
		return "success";
	}
	/*既可以用在方法上,也可以用在类上,这是方法*/
	@RequestMapping("/testRequestMapping")
	public String testRequestMapping() {
		System.out.println("testRequestMapping...");
		return "success";
	}
	
}

修改 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 Index</a>
	<br></br>
	<a href="testRequestMapping"> Hello testRequestMapping</a>
	<br></br>
	
	<a href="testMethod"> Hello testMethod</a>
	<br></br>
	<form action="testMethod" method="post"> 
		<input type="submit" value="testMethod">
	</form>
	<br></br>
	<a href="testUrl1"> Hello testUrl1</a>
	<br></br>
	<a href="testUrl2"> Hello testUrl2</a>
	<br></br>
	<a href="testParams?name=zhang&pwd=123"> Hello testParams</a>
	<br></br>
	<a href="testParamsRequired?username=zhang"> Hello testParamsRequired</a>   //新增代码
	
</body>
</html>

输出:点击 Hello testParamsRequired    控制台打印 username 和 age

      

猜你喜欢

转载自blog.csdn.net/jianghao233/article/details/81557041