SpringMVC学习笔记二:请求与响应

2022/7/11 6:20:16

本文主要是介绍SpringMVC学习笔记二:请求与响应,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

请求映射路径

  1. 多人开发,每人设置不同的请求路径,冲突问题如何解决?
    设置模块名作为请求路径前缀
@Controller
//设置整体请求路径前缀
@RequestMapping("/hello")
public class HelloController {
	//请求方式为-->localhost/hello/save
    @RequestMapping("/save")
    @ResponseBody
    public String save(){
        return "{'module':'springmvc'}";
    }
	//请求方式为-->localhost/hello/save
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(){
        return "{'module':'springmvc delete'}";
    }
}

请求方式

get请求

@Controller
public class HelloController {
	//请求方式为-->localhost/hello/save?name=输入值&age=输入值2
	@RequestMapping("/save")
	@ResponseBody
	public String save(String name,int age){
		return "name = "+ name+"\n"+"age = "+ age;
	}
}

测试结果
image

post请求

java程序不变,post使用请求体发送表单
image

post请求中文乱码的情况(在Servlet容器内设置过滤器)

public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }
 
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{{SpringMVCConfig.class}};
    }
 
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
	
	@Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter filter = new CharacterEncodingFilter("UTF-8");
        return new Filter[]{filter};
    }
}

请求参数

1. 绑定请求参数和形参的映射关系

@Controller
public class HelloController {
	//请求方式为-->localhost/hello/save?name=输入值&age=输入值2
	@RequestMapping("/save")
	@ResponseBody
	//将请求参数name映射到形参username上
	//请求方式为-->localhost/hello/save?name=输入值&age=输入值2
	public String save(@RequestParam("name")String username,int age){
		return "name = "+ name+"\n"+"age = "+ age;
	}
}

2. 实体类传参

@Controller
public class HelloController {
	//请求方式为-->localhost/hello/save?name=输入值&age=输入值2
	@RequestMapping("/user")
	@ResponseBody
	//实体类:根据构造函数的形参名自动填充
	public String save(User user){
		System.out.println(user);
        return user.toString();;
	}
}
public class User {


    private String username;
    private int age ;
    private Address address;

    public String getUsername() {
        return username;
    }

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

    public int getAge() {
        return age;
    }

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

    public Address getAddress() {
        return address;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", age=" + age +
                ", address=" + address +
                '}';
    }
}
public class Address {


    private String location;

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    @Override
    public String toString() {
        return "Address{" +
                "location='" + location + '\'' +
                '}';
    }
}

结果
image

3. 列表数组传参

@Controller
public class HelloController {
	@RequestMapping("/array")
	@ResponseBody
	public String user(String[] hobby){
		System.out.println(Arrays.toString(hobby));
		return Arrays.toString(hobby);
	}
}

image



这篇关于SpringMVC学习笔记二:请求与响应的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程