方法一
@Controller
public class HelloController {
@RequestMapping({"/hello","/admin"})
public String hello(@RequestParam("username") String username,Map<String,Object> context){
context.put("username", username);
System.out.println(username);
return "hello";
}
方法二
@Controller
public class HelloController {
@RequestMapping({"/hello","/admin"})
public String hello(@RequestParam("username") String username,Model model){
model.addAttribute("username",username);
System.out.println(username);
return "hello";
}
addAttribute(String,Object)方法,就是完成String到Object的映射,然后将String传给View层,View层调用的话,用String(映射名)+属性即可。
如果String 映射的 Object 是一个Map的话,要用String.value+属性
例如
<c:forEach items="${users }" var="um">
${um.value.name }</br>
${um.value.password }</br>
${um.value.email }</br>
</c:forEach>