<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/views/" p:suffix=".jsp" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/WEB-INF/views/"
p:suffix=".jsp" />
path | 用属性路径表示的表单对象属性; |
htmlEscape | 绑定的表单属性值是是否要对 HTML 特殊字符进行转换,默认值为 true; |
cssClass | 表单组件对应的CSS样式类名 |
cssErrorClass | 当表单组件的数据存在相应的验证错误时,采用的 CSS 样式类 |
cssStyle | 表单组件对应的 CSS 样式串 |
输入框标签组件,如 |
|
密码框输入组件,如 |
|
隐藏框组件,如 |
|
多行输入框组件,如 |
|
单选框组件,当表单的对应属性和value值相同时,该单选框被选中,如: 当表单对象sex=0,第一个单选框被选中; |
|
单选框组件组,用于构造多个的单选框,如下: sexOptions 可以在jsp中定义,该对象可以是一个List,String[],或Map,用于产生和该变量元素相同的单选框组件; 默认情况下,如果 sexOptions 为 List / String[] ,单选框的 value 和 label 相同; 如果 sexOptions 为 Map 类型,单选框的 value 为 Map 的 key,label 为 Map 的 value; 也可以通过 itemValue,itemLabel 来更改以上Map这个行为: |
|
复选框组件,如: 一般来说,like |
|
复选框组件组,用于够着多个复选框,如下: 用法类似于 |
|
下拉框组件,如: 用法类似于 |
|
显示表单错误校验信息,如: |
<jsp:useBean id="user" class="site.assad.domain.User" scope="request" />
<form:form action="/user/handleRegister" method="post" modelAttribute="user">
用户名:<form:input path="userName" /><br/>
<form:errors path="userName" cssClass="error"/><br />
密码:<form:input path="password" /><br/>
<form:errors path="password"/><br />
Email:<form:input path="email" cssClass="error" /><br/>
<form:errors path="email"/><br />
生日(格式"yyyy-MM-dd"):<form:input path="birthday" /><br/>
<form:errors path="birthday" cssClass="error"/><br />
<input type="submit" value="提交" />
<input type="reset" value="重置" /><br/>
form:form>
<html>
<head>
<title>User Liststitle>
head>
<body>
<#list userList as user>
<p>user id:${user.userId}p>
<p>user name:${user.userName}p>
<p>user password:${user.password}p>
<p>user birthday:${user.birthday?string("yyyy-MM-dd")}p>
<br/>
#list>
body>
html>
package site.assad.web;
"/user") (
public class UserController {
private UserService userService;
//转发到 FreeMarker 模板视图
"/showUserListByFtl") (
public ModelAndView showUserListByFtl(){
List<User> userList = userService.getAllUser();
return new ModelAndView("/user/ftl/userListFtl","userList",userList);
}
}
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"
p:templateLoaderPath="/WEB-INF/views"
p:defaultEncoding="UTF-8">
<property name="freemarkerSettings">
<props>
<prop key="classic_compatible">trueprop>
props>
property>
bean>
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"
p:order="5"
p:suffix=".ftl"
p:contentType="text/html;charset=utf-8"
/>
<bean id="xmlMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="streamDriver">
<bean class="com.thoughtworks.xstream.io.xml.StaxDriver" />
property>
<property name="annotatedClasses">
<list>
<value>site.assad.domain.Uservalue>
list>
property>
bean>
<bean id="userListXML" class="org.springframework.web.servlet.view.xml.MarshallingView"
p:modelKey="userList"
p:marshaller-ref="xmlMarshaller"
/>
package site.assad.web;
"/user") (
public class UserController {
private UserService userService;
//转发到 XML 视图
"/showUserListByXML") (
public ModelAndView showUserListByXML(){
List<User> userList = userService.getAllUser();
return new ModelAndView("userListXML");
}
}
<bean id="userListJSON" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"
p:modelKey="userList"
/>
package site.assad.web;
"/user") (
public class UserController {
private UserService userService;
//转发到 JSON 视图
"/showUserListByJSON") (
public ModelAndView showUserListByJSON(){
List<User> userList = userService.getAllUser();
return new ModelAndView("userListJSON");
}
}
package site.assad.web;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import site.assad.domain.User;
public class UserListExcelView extends AbstractExcelView {
protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
//excel 文件编码必须为“iso8859-1“,否则会出现乱码
response.setHeader("Content-Disposition","inline;filename=" + new String("用户列表".getBytes(),"iso8859-1"));
List<User> userList = (List<User>) model.get("userList");
//创建excel表格
HSSFSheet sheet = workbook.createSheet("users");
//创建行首
HSSFRow header = sheet.createRow(0);
header.createCell(0).setCellValue("账号");
header.createCell(1).setCellValue("用户名");
header.createCell(2).setCellValue("生日");
//填充模型
int rowNum = 1;
for(User user : userList){
HSSFRow row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(user.getUserId());
row.createCell(1).setCellValue(user.getUserName());
row.createCell(2).setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(user.getBirthday()));
}
}
}
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="10" />
<bean id="userListExcel" class="site.assad.web.UserListExcelView" />
//转发到 Excel 视图
"/showUserListByExcel") (
public ModelAndView showUserListByExcel(){
List<User> userList = userService.getAllUser();
return new ModelAndView("userListExcel");
}
package site.assad.web;
import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.BaseFont;
import org.springframework.web.servlet.view.document.AbstractPdfView;
import com.lowagie.text.pdf.PdfWriter;
import site.assad.domain.User;
public class UserListPDFView extends AbstractPdfView{
protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer, HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setHeader("Content-Disposition","inline;filename=" + new String("用户列表".getBytes(),"iso8859-1"));
List<User> userList =(List<User>) model.get("userList");
//创建表格,并设置表格格式
Table table = new Table(3);
table.setWidth(80); //设置表格宽度
table.setBorder(1); //设置表格边框
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); //设置对齐格式
table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
//设置中文字体
BaseFont cnBaseFont = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",false);
Font cnFont = new Font(cnBaseFont,10,Font.NORMAL, Color.BLUE);
//填充表头,中文字符需要使用转换器构造Cell,否则会产生乱码
table.addCell(buildFontCell("账号",cnFont));
table.addCell(buildFontCell("名称",cnFont));
table.addCell(buildFontCell("生日",cnFont));
//填充表格数据
for(User user : userList){
table.addCell(user.getUserId()+""); //英文字符直接添加
table.addCell(buildFontCell(user.getUserName(),cnFont));
table.addCell(new SimpleDateFormat("yyyy-MM-dd").format(user.getBirthday()));
}
}
private Cell buildFontCell(String content,Font font) throws RuntimeException{
try {
return new Cell(new Phrase(content,font));
} catch (BadElementException e) {
throw new RuntimeException(e);
}
}
}
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="10" />
<bean id="userListPDF" class="site.assad.web.UserListPDFView" />>
//转发到 PDF 视图
"/showUserListByPDF") (
public ModelAndView showUserListByPDF(){
List<User> userList = userService.getAllUser();
return new ModelAndView("userListPDF");
}
//使用同一URL获取不同格式的返回内容
"/showUserListMix") (
public ModelAndView showUserListMix(){
List<User> userList = userService.getAllUser();
return new ModelAndView("userListMix");
}
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"
p:ignoreAcceptHeader="true"
p:favorPathExtension="false"
p:favorParameter="true"
p:parameterName="format"
p:defaultContentType="text/html" >
<property name="mediaTypes">
<value>
html=text/html
xml=application/xml
json=application/json
value>
property>
bean>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"
p:order="0"
p:contentNegotiationManager-ref="contentNegotiationManager" >
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"
p:modelKey="userList" />
<bean class="org.springframework.web.servlet.view.xml.MarshallingView"
p:modelKey="userList" p:marshaller-ref="xmlMarshaller" />
list>
property>
bean>
.......