初识 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)

接收null

修改代码: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

      

接收实体类

修改代码: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;

import com.neuedu.springmvc.po.User;

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

public class SpringmvcTest { 
	/*实体类*/                
	@RequestMapping("/testPojo")     //新增代码
	public String testPojo(User user) {
		System.out.println(user);       //新增代码
		
		return "success";
	}
	
	
	@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(支持连缀写法--address.province / address.city)

<%@ 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>
	<br></br>
	<form action="testPojo" method="post">         //新增代码 
		username: <input type="text" name="username"/><br>       //新增代码
		password: <input type="text" name="password"/><br>        //新增代码
		gender:   <input type="radio" name="gengder" value="male"/>男<br><input type="radio" name="gender" value="female"/>女<br>
		email: <input type="text" name="email"/><br>               //新增代码
		phone: <input type="text" name="phone"/><br>
province: <input type="text" name="address.province"/><br>
		city: <input type="text" name="address.city"/><br>  //新增代码
		<input type="submit" value="testPojo">      //新增代码
	</form>
</body>
</html>

新增代码:User.java

package com.neuedu.springmvc.po;

public class User {
	private String username;
	private String password;
	private String gender;
	private String email;
	private String phone;
	private Adderss address;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	public Adderss getAddress() {
		return address;
	}
	public void setAddress(Adderss address) {
		this.address = address;
	}
	@Override
	public String toString() {
		return "User [username=" + username + ", password=" + password + ", gender=" + gender + ", email=" + email
				+ ", phone=" + phone + ", address=" + address + "]";
	}
	
	
}

新增 Address.java

package com.neuedu.springmvc.po;

public class Adderss {
	private String province;
	private String city;
	public String getProvince() {
		return province;
	}
	public void setProvince(String province) {
		this.province = province;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	@Override
	public String toString() {
		return "Adderss [province=" + province + ", city=" + city + "]";
	}
	
	
}

输出:

  

注意:private String username;  中的 username 必须和  

(<form action="testPojo" method="post">   username: <input type="text" name="username"/><br>)中的 username 相同

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值