Spring mvc 值的传递

来源:互联网 发布:淘宝网消费者号码 编辑:程序博客网 时间:2024/05/21 16:22

1.view--->controller

1)public String welcome(@RequestParam("username") String username) 必须传递username 不然会报错404
2)直接在函数参数列表中添加参数接收 public String welcome(String username) 不强制必须传递username


2.controller--->view
1)用一个map 例如:Map<String, Object>context  ,然后向context中put进去要传的参数,context.put("username", username)

package com.join.spring.controller;import java.util.Map;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;@Controllerpublic class HelloController {@RequestMapping("/hello")public String welcome(@RequestParam("username") String username,Map<String, Object> context) {context.put("username", username);System.out.println(username);return "welcome";}}


2)用Model来实现
  
package com.join.spring.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;@Controllerpublic class HelloController {@RequestMapping("/hello")public String welcome(@RequestParam("username") String username,Model model) {model.addAttribute("username", username);System.out.println(username);return "welcome";}}
</pre><pre name="code" class="java">


0 0
原创粉丝点击