SpringMvc框架实现前端后端数据交互

SpringMvc框架实现前端向后端传数据

(1)直接将表单的name参数写在Controller类的方法参数中(注意要同名才能映射成功)

<form action="/login" method="post">

    name:<input type="text" name="name"/>

    password:<input type="text" name="password"/>

    <input type="submit"/>

</form>

 

//后台controller,addUser方法的userName,password参数名称与表单的name同名才能映射得到数据

@RequestMapping(value = "/login", method = RequestMethod.POST)

public String add(Model model, String name, String password){

    ...

}

 

(2) 表单数据后台通过request来获取数据。同样使用上面的表单

@RequestMapping(value = "/login",method=RequestMethod.POST) 

public String addUser(HttpServletRequest request){ 

String userName = request.getParameter("userName"); 

String password = request.getParameter("password"); 

//... return "userlist";

 } 

 

(3)表单数据封装前端数据到bean中

//定义一个user的类

public class User{ 

private String userName; 

private String password; 

 

//getter 

public void setUserName(String userName){ 

this.userName = userName; 

public void setPassword(String password){ 

this.password = password; 

}

 

 //controller中,就可以通过User user对象封装form表单数据 

@RequestMapping(value = "/login",method=RequestMethod.POST) 

public String addUser(User user){ 

String userName = user.getUserName(); 

String password = user.getPassword(); 

//... return "userlist";

 }

 

 

(4)查询参数方式,通过HTTP发起的一个带有参数的RPC请求,请求的形式为"/aa?name=haha"

@RequestMapping(value = "/aa", method = RequestMethod.POST)

public String func(Model model, @RequestParam("name") String name) {

    ...

}

 

(5)路径变量,直接请求资源,请求的形式为"/aa/haha"

@RequestMapping(value = "/aa/{name}", method = RequestMethod.POST)

public String func(Model model, @PathVariable("name") String name) {

    ...

}

 

 

 

SpringMvc框架实现后端向前端传数据

 

(1)通过request对象

页面名称userAdd

<body> 

<h1>添加用户信息</h1> 

<form action="user/add" method="post"> 

<input type="submit" value="提交"> 

</form>

${personId } 

</body>

 

@RequestMapping("/add") 

public String add(HttpServletRequest request){

request.setAttribute("personId",12);

return "userAdd";

}

 

 

(2)通过ModelAndView对象

@RequestMapping("/add") 

public ModelAndView add(){ 

ModelAndView mav = new ModelAndView("userAdd"); 

mav.addObject("personId", 12); 

return mav; 

 

(3) 通过Model方式

@RequestMapping(value = "/add", method = RequestMethod.POST)

public String add(Model model){

    String personId = "12"

    model.addAttribute("personId",personId);

    return "userAdd";

}

//或

@RequestMapping(value = "/add", method = RequestMethod.POST)

public String add(Model model){

    String personId = "12"

    return personId;

}

 

(4)通过Map对象

@RequestMapping(value = "/add", method = RequestMethod.POST)

public String add(Map map){

    String personId = "12"

    map.put("personId",personId);

    return "userAdd";

}

  • 5
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值