SpringMVC(二七) 自定义视图

时间:2023-03-09 23:01:46
SpringMVC(二七) 自定义视图

可以参考博客http://www.cnblogs.com/parryyang/p/5683600.html,举例很清晰。

对自定义的视图名称匹配不同的解析器进行解析。

作用:自己定义视图,视图继承view类或者abstractExcelView或者abstractPdfView,将内容以Excel或者PDF格式显示。

关键的实现过程:

1. 创建excelView视图,继承AbstractXlsxView。

 参考如下代码,实现功能:从modelAndView中获取model数据,作为excel视图显示。

 

2.创建控制器。

参考如下代码,实现功能:访问相关URL时,直接去访问创建的excelView视图。

SpringMVC(二七) 自定义视图
package com.tiekui.springmvc.handlers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import com.sun.org.apache.xpath.internal.operations.Mod;
import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;
import com.tiekui.springmvc.pojo.User;
import com.tiekui.springmvc.views.excelView; @Controller
public class ExcelViewTest { @RequestMapping("excelView")
public ModelAndView excelViewTest(User user) {
Map<String, Object> model = new HashMap<>();
ArrayList<User> userlist = new ArrayList<>();
userlist.add(user);
model.put("userList", userlist);
ModelAndView modelAndView = new ModelAndView(new excelView(),model);
return modelAndView;
} }
SpringMVC(二七) 自定义视图

3.访问视图index.jsp

SpringMVC(二七) 自定义视图
    <form action="excelView">
username : <input type="text" name="username" value="1">
<br>
password : <input type="password" name="password" value="1">
<br>
email : <input type="text" name="email" value="1">
<br>
age : <input type="text" name="age" value="1">
<br>
province : <input type="text" name="address.province" value="1">
<br>
city : <input type="text" name="address.city" value="1">
<br>
<input type="submit" value="PojoTest">
</form>
SpringMVC(二七) 自定义视图

4.SpringMVC配置文件中必须添加以下内容: 

<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="0"></property>
</bean>