1.前端向后端传值
@Controller
public class TestController {
@RequestMapping("test")
//如果想要获得前台的参数,可直接定义,名字必须一样
public String test(String username,String password) {
System.out.println("运行了test方法,username="+username+",password="+password);
//return返回的值是要跳转的页面
return "/page/index.jsp";
}
@RequestMapping("test2")
//如果名字不一样,可以使用@RequestParam注解,这个注解还可以配置默认值@RequestParam(defaultValue="")
public String test2(@RequestParam(value="username")String name,@RequestParam(value="password")String pwd) {
System.out.println("运行了test方法,username="+name+",password="+pwd);
//return返回的值是要跳转的页面
return "/page/index.jsp";
}
@RequestMapping("test3")
//如果写对象,属性名要对应,springmvc会自动将数据封装为一个对象,也可以加上request和response,session
public String test3(User user,HttpServletRequest req,HttpSession session) {
System.out.println(user);
return "/page/index.jsp";
}
@RequestMapping("test4")
//当参数为集合类型,前端为main.jsp中的复选框
public String test4(@RequestParam("hover")List<String> list) {
System.out.println(list);
return "/page/index.jsp";
}
@RequestMapping("test5")
//当参数为user.username,user.password,可以自动封装为people类型
public String test5(People people) {
System.out.println(people);
return "/page/index.jsp";
}
@RequestMapping("test6/{username}/{password}")
//restful风格的参数,用起来会使请求格式统一,@PathVariable是必须加的
public String test6(@PathVariable("username")String name,@PathVariable String password ) {
System.out.println("restful风格的参数为,username:"+name+",password:"+password);
return "/page/index.jsp";
}
@RequestMapping("test7")
//如果想直接返回一个字符串或json对象,可以这样
public void test7(HttpServletResponse res) {
User user=new User("aaa", "123");
try {
res.getWriter().print(user);
} catch (IOException e) {
e.printStackTrace();
}
}
//实现test7的另一种方法,@ResponseBody注解将对象转化为json字符串,并将响应头改为application/json
//如果不加@ResponseBody,还是会实现跳转功能,不要忘记添加jackson依赖
@RequestMapping("test8")
@ResponseBody
public User test8() {
User user=new User("张三", "123");
return user;
}
//如果返回值不是key-value形式(对象,map),将响应头改为text/html
//所以如果是中文需要设置编码
@RequestMapping(value="test9",produces="text/html;charset=utf-8")
@ResponseBody
public String test9() {
return "中文";
}
}
2.后端向前端传值
@Controller
public class Test2Controller {
// 第一种:使用原生的servlet
@RequestMapping("test2-1")
public String test1(HttpServletRequest req) {
HttpSession session = req.getSession();
// 向session中放入值
session.setAttribute("", "");
ServletContext application = req.getServletContext();
// 向application中放入值
application.setAttribute("", "");
return "/page/index.jsp";
}
// 第二种:使用map
@RequestMapping("test2-2")
public String test2(Map<String, Object> map) {
// 向map中放入值后,可以在跳转后的jsp页面中直接使用map
// 其实是将map中的键值对放入到了request中
map.put("map", "这是map的值");
return "/page/index.jsp";
}
// 第三种:使用model,与map的方法类似
@RequestMapping("test2-3")
public String test3(Model model) {
model.addAttribute("model", "这是model的值");
return "/page/index.jsp";
}
//第四种:使用ModelAndView,将视图和数据放到一个ModelAndView对象中
@RequestMapping("test2-4")
public ModelAndView test4(ModelAndView mv) {
mv.setViewName("/page/index.jsp");
mv.addObject("modelandview", "这是modelandview的值");
return mv;
}
}