SpringMVC(二七) 自定义视图

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

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

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

 

关键的实现过程:

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

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

 View Code

 

2.创建控制器。

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

  

复制代码
复制代码
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;
    }
    
}
复制代码
复制代码

3.访问视图index.jsp

  

复制代码
复制代码
    <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>
复制代码
复制代码

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

<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
    <property name="order" value="0"></property>
</bean>
posted @   尐鱼儿  阅读(148)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示