开发者

Insert html elements dynamically into a template of jsp file

I want to insert some html elements dynamically into a template of jsp file.

I know I can do this by using a code snippet of javascript, but I wonde开发者_C百科r is there better ways?

Here my example:

myTemplate.jsp

......
<div id="content"></div>
.....

myPage.jsp

<jsp:include page="myTemplate.jsp"></jsp:include>

//This the line which I'm searching if there is.
setContent into the div with id "content"


That's not possible with JSP. Consider migrating from JSP to Facelets. It's a XHTML based view technology. Then you'll be able to achieve the desired functionality with <ui:insert> and <ui:define>.

/WEB-INF/web.xml

<servlet>
    <servlet-name>facesServlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>facesServlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

/WEB-INF/template.xhtml

<!DOCTYPE html>
<html lang="en" xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
    <head>
        <title><ui:insert name="title" /></title>
    </head>
    <body>
        <ui:insert name="content" />
    </body>
</html>

/page.xhtml

<ui:composition template="/WEB-INF/template.xhtml" xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
    <ui:define name="title">Page title</ui:define>
    <ui:define name="content">
        <div id="content"></div>
    </ui:define>
</ui:composition>

Calling /page.xhtml in webbrowser will end up as

<!DOCTYPE html>
<html lang="en">    
    <head>
        <title>Page title</title>
    </head>
    <body>
        <div id="content"></div>
    </body>
</html>

Another advantage of Facelets is the builtin support for JSF, a component based MVC framework.


Well, whatever framework you will use, that will be translated to some Browser Component[javascript/flash/applet] and some Server Side component[Servlet/Filter], if you want to modify content dynamically, without refreshing the page. From that perspective plain JavaScript/Servlet combination is fine. But for manageability perspective, use any java framework which has Ajax support. JSF2, JSF with RichFaces, GWT etc. to name a few.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜