You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Struts1转Struts2:TLD文件迁移方法咨询

Struts 1 to Struts 2: TLD Migration Guide for Your Tags

Got it, let's walk through how to migrate each of these TLDs from Struts 1 to Struts 2—since the original duplicate post didn't cover this specific detail, I'll break down each case with practical examples:

1) struts-html.tld (Struts 1 HTML Form Tags)

Struts 2 replaces these with its core s tag library, which offers more flexibility and integrates seamlessly with the OGNL expression language.

Key Changes:

  • Replace your taglib declaration from:
    <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
    
    To:
    <%@ taglib uri="/struts-tags" prefix="s" %>
    
  • Common tag replacements:
    • <html:form action="/login" method="post"> → <s:form action="login" method="post"> (note: Struts 2 uses action names, not full paths)
    • <html:text property="username"/> → <s:textfield name="username"/>
    • <html:errors/> → Use <s:actionerror> for global errors or <s:fielderror> for field-specific validation errors
    • <html:submit value="Login"/> → <s:submit value="Login"/>

2) struts-logic.tld (Struts 1 Logic Tags)

Most of these logic tags can be replaced directly with Struts 2's s tags or OGNL expressions embedded in your JSPs, eliminating the need for a separate logic taglib entirely.

Common Replacements:

  • <logic:equal name="user" property="role" value="admin">...</logic:equal> → <s:if test="%{user.role == 'admin'}">...</s:if>
  • <logic:notEmpty name="userList">...</logic:notEmpty> → <s:if test="%{!empty userList}">...</s:if>
  • <logic:iterate id="user" name="userList">${user.name}</logic:iterate> → <s:iterator value="userList" var="user"><s:property value="#user.name"/></s:iterator>

3) c.tld (JSTL Core Tags)

Good news: JSTL works seamlessly with Struts 2—you don't need to migrate these tags at all. Just keep your existing taglib declaration:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

You can continue using <c:if>, <c:forEach>, <c:out>, etc., alongside Struts 2's s tags. If you prefer to standardize on Struts 2 tags, you can replace them with equivalent s tags, but this is completely optional.

4) bdb.tld (Custom Tag Library)

Since this is a custom tag library, the approach depends on how tightly it's coupled to Struts 1:

  • If tags depend on Struts 1 APIs: For example, if your tag classes extend org.apache.struts.taglib.TagSupport, you'll need to rewrite them to work with Struts 2's tag API. Struts 2 tags extend org.apache.struts2.views.jsp.TagSupport, and you'll use ActionContext.getContext() to access the value stack and action objects instead of Struts 1's ActionForm or RequestProcessor APIs.
  • If tags are generic JSP custom tags: No changes needed! As long as the tag classes and TLD file are properly packaged in your project, you can keep using the same taglib declaration and tags as before.
  • Optional: Migrate to Struts 2-style custom tags: For better integration, you could rewrite the custom logic as Struts 2 UI or non-UI tags, or even replace simple logic with OGNL expressions to reduce custom code maintenance overhead.

内容的提问来源于stack exchange,提问作者AbhiN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:13:17