public void registBean(String username,int age,double income,String[] interests,boolean marrid)
保证JSP页面上提交项和方法中对应参数同名。其中boolean的提交项的value是字符串true和false。
public void registBean(UserBean2 user)
public class UserBean2{
private List<AddressListBean> list= new ArrayList<AddressListBean>();
}
public void registBean(UserBean2 user)
JSP页面上提交项的命名:参数名[下标].属性名。
public class UserBean2{
private Set<AddressSetBean> set= new HashSet<AddressSetBean>();
public Classes(){
set.add(new AddressSetBean());
set.add(new AddressSetBean());
set.add(newAddressSetBean());
}
}
public class UserBean2{
private Map<String String> map= new HashMap<String String>();
}
public void registBean(UserBean2 user)
public class UserBean2{
private String name;
private int age;
private AddressBean address;
}
public void registBean(UserBean2 user)
JSP页面提交项命名:复合属性名.属性名
主要代码:
1 package com.cy.springannotation.entity; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.HashSet; 6 import java.util.List; 7 import java.util.Map; 8 import java.util.Set; 9 10 11 public class UserBean2 {12 private String username;13 private int age;14 private double income;15 private String[] interests;16 private boolean married;17 //List不能单独使用,必须要绑定到某一个具体的Bean,并且要实例化ArrayList18 private Listlist=new ArrayList ();19 //Set不能单独使用,必须要绑定到某一个具体的Bean,并且要实例化HashSet20 private Set set=new HashSet ();21 //Map不能单独使用,必须要绑定到某一个具体的Bean,并且要实例化HashMap22 private Map map=new HashMap ();23 //复合数据类型24 private AddressBean address;25 26 public UserBean2(){27 //set集合需要提前定义个数28 for(int i=0;i<2;i++){29 AddressSetBean asb=new AddressSetBean();30 set.add(asb);31 }32 33 }34 public String getUsername() {35 return username;36 }37 public void setUsername(String username) {38 this.username = username;39 }40 public int getAge() {41 return age;42 }43 public void setAge(int age) {44 this.age = age;45 }46 public double getIncome() {47 return income;48 }49 public void setIncome(double income) {50 this.income = income;51 }52 public String[] getInterests() {53 return interests;54 }55 public void setInterests(String[] interests) {56 this.interests = interests;57 }58 public boolean isMarried() {59 return married;60 }61 public void setMarried(boolean married) {62 this.married = married;63 }64 65 public Map getMap() {66 return map;67 }68 public void setMap(Map map) {69 this.map = map;70 }71 72 public Set getSet() {73 return set;74 }75 public void setSet(Set set) {76 this.set = set;77 }78 public List getList() {79 return list;80 }81 public void setList(List list) {82 this.list = list;83 }84 public AddressBean getAddress() {85 return address;86 }87 public void setAddress(AddressBean address) {88 this.address = address;89 }90 91 }
页面设置“
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 8 9 1089 9011 12 注册页面 13 14 15 16 17 18 19 22 23 24 25 26
业务控制器:
1 package com.cy.springannotation.controller; 2 3 import org.apache.log4j.Logger; 4 import org.springframework.stereotype.Controller; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 7 import com.cy.springannotation.entity.AddressListBean; 8 import com.cy.springannotation.entity.AddressSetBean; 9 import com.cy.springannotation.entity.UserBean2;10 11 @Controller12 public class RegistController {13 private Logger log = Logger.getLogger(this.getClass());14 15 16 /* @RequestMapping("/regist.do")17 public String regist(String username,int age,double income,String[] interests,boolean married){18 log.info("username="+username);19 log.info("age="+age);20 log.info("income="+income);21 log.info("interests="+interests.length);22 log.info("married="+married);23 for (String string : interests) {24 log.info("兴趣有:"+string);25 }26 return "index";27 28 }29 */ 30 //简单对象类型31 @RequestMapping("/regist.do")32 public String registBean(UserBean2 user){33 log.info("username:"+user.getUsername());34 log.info("age:"+user.getAge());35 log.info("income:"+user.getIncome());36 log.info("interests.length:"+user.getInterests().length);37 log.info("married:"+user.isMarried());38 log.info("addressList.size():"+user.getList().size());39 log.info("addressSet.size():"+user.getSet().size());40 log.info("addressMap.size():"+user.getMap().size());41 log.info("address:"+user.getAddress().getAddressName());42 43 for (String string : user.getInterests()) {44 log.info("具体的兴趣有=========" + string);45 }46 47 for(AddressListBean add:user.getList()){48 log.info("具体的list地址有=========" + add.getAddressListName());49 }50 51 for(AddressSetBean asetb:user.getSet()){52 log.info("具体的set地址有=========" + asetb.getAddressSetName());53 }54 55 for(int i=0;i<2;i++){56 log.info("具体的map地址有=========" + user.getMap().get("a"+i).getAddressMapName());57 }58 59 return "index";60 }61 62 63 }
控制台显示:
数据绑定over。