spring提供了两个标签库文件:spring-form.tld(表单标签库,用于输出HTML表单) spring.tld(基础标签库,用于Spring数据绑定等)
使用步骤:
1,配置表单标签库,复制spring.tld到WEB-INF目录下,并在web.xml中添加该标签库的引用:
<jsp-config> <taglib> <taglib-uri>/spring-form</taglib-uri> <taglib-location>/WEB-INF/spring-form.tld</taglib-locatioin> </tablib> </jsp-config>
2,在JSP文件中引入标签:
<%@taglib prefix="form" uri="/spring-form"%>
3,在request中保存JavaBean.
1 public class User { 2 3 private String username="admin"; 4 private String password="1243"; 5 private String [] interests= {"Structs","Hibernate","Spring"}; 6 private String sex="M"; 7 public String getUsername() { 8 return username; 9 } 10 public void setUsername(String username) { 11 this.username = username; 12 } 13 public String getPassword() { 14 return password; 15 } 16 public void setPassword(String password) { 17 this.password = password; 18 } 19 public String[] getInterests() { 20 return interests; 21 } 22 public void setInterests(String[] interests) { 23 this.interests = interests; 24 } 25 public void setSex(String sex) { 26 this.sex = sex; 27 } 28 29 }
然后在JSP页面中创建一个User对象,并保存在request对象中,命令为user;
<% User user= new User();
request.setAttribute("user",user);
%>
4,绑定JavaBean对象。
<form:form commandName="user"></form>
5,绑定JavaBean属性