SpringMVC学习(三)请求数据的传入和传出
1、数据传入
前端数据传入到后台
1.1、@RequestParam注解
没用@ResquestParame注解方式
@RequestMapping("/hello")
public String hello(String name){
System.out.println(name);
return "hello";
}
在网页中数据输入方式:http://localhost:8080/hello?name=song 需要与方法中的参数名一致
若用了@ResqusetParmae注解
@RequestMapping("/hello")
public String hello(@RequestParam("username") String name){
System.out.println(name);
return "hello";
}
在网页中数据输入方式:http://localhost:8080/hello?username=song 可以自己定义 可以与方法中的参数名称不一致
@Controller
public class HelloController {
/**
* @RequestParam 获取请求的参数,参数默认必须要带
* value 指定的key
* required 参数是否必须带
* defaultValue 默认参数的值
* @param username
* @return
*/
@RequestMapping("/handle01")
public String helloHandle(
@RequestParam(value = "user",required = false,defaultValue = "no no ")String username
, @RequestHeader("User-Agent") String userAgent){
System.out.println("这个变量值:"+username);
System.out.println("请求头中浏览器的信息:"+userAgent);
return "success";
}
}
- 在处理方法入参处使用 @RequestParam 可以把请求参数传递给请求方法
- value:参数名
- required:是否必须。默认为 true, 表示请求参数中必须包含对应的参数,若不存在,将抛出异常
- defaultValue: 默认值,当没有传递参数时使用该值
使用pojo作为参数
@RequestMapping("/user")
public String user(User user){
System.out.println(user);
return "hello";
}
在网页中数据输入方式:http://localhost:8080/user? name=song&age=19
2、数据输出
数据显示到前端
2.1、SpringMVC 输出模型数据的描述
- ModelAndView: 处理方法返回值类型为 ModelAndView 时, 方法体即可通过该对象添加模型数据
- Map 及 Model: 入参为 org.springframework.ui.Model、org.springframework.ui.ModelMap 或 java.uti.Map 时,处理方法返回时,Map 中的数据会自动添加到模型中。
- @SessionAttributes: 将模型中的某个属性暂存到 HttpSession 中,以便多个请求之间可以共享这个属性
- @ModelAttribute: 方法入参标注该注解后, 入参的对象就会放到数据模型中
ModelAndView使用
@RequestMapping("/hello")
public ModelAndView testModelAndView(){
System.out.println("testModelAndView");
String viewName = "success";
ModelAndView mv = new ModelAndView(viewName );
mv.addObject("time",new Date().toString()); //实质上存放到request域中
return mv;
}
Model和Model的使用
@RequestMapping("/hello")
public String hello(@RequestParam("username") String name, Model model,ModelMap modelmap){
//封装要显示到视图中的数据
//相当于req.setAttribute("name",name);
model.addAttribute("msg",msg);
model.addAttribute("name",name);
System.out.println(msg);
System.out.println(name);
return "test";
}
3、解决中文乱码问题
方式一
自己编写 过滤器Filter
public class CharacterEndoingFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//解决乱码问题
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
servletResponse.setContentType("text/html;charset=utf-8");
filterChain.doFilter(servletRequest,servletResponse);
}
public void destroy() {
}
}
在web.xml中注册
<filter>
<filter-name>ShowFilter</filter-name>
<filter-class>com.song.filter.CharacterEndoingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ShowFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
方法二
用springmvc中提供的过滤器
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
方式三
可以自己在网上查找解决乱码大部分问题的方法
可自己查看SpringMVC官方文档
推荐学习SpringMVC视频 B站 狂神说java 或者尚硅谷
谢谢大家的阅读! 若上面有写错的 欢迎纠正哦