SpringMVC的标签库
form |
input |
password |
hidden |
textarea |
checkbox |
checkboxes |
radiobutton |
radiobuttons |
select |
option |
options |
errors |
<%@ taglib prefix = "form" uri = "http://www.springframework.org/tags/form" %>
form和input标签的使用;上代码;
<%@ taglib prefix = "form" uri = "http://www.springframework.org/tags/form" %> <form:form method = "post" action = "register"> <table> <tr> <td>姓名:</td> <td><form:input path = "username"></td> </tr> <tr> <td>性别:</td> <td><form:input path = "sex"/></td> </tr> <tr> <td>年龄:</td> <td><form:input path = "age"/></td> </tr> </table> </form:form>
而在控制器这边,则只需要将user里面的属性设置好了之后放到modelAttribute模型容器中就可以了,其中要记得放入默认的容器command中
@Controller public class UserController{ @RequestMapping(value = "/registerForm",method = RequestMethod.POST) public String registerForm(Model model){ User user = new User("jack","男",28); model.addAttribute("command",user); return "registerForm"; } }
<%@ taglib prefix = "form" uri = "http://www.springframework.org/tags/form" %> <form:form modelAttribute = "user" method = "post" action = "register"> <table> <tr> <td>姓名:</td> <td><form:input path = "username"></td> </tr> <tr> <td>性别:</td> <td><form:input path = "sex"/></td> </tr> <tr> <td>年龄:</td> <td><form:input path = "age"/></td> </tr> </table> </form:form>
在form表单标签中加一个modelAttribute = "user";就可以了;接下来是控制器的改变;
@RequestMapping(value = "/registerForm2",method = RequestMethod.GET) public String registerForm2(Model model){ User user = new User("jack","男",28); model.addAttribute("user",user); return "registerForm2"; }
接下来的几个标签都是正常操作,只是封装了path属性而已;
<form:password path = "password"/> <form:hidden path = "id"/> <form:textarea path = "remark" rows = "5" cols = "20"/>
@Controller public class UserController{ @RequestMapping(value = "/checkboxForm",method = RequestMethod.GET) public String registerForm(Model model){ User user = new User(); user.setReader(true); List<String> list = new ArrayList<String>(); list.add("JAVAEE"); list.add("Spring"); user.setCourses(list); model.addAttribute("user",user); return "checkboxForm"; } }
在这个控制器中,通过List容器来封装选中选项,而全部选项则是直接列出来JSP页面中
<%@ taglib prefix = "form" uri = "http://www.springframework.org/tags/form" %> <form:form modelAttribute = "user" method = "post" action = "checkboxForm"> <table> <tr> <td>选择课程</td> <td> <form:checkbox path = "courses" value = "JAVAEE" label = "JAVAEE"/> <form:checkbox path = "courses" value = "Mybatis" label = "MyBatis"/> <form:checkbox path = "courses" value = "Spring" label = "Spring"/> </td> </tr> </table> </form:form>
@RequestMapping(value = "/checkboxesForm",method = RequestMethod.GET) public String registerForm(Model model){ User user = new User(); List<String> list = new ArrayList<String>(); list.add("JAVAEE"); list.add("Spring"); user.setCourses(list); List<String> courseList = new ArrayList<String>(); courseList.add("JAVAEE"); courseList.add("MyBatis"); courseList.add("Spring"); model.addAttribute("user",user); model.addAttribute("courseList",courseList); return "checkboxesForm"; }
看到没有,将全部选项和选中选项全部进行封装,都放在modelAttribute容器中;
<%@ taglib prefix = "form" uri = "http://www.springframework.org/tags/form" %> <form:form modelAttribute = "user" method = "post" action = "checkboxesForm"> <table> <tr> <td>选择课程:</td> <td> <form:checkboxes items = "${courseList}" path = "courses"/> </td> </tr> </table> </form:form>
@RequesetMapping(value = "/checkboxesForm2",method = RequestMethod.GET) public String registerForm2(Model model){ User user = new User(); List<String> list = new ArrayList<String>(); list.add("1"); list.add("3"); user.setCourses(list); Map<String,String> courseMap = new HashMap<String, String>(); courseMap.put("1","JAVAEE"); courseMap.put("2","MyBatis"); courseMap.put("3","Spring"); model.addAttribute("user",user); model.addAttribute("courseMap",courseMap); return "checkboxesForm2"; }
用Map容器封装,而JSP页面没有变化的;
<form:form modelAttribute = "user" method = "post" action = "checkboxesForm2"> <table> <tr> <td>选择课程:</td> <td> <form:checkboxes items = "${courseMap}" path = "courses"/> </td> </tr> </table> </form:form>
好了,理论上这已经是最好的了,但是我们知道,这些选项到真正的企业开发中都是用到了数据库的,而谈到数据库,我们当然就想到了ORM数据库框架,而这些框架都是用对象来映射对应的数据库数据的,那我们能不能将这些选项(不管是全部还是选中)先封装在一个对象中,然后再把这一个个的对象封装到list模型里面呢?这才是真正的骚操作啊。。。。
@RequestMapping(value = "/checkboxesForms",method = requestMethod.GET) public String registerForm3(Model model){ Employee employee = new Employee(); Dept dept = new Dept(1,"开发部"); List<dept> list = new ArrayList<Dept>(); list.add(dept); employee.setDepts(list); List<Dept> deptList = new ArrayList<Dept>(); deptList.add(dept); deptList.add(new Dept(2,"销售部")); deptList.add(new Dept(3,"财务部")); model.addAttribute("employee",employee); model.addAttribute("deptList",deptList); return "checkboxesForm3"; }
看到了吗,将选项封装成对象,再将对象封装在list容器中,而在JSP页面要注意一点,你要声明对象里面有什么?不然的话就是一个对象的地址而已
<form:form modelAttribute = "employee" method = "post" action = "checkboxesForm3"> <table> <tr> <td>选择部门:</td> <td> <form:checkboxes items = "${deptList}" path = "depts" itemLabel = "name" itemValue = "id"/> </td> </tr> </table> </form:form>
@Controller public class UserController{ @RequestMapping(value = "/radiobuttonForm",method = RequestMethod.GET) public String registerForm(Model model){ User user = new User(); user.setSex("男"); model.addAttribute("user",user); return "radiobuttonForm"; } }
Controller内容如下;
<%@ taglib prefix = "form" uri = "http://www.springframework.org/tags/form" %> <form:form modelAttribute = "user" method = "post" action = "radiobuttonForm"> <table> <tr> <td>性别:</td> <td> <form:radiobutton path = "sex" value = "男"/>男 <form:radiobutton path = "sex" value = "女"/>女 </td> </tr> </table> </form:form>
@RequestMapping(value = "/radiobuttonsForm",method = RequestMethod.GET) public String registerForm2(Model model){ User user = new User(); user.setSex("男"); List<String> sexList = new ArrayList<String>(); sexList.add("男"); sexList.add("女"); model.addAttribute("user",user); model.addAttribute("sexList",sexList); return "radiobuttonsForm"; }
<%@ taglib prefix = "form" uri = "http://www.springframework.org/tags/form" %> <form:form modelAttribute = "user" method = "post" action = "radiobuttonForm"> <table> <tr> <td>性别:</td> <td> <form:radiobuttons path = "sex" items = "${sexList}"/> </td> </tr> </table> </form:form>
下面是Map容器方法;
@RequestMapping(value = "/radiobuttonForm2",method = RequestMethod.GET) public class registerForm3(Model model){ User user = new User(); user.setSex("1"); Map<String,String> sexMap = new HashMap<String,String>(); sexMap.put("1","男"); sexMap.put("2","女"); model.addAttribute("user",user); model.addAttribute("sexMap",sexMap); return "radiobuttonsForm2"; }
<%@ taglib prefix = "form" uri = "http://www.springframework.org/tags/form" %> <form:form modelAttribute = "user" method = "post" action = "radiobuttonForm"> <table> <tr> <td>性别:</td> <td> <form:radiobuttons path = "sex" items = "${sexMap}"/> </td> </tr> </table> </form:form>
@RequestMapping(value = "/selectForm",method = RequestMethod.GET) public String selectForm(Model model){ User user = new User(); user.setDeptId(2); model.addAttribute("user",user); return "selectForm"; }
<form:form modelAttribute = "user" method = "post" action = "selectForm"> <table> <tr> <td>部门:</td> <td> <form:select path = "deptId"> <form:option value = "1">财务部</form:option> <form:option value = "2">开发部</form:option> <form:option value = "3">销售部</form:option> </form:select> </td> </tr> </table> </form:form>
@RequestMapping(value = "/selectForm2",method = RequestMethod.GET) public String selectForm2(Model model){ User user = new User(); user.setDeptid(2); Map<Integer,String> deptMap = new HashMap<Integer,String>(); deptMap.put(1,"财务部"); deptMap.put(2,"开发部"); deptMap.put(3,"销售部"); model.addAttribute("user",user); model.addAttribute("deptMap",deptMap); return "selectForm2"; }
<form:form modelAttribute = "user" method = "post" action = "selectForm2"> <table> <tr> <td>部门:</td> <td> <form:select path = "deptId" items = "${deptMap}"/> </td> </tr> </table> </form:form>
@RequestMapping(value = "/selectForm4",method = RequestMethod.GET) public String selectForm4(Model model){ User user = new User(); user.setDeptId(2); List<Dept> deptList = new ArrayList<Dept>(); deptList.add(new Dept(1,"财务部")); deptList.add(new Dept(2,"开发部")); deptList.add(new Dept(3,"销售部")); model.addAttribute("user",user); model.addAttribute("deptList",deptList); return "selectForm4"; }
<form:form modelAttribute = "user" method = "post" action = "selectForm2"> <table> <tr> <td>部门:</td> <td> <form:select path = "deptId" items = "${deptList}" itemLabel = "name" itemValue = "id"/> </td> </tr> </table> </form:form>
public class UserValidator implements Validator{ @Override public boolean supports(Class<?> clazz){ return User.class.equals(clazz); } @Override public void validator(Object object,Errors errors){ ValidatorUtils.rejectIfEmpty(errors,"username",null,"用户名不能为空"); ValidatorUtils.rejectIfEmpty(errors,"sex",null,"性别不能为空"); ValidatorUtils.rejectIfEmpty(errors,"age",null,"年龄不能为空"); } }
UserValidator类实现org.springframework.validator.Validator接口,完成验证的功能;
@Controller public class UserController{ @RequestMapping(value = "/registerForm",method = RequestMethod.GET) public String registerForm(Model model){ User user = new User(); model.addAttribute("user",user); return "registerForm"; } @InitBinder public void initBinder(DataBinder binder){ binder.setValidator(new UserValidator()); } @RequestMapping(value = "/register",method = RequestMethod.POST) public String register(@Validated User user,Errors errors){ if(errors.hasFieldErrors()) return "registerForm"; return "submit"; } }
UserController类使用@InitBinder注解绑定验证对象;
<form:form modelAttribute = "user" method = "post" action = "register"> <table> <tr> <td>姓名</td> <td><form:input path = "username"/></td> <td><font color = "red"><form:errors path = "username"></font></td> </tr> <tr> <td>性别</td> <td><form:input path = "sex"/></td> <td><font color = "red"><form:errors path = "sex"></font></td> </tr> <tr> <td>年龄</td> <td><form:input path = "age"/></td> <td><font color = "red"><form:errors path = "age"></font></td> </tr> </table> </form:form>