springmvc数据绑定入门篇

约定:本文不介绍springmvc环境搭建,直接讲数据绑定

数定绑定的步骤
  1. 定义实体
  2. 定义Controller,声明ModelAttribute
  3. 编写页面,结合springmvc标签声明如何数据绑定
1.定义实体
public class Account {
	
	private Integer uid;
	
	private String uname;
	
	private String upwd;
	
	// setter getter method

}

这里定义是一个账户类,省略getter和setter方法了

2.定义Controller,声明ModelAttribute
@ModelAttribute("account")
public Account createAccount() {
	return new Account();
}

@RequestMapping(value = "login", method = RequestMethod.GET)
public ModelAndView login(
@ModelAttribute("account") Account account,
BindingResult result, Model model) throws Exception {		
	return new ModelAndView();
}

@RequestMapping用来定义访问地址,在这里,访问的短路径是login,访问方法必须是GET方式

@ModelAttribute用来声明数据绑定,account与页面的ModelAttribute相对应,而且createAccount方法不能少,要不然会报错.

3.引入springmvc标签
<@form.form modelAttribute="account">
<table>
	<tr>
		<td><@form.input path="uname" /></td>		
	</tr>	
	
	<tr>
		<td><@form.password path="upwd" /></td>		
	</tr>
</table>
</@form.form>

@form:form引用的是springmvc的jsp标签,这里用的是freemarker喧染页面,省略引入方式,注意看,modelAttribute="account"与Controller中的注解一样

@form:input中的path就是实体类的属性了,这样写就行

OK,到这里一个简单的数据绑定就完成了.

转载于:https://my.oschina.net/coolfire368/blog/131319

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值