freemarker 在FreeMarker中使用JSP标签

首先 在web.xml 文件里加入以下语句

xml 代码
  1. <servlet>  
  2.     <servlet-name>JspSupportServlet</servlet-name>  
  3.     <servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class>  
  4.     <load-on-startup>1</load-on-startup>  
  5. </servlet>  

然后,在需要用到JSP标签的页面 加入

<#assign  c =JspTaglibs["/WEB-INF/tld/c.tld"] >

使用方法

<@c.import url="http://127.0.0.1:8080/menu.do"/>


转载自:http://qisa.iteye.com/blog/124656

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在 Spring Boot 项目,您可以借助 Spring 的集成来更方便地使用 FreeMarkerJSP,同时使用 Spring 的 JDBC 模块来与数据库进行交互。以下是一个示例代码,演示如何将文件存储到 MySQL 数据库: 1. 添加依赖 在 pom.xml 文件添加以下依赖: ```xml <dependencies> <!-- Spring Boot Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- FreeMarker 模板引擎 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <!-- JDBC 模块 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- MySQL 驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- Tomcat JDBC 连接池 --> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> </dependency> </dependencies> ``` 2. 配置数据库连接 在 application.properties 文件添加数据库连接信息: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase spring.datasource.username=username spring.datasource.password=password ``` 3. 定义文件实体类 创建一个名为 FileEntity 的类,用于表示文件实体: ```java public class FileEntity { private Long id; private String name; private byte[] content; // 省略 getter 和 setter 方法 } ``` 4. 定义文件存储服务 创建一个名为 FileStorageService 的接口,定义文件存储服务的方法: ```java public interface FileStorageService { void save(FileEntity file); FileEntity getById(Long id); } ``` 在实现类使用 Spring 的 JdbcTemplate 对象来执行 SQL 语句: ```java @Service public class FileStorageServiceImpl implements FileStorageService { @Autowired private JdbcTemplate jdbcTemplate; public void save(FileEntity file) { String sql = "INSERT INTO files (name, content) VALUES (?, ?)"; jdbcTemplate.update(sql, file.getName(), file.getContent()); } public FileEntity getById(Long id) { String sql = "SELECT * FROM files WHERE id = ?"; return jdbcTemplate.queryForObject(sql, new Object[]{id}, new RowMapper<FileEntity>() { public FileEntity mapRow(ResultSet rs, int rowNum) throws SQLException { FileEntity file = new FileEntity(); file.setId(rs.getLong("id")); file.setName(rs.getString("name")); file.setContent(rs.getBytes("content")); return file; } }); } } ``` 5. 定义控制器 创建一个名为 FileController 的控制器,在其注入 FileStorageService,并提供上传文件和下载文件的方法: ```java @Controller public class FileController { @Autowired private FileStorageService fileStorageService; @RequestMapping(value = "/upload", method = RequestMethod.POST) public String upload(@RequestParam("file") MultipartFile file) throws IOException { if (!file.isEmpty()) { FileEntity fileEntity = new FileEntity(); fileEntity.setName(file.getOriginalFilename()); fileEntity.setContent(file.getBytes()); fileStorageService.save(fileEntity); } return "redirect:/"; } @RequestMapping(value = "/download/{id}", method = RequestMethod.GET) public void download(@PathVariable("id") Long id, HttpServletResponse response) throws IOException { FileEntity fileEntity = fileStorageService.getById(id); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileEntity.getName() + "\""); response.getOutputStream().write(fileEntity.getContent()); } } ``` 6. 编写 FreeMarkerJSP 视图 在 FreeMarkerJSP 视图,您可以使用类似以下的语句来上传文件: ```html <form method="post" enctype="multipart/form-data" action="/upload"> <input type="file" name="file"/> <button type="submit">上传</button> </form> ``` 使用类似以下的语句来下载文件: ```html <a href="/download/${file.id}">${file.name}</a> ``` 在上述代码,${file.id} 和 ${file.name} 是从数据库检索文件得到的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值