SpringMVC的标签库

        Spring2.0版本开始后,提供了一组功能强大的标签用来在JSP和SpringWebMVC中处理表单元素 ,可以用来访问控制器处理命令对象和绑定数据;
        以下是表单标签库中的所有标签;
form
input
password
hidden
textarea
checkbox
checkboxes
radiobutton
radiobuttons
select
option
options
errors
        其实这些标签库就是SpringMVC将我们的日常表单标签给封装了起来而已。所以只要记住他们的用法即可;对了,在使用这些封装后的标签的时候,你要先在jsp页面的第一行声明一下taglib指令:
<%@ 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";
}
}

  

这里我解释一下,<form:input path = "age"/>这个就是我说的封装之后的形态了,在jsp中会自动被渲染成<input id = "username" name = "username“ type = "text" value = "jack"/>能被封装成这样,还是挺不错的;
        那如果我不想使用默认的ModelAttribute容器那怎么办呢?你可以自己去定义一个容器的;其实也就是取个名字而已;
<%@ 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"/>

  

       接下来的将是骚操作的开始;注意了:
        checkbox标签:这个标签要注意的点在于两个地方,你的全部选项和选中选项;
        这是第一种方式:将你的全部选项在JSP页面中全部列出来,选中选项进行封装,是为下策;
@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>

  

        接下来是checkboxes标签的使用,这里呢,有三种方法,一种比一种好;
            1,用List容器,将全部选项和选中选项都封装起来;这样的话,跟上一个的区别在于,在JSP页面中,用item = "${courseList}"来替代掉一个个列出来;
@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>

  

        用一个items = ${...}即可了;
        2,前面我们说了,将两个都封装了起来,但是我们这样的话,就相当于你要写两次了,要是你的选项很长那可怎么办?下面是骚操作的开始;
        我们用另一种容器来封装试试,那就Map<String,String>容器,前一个Spring用数字来表示,后一个String才是真正的全部选项,这样的话,我们的选中选项可以直接用数字来表示了;上代码;
@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>

  

        介绍完这个之后,接下来的标签就是差不多的了。
        radiobutton标签;
        1,全部选项列出来;
@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>

  

        radiobuttons标签;
        可以有两种方法,List容器和Map容器;
@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>

  

        接下来是select标签,option标签,options标签;
        有三种方法,全选选项列出来,Map容器,List对象封装
@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>

  

其中,itemLabel表示的是Map中的key,itemValue表示的是Map中的value;
        errors标签;这个标签相当于将我们平时对项目执行中出现的各种用户的误操作进行了囊括和封装,将错误的类型列出来并做了相应的处理。首先,我们需要一个UserValidator;
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>

  

在registerForm.jsp页面中,在每个需要输入的空间后面增加一个errors标签,用来显示错误信息;
上一篇:超频真的不难!G3258超频4.5GHz全攻略


下一篇:3n 块披萨