1、导入必要jar包 (同上一篇)
2、编写springmvc-second.xml
<!-- 包扫描-->
<context:component-scan base-package="com.codeyancy.cn.controller"/>
<!-- 代替配置处理器映射器和处理器适配器-->
<mvc:annotation-driven/>
<!-- 配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀-->
<property name="prefix" value="/WEB-INF/pages/"/>
<!-- 后缀-->
<property name="suffix" value=".jsp"/>
</bean>
3、编写web.xml
<servlet>
<servlet-name>dispacherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- contextConfigLocation固定写法 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-second.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispacherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
接收前端传来基本类型和字符串
4、在index.jsp中编写前端代码
<h2>普通参数类型接收</h2>
<form action="/receiveBasicParam">
用户名:<input type="text" name="username"> <br>
密码: <input type="password" name="password"> <br>
年龄: <input type="text" name="age"> <br>
<input type="submit" value="提交">
</form>
5、编写ParamReceiveController
@Controller
public class ParamReceiveController {
/**
* 接收基本类型和字符串 key=value形式
* @param name
* @param password
* @param age
* @return
*/
@GetMapping("receiveBasicParam")
public ModelAndView basicParam(@RequestParam("username") String name, String password,int age){
System.out.println("用户名:"+name);
System.out.println("密码:"+password);
System.out.println("年龄:"+age);
ModelAndView mv = new ModelAndView();
mv.setViewName("success");
return mv;
}
}
注意事项:
Controller类中的参数名需要和前端input中的name值保持一致。如果不一致需要使用@RequestParam("username") String name
测试
输入后点击提交页面成功跳转,后台拿到数据。
接收数组
4、在index.jsp中编写前端代码
<h2>数组参数类型接收</h2>
<form action="/receiveArrayParam">
羽毛球:<input type="checkbox" name="hobby" value="羽毛球"> <br>
乒乓球:<input type="checkbox" name="hobby" value="乒乓球"> <br>
篮球:<input type="checkbox" name="hobby" value="篮球"> <br>
足球:<input type="checkbox" name="hobby" value="足球"> <br>
<input type="submit" value="提交">
</form>
5、编写ParamReceiveController
@GetMapping("receiveArrayParam")
public ModelAndView arrayParam(String[] hobby){
System.out.println(Arrays.toString(hobby));
ModelAndView mv = new ModelAndView();
mv.setViewName("success");
return mv;
}
接收自定义类型
在接收自定义类型时,比如用户注册,需要提交很多信息,这时候可以封装成一个实体类来保存这些信息。
4、在index.jsp中编写前端代码
<h2>接收自定义类型</h2>
<form action="receiveCustomParam">
用户名:<input type="text" name="username"> <br>
密码: <input type="password" name="password"> <br>
年龄: <input type="text" name="age"> <br>
地址: <input type="text" name="address"> <br>
<input type="submit" value="注册">
</form>
编写实体类来保存这些信息 User
public class User {
private String username;
private String password;
private String age;
private String address;
getter setter toString
5、编写ParamReceiveController
- 将User当作参数传入
@GetMapping("receiveCustomParam")
public ModelAndView customParam(User user){
System.out.println(user);
ModelAndView mv = new ModelAndView();
mv.setViewName("success");
return mv;
}
结果:
接收包装类型
在自定义类型的基础上编写包装类
编写UserExt
public class UserExt {
private User user;
private String desc;
getter setter toString
4、在index.jsp中编写前端代码
<h2>接收包装类</h2>
<form action="receivePackingParam">
用户名:<input type="text" name="user.username"> <br>
密码: <input type="password" name="user.password"> <br>
年龄: <input type="text" name="user.age"> <br>
地址: <input type="text" name="user.address"> <br>
描述: <input type="text" name="desc"> <br>
<input type="submit" value="注册">
</form>
5、编写ParamReceiveController
/**
* 包装类接收
*/
@GetMapping("receivePackingParam")
public ModelAndView packingParam(UserExt userExt){
System.out.println(userExt);
ModelAndView mv = new ModelAndView();
mv.setViewName("success");
return mv;
}
结果:
接收list集合
在包装类里添加user集合
UserExt
public class UserExt {
private User user;
private String desc;
private List<User> list;
getter setter toString
4、在index.jsp中编写前端代码
<h2>接收list集合</h2>
<form action="receiveListParam">
用户名:<input type="text" name="list[0].username"> <br>
密码: <input type="password" name="list[0].password"> <br>
年龄: <input type="text" name="list[0].age"> <br>
地址: <input type="text" name="list[0].address"> <br>
用户名:<input type="text" name="list[1].username"> <br>
密码: <input type="password" name="list[1].password"> <br>
年龄: <input type="text" name="list[1].age"> <br>
地址: <input type="text" name="list[1].address"> <br>
用户名:<input type="text" name="list[2].username"> <br>
密码: <input type="password" name="list[2].password"> <br>
年龄: <input type="text" name="list[2].age"> <br>
地址: <input type="text" name="list[2].address"> <br>
<input type="submit" value="注册">
</form>
5、编写ParamReceiveController
/**
* 集合的接收
*/
@GetMapping("receiveListParam")
public ModelAndView listParam(UserExt userExt){
System.out.println(userExt.getList());
ModelAndView mv = new ModelAndView();
mv.setViewName("success");
return mv;
}
结果:
接收map集合
在包装类中添加成员变量
private Map<String,String> map;
4、在index.jsp中编写前端代码
<h2>接收map集合</h2>
<form action="receiveMapParam">
<input type="text" name="map['name']"> <br>
<input type="text" name="map['desc']"> <br>
<input type="submit" value="注册">
</form>
5、编写ParamReceiveController
/**
* map集合的接收
*/
@GetMapping("receiveMapParam")
public ModelAndView mapParam(UserExt userExt){
System.out.println(userExt.getMap());
ModelAndView mv = new ModelAndView();
mv.setViewName("success");
return mv;
}
结果:
接收Json字符串
这里使用jackson,需要导入必要的jar包
使用浏览器前端页面无法发送json字符串,这里我们使用工具 Postman
直接把json字符串的值封装到User对象上进行解析
1、编写JsonReceiveController
@Controller
public class JsonReceiveController {
/**
* 接收Json格式字符串的参数
*
* 1、post请求和put请求 可以传递json格式字符串
* 2、参数的位置在请求体 @RequestBody
*/
@PostMapping("receiveParamJson")
public ModelAndView receiveJson(@RequestBody User user){
System.out.println(user);
ModelAndView mv = new ModelAndView();
mv.setViewName("success");
return mv;
}
}
2、使用工具 Postman发送json字符串
结果:
后台成功拿到并解析json字符串。
如果postman中发送的json字符串和user中的属性不一致,那么数据封装的时候只会封装user有的,而没有封装上的也不会报错,后台只会显示为null.
如果我们只想取出其中的一个值怎么做?比如username
1、修改JsonReceiveController
@PostMapping("receiveParamJson")
public ModelAndView receiveJson(@RequestBody Map<String,String> map){
System.out.println(map.get("username"));
ModelAndView mv = new ModelAndView();
mv.setViewName("success");
return mv;
}
2、使用工具 Postman发送json字符串
后台服务器:
参数的乱码问题
json格式字符串参数是不会乱码的
post乱码解决配置web.xml过滤器
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>