第四篇:Spring-JSP标签
1,Spring绑定标签,合计14个如下
2,Spring通用标签,合计10个,有些已经不再使用,下面重点使用messages标签
3,绑定标签实例
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!-- prefix可以赋值为任意值,本系列,将其命名为form,
后面使用时需要以此为前缀,如<form:form>
spring的jsp标签库合计有14个标签,如下表所示
-->
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<style>
table{ background:#000}
table td{ background:#FFF}
</style>
<body>
<h2>Interface</h2>
<!-- form:form 会渲染成一个HTML的<form>标签
但它包含commandName属性,该属性构建针对某个模型对象的上下文信息 -->
<form:form method="POST" commandName="intf">
<table>
<tr>
<td>名称</td>
<!-- form:input 会被渲染成HTML的<input>标签
其中path属性会将input标签中value的值设置为模型中的path属性对应的值 -->
<td><form:input type="text" path="name"/></td>
</tr>
......
</table>
</form:form>
</body>
</html>
4,表单错误信息提示+参数验证
<form:errors path="*" element="div" cssClass="errors"></form:errors>
配合上节讲的表单提交以及校验
5,国际化<s:message>使用
需要创建一个MessageSource的bean,在WebConfig添加
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages");
messageSource.setCacheSeconds(10);
return messageSource;
}
创建文件 放在classpath下面,
messages_zh_CN.properties
idat.welcome=welcome to IDAT
rmessages_en_US.properties
idat.welcome=\u6B22\u996E\u6765\u5230IDAT
欢迎页
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags" %>
<html>
<body>
<h2><s:message code="idat.welcome" text="Welcome"/></h2>
</body>
</html>
系统会根据浏览器语言选择不同的展示文本