怎么用 Solon 开发基于 undertow jsp tld 的项目?(新)
2022-04-25 12:20
1266 查看
Solon开发
jsp项目是非常简单的,只要改用
jetty启动器 或者
undertow启动器,其它也没特别之处了。此文用
undertow + jsp + tld这个套路搞一把:
一、 开始Meven配置走起
用solon 做 undertow + jsp 的开发;只需要配置一下 meven 即可(不需要其它的额外处理或启用)
<parent> <groupId>org.noear</groupId> <artifactId>solon-parent</artifactId> <version>1.7.2</version> </parent> <dependencies> <!-- 添加 solon web 开发包 --> <dependency> <groupId>org.noear</groupId> <artifactId>solon-web</artifactId> <type>pom</type> <exclusions> <!-- 排除默认的 jlhttp 启动器 --> <exclusion> <groupId>org.noear</groupId> <artifactId>solon.boot.jlhttp</artifactId> </exclusion> </exclusions> </dependency> <!-- 添加 undertow 启动器 --> <dependency> <groupId>org.noear</groupId> <artifactId>solon.boot.undertow</artifactId> </dependency> <!-- 添加 undertow jsp 扩展支持包 --> <dependency> <groupId>org.noear</groupId> <artifactId>solon.extend.undertow.jsp</artifactId> </dependency> <!-- 添加 jsp 视图渲染器(可以添加一堆别的view插件) --> <dependency> <groupId>org.noear</groupId> <artifactId>solon.view.jsp</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.14.4</version> <scope>provided</scope> </dependency> </dependencies>
二、 其它代码和平常开发就差不多了
//资源路径说明(不用配置) resources/application.properties(或 application.yml) 为应用配置文件 resources/static/ 为静态文件根目标 resources/WEB-INF/view/ 为视图文件根目标(支持多视图共存) //调试模式: 启动参数添加:-deubg=1
- 添加个控制器
src/main/java/webapp/controller/HelloworldController.java
@Controller public class HelloworldController { //这里注入个配置 @Inject("${custom.user}") protected String user; @Mapping("/helloworld") public ModelAndView helloworld(Context ctx){ UserModel m = new UserModel(); m.setId(10); m.setName("刘之西东"); m.setSex(1); ModelAndView vm = new ModelAndView("helloworld.jsp"); //如果是ftl模板,把后缀改为:.ftl 即可 vm.put("title","demo"); vm.put("message","hello world!"); vm.put("m",m); vm.put("user", user); vm.put("ctx",ctx); return vm; } }
- 再搞个自定义标签
src/main/java/webapp/widget/FooterTag.java
(对jsp来说,这个演示很重要)
public class FooterTag extends TagSupport { @Override public int doStartTag() throws JspException { try { String path = Context.current().path(); //当前视图path StringBuffer sb = new StringBuffer(); sb.append("<footer>"); sb.append("我是自定义标签,FooterTag;当前path=").append(path); sb.append("</footer>"); pageContext.getOut().write(sb.toString()); } catch (Exception e){ e.printStackTrace(); } return super.doStartTag(); } @Override public int doEndTag() throws JspException { return super.doEndTag(); } }
- 加tld描述文件
src/main/resources/WEB-INF/tags.tld
(位置别乱改,就放这儿...)
<?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0"> <description>自定义标签库</description> <tlib-version>1.1</tlib-version> <short-name>ct</short-name> <uri>/tags</uri> <tag> <name>footer</name> <tag-class>webapp.widget.FooterTag</tag-class> <body-content>empty</body-content> </tag> </taglib>
- 视图
src/main/resources/WEB-INF/view/helloworld.jsp
<%@ page import="java.util.Random" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="ct" uri="/tags" %> <html> <head> <title>${title}</title> </head> <body> <div> context path: ${ctx.path()} </div> <div> properties: custom.user :${user} </div> <main> ${m.name} : ${message} (我想<a href="/jinjin.htm">静静</a>) </main> <ct:footer/> </body> </html>
三、 疑问
一路上没有web.xml ? 是的,没有。
四、 源码
相关文章推荐
- 编程小白的计算机毕业设计指导开发教程-jsp+servlet是怎么基于cookie实现登陆状态管理的
- 1j2ee即web项目,严格意义上来说,没有jsp代码,只有class文件、html文件、javascript文件。2 基于extjs框架的项目怎么怎么查看在浏览器中展示的最终html页面的代码 3
- JSP实战项目教程|基于JSP的学生请假系统开发视频教程
- .NETCore 基于 dbfirst 体验快速开发项目
- cxf开发基于web的webservice项目1
- jsp+ssm+mysql综合项目实战教程:开发通用进销存管理系统
- 网络爬虫项目开发日志(七): 基于MD5去重树的爬虫设计与优化
- 软件开发人员的简历项目经验怎么写?
- 1小时开发一个基于RTMP的直播项目
- 地图的开发研究--基于openlayers+geoserver+tomcat的离线地图-JAVA项目跨域问题的解决
- 基于 abp vNext 和 .NET Core 开发博客项目 - 数据访问和代码优先
- 项目开发技巧(四):使用JspSmartupload实现文件上传下载(二):jspSmartUpload上传下载全攻略(ZZ)
- 基于IntelliJ IDEA开发Spark的Maven项目——Scala语言
- JAVAWEB开发之权限管理(三)——shiro与企业项目整合开发(基于Spring)
- 基于.net开发平台项目案例集锦
- 怎么让一个项目里swift与OC可以兼容混合开发?
- Web前端开发:SQL Jsp小项目(一)
- 基于springboot的Maven项目开发步骤(1)
- JavaWeb物流管理项目开发(JSP、Servlet、JDBC):4、明确规范
- 做项目的研发模式,即怎么研发一个系统,一步一步怎么做:UP、RUP、迭代式、瀑布式、快速原型、敏捷开发,区别