[SpringMVC]夹带请求参数该如何操作@RequestParam/POJO对象绑定请求参数/原生ServletAPI

@RequestParam

处理get里携带参数【url...?参数1&参数2】怎么在后台取,因为@PathVariable 不够

如果觉得url允许不带参数而不报错,可以写上【required=false】属性

如果参数为int,可以写上defalutValue="0"即可

package com.yiki.Handle;

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

@RequestMapping("/params")
@Controller
public class RequstParams {
	private static final String SUCCESS = "success";

	@RequestMapping(value = "/testParams")
	public String testParams(@RequestParam(value = "name",required=false) String name, @RequestParam(value = "age",required=false) integer age) {
		System.out.println("testParams :"+"name= "+name+" age :"+age);
		return SUCCESS;
	}

}
	params

/@RequestHeader/@CookieValue/--->自行去了解

POJO

[SpringMVC]夹带请求参数该如何操作@RequestParam/POJO对象绑定请求参数/原生ServletAPI_第1张图片

package com.yiki.pojo;

public class User {
	private String username;
	private Integer psw;
	private Integer age;

	private Address address;//级联属性

	@Override
	public String toString() {
		return "User [username=" + username + ", psw=" + psw + ", age=" + age + ", address=" + address + "]";
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Integer getPsw() {
		return psw;
	}

	public void setPsw(Integer psw) {
		this.psw = psw;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

}
package com.yiki.pojo;

public class User {
	private String username;
	private Integer psw;
	private Integer age;

	private Address address;//级联属性

	@Override
	public String toString() {
		return "User [username=" + username + ", psw=" + psw + ", age=" + age + ", address=" + address + "]";
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Integer getPsw() {
		return psw;
	}

	public void setPsw(Integer psw) {
		this.psw = psw;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

}
package com.yiki.Handle;

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

import com.yiki.pojo.User;

@RequestMapping("/testPojo")
@Controller
public class TESTPojo {

	private static final String SUCCESS = "success";

	@RequestMapping("/pojoPost")
	public String testPojoPost(User user) {
		System.out.println("testpojo"+user);
		return SUCCESS;
	}

	@RequestMapping("/pojoGet")
	public String testPojoGet(User user) {
		System.out.println("testpojoGet"+user.getUsername());
		return SUCCESS;
	}

}
姓名srting:
密码int:
年龄int:
省份string:
城市string:

pojoGet

ServletAPI

[SpringMVC]夹带请求参数该如何操作@RequestParam/POJO对象绑定请求参数/原生ServletAPI_第2张图片

package com.yiki.Handle;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

@RequestMapping("/springServlet")
@Controller
public class ServletAPI {
	private static final String SUCCESS = "success";

	@RequestMapping("/servletAPI")
	public String servletAPI(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("servletAPI" + req + resp);
		return SUCCESS;

	}

	@RequestMapping("/servletWrite")
	public void servletWrite(HttpServletRequest req, HttpServletResponse resp, Writer out) throws IOException {
		System.out.println("servletWrite" + req + resp);
		out.write("HelloWorld");

	}
}
servletAPI
servletWrite










你可能感兴趣的:(SpringMVC)