SpringMVC---控制器向视图传参数(对象,集合,数组)

@RequestParam(value = “username”,required = false),flase可以没有参数
/hello,是别人访问本方法的url地址
text,是视图名

Thymeleaf教程
(用于html和控制器传递参数)

Controller.java(控制器页面)
记得要写注解

@Controller
public class IndexController_zhujie {
	@RequestMapping("/hello")
    public String hello(Model model){
        //向页面传递对象
        User user=new User();
        user.setUserName("李明");
        user.setUserPassword("123456");
        model.addAttribute(user);

        //向页面传递集合
        user.setUserName("李明");
        user.setUserPassword("123456");
        User user2=new User();
        user2.setUserName("李明2");
        user2.setUserPassword("654321");
        List userList=new ArrayList<>();
        userList.add(user);
        userList.add(user2);
        model.addAttribute("userList",userList);

        //向页面传递数组
        String[]string={"李明","二公子","二少爷"};
        model.addAttribute("array",string);

        return "text";
    }
}

html(视图页面)
顶端要写上此标签(Thymeleaf)

(还有Thymeleaf视图解析器,在servlet配置文件中配)

text页面

对象

姓名

密码:

集合

  • 名字

数组

  • 名字

你可能感兴趣的:(SpringMVC,Spring框架)