【问题标题】:How should I template my site in Eclipse我应该如何在 Eclipse 中模板化我的网站
【发布时间】:2011-07-08 03:46:23
【问题描述】:

我是第一次使用 Eclipse,想知道用什么方法来模板化它?我对tiles和jsp知之甚少,对数据库知之甚少。

网站:

  • 静态页眉、导航、侧边栏和页脚
  • 几个不同的内容jsp的
  • 主要问题是 --> 我有一个内容部分,只有一种布局,但有 100 个不同的 jsp...我应该怎么做?

谢谢

【问题讨论】:

    标签: eclipse templates jsp struts tile


    【解决方案1】:

    我不确定 Struts 和数据库是如何与此相关的,但基本上,<jsp:include> 是您可以在 JSP 中获得的最好的东西。

    /WEB-INF/template.jsp 的基本启动示例

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <title>${title}</title>
            <link rel="stylesheet" href="style.css" />
            <script src="script.js"></script>
        </head>
        <body>
            <div id="header">
                header
            </div>
            <div id="menu">
                menu
            </div>
            <div id="content">
                <jsp:include page="/WEB-INF/${view}.jsp" />
            </div>
            <div id="footer">
                footer
            </div>
        </body>
    </html>
    

    还有控制器Servlet

    @WebServlet(urlPatterns={"/pages/*"})
    public class Controller extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String view = request.getPathInfo().substring(1);
            String title = titles.get(view); // Imagine that it's a map or something.
    
            request.setAttribute("view", view);
            request.setAttribute("title", title);
            request.getRequestDispatcher("/WEB-INF/template.jsp").forward(request, response);
        }
    
    }
    

    调用它

    http://localhost:8080/contextname/pages/foo

    并提供一个代表内容的/WEB-INF/foo.jsp 文件。例如

    /WEB-INF/foo.jsp

    <h1>This is Foo!</h1>
    <p>Lorem ipsum</p>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-05
      • 2015-08-17
      • 2010-11-29
      • 2010-10-10
      • 1970-01-01
      相关资源
      最近更新 更多