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<User> 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配置文件中配)
<body>
<h1>text页面</h1>

<h2>对象</h2>
<!--对象-->
	<form method="get" action="/hellow">
	    <p>姓名<input type="text" th:value="${user.userName}"></p>
	    <p>密码:<input type="text" th:value="${user.userPassword}"></p>
	    <input type="submit" name="提交">
	</form>
<!--集合-->
	<h2>集合</h2>
	<ul th:each="user:${userList}">
	    <li th:text="${user.userName}">名字</li>
	</ul>
<!--数组-->
	<h2>数组</h2>
	<ul th:each="a:${array}">
	    <li th:text="${a}">名字</li>
	</ul>

</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值