Is it possible to use JSP/JSTL to generate dynamic css/javascript files?

后端 未结 4 496
名媛妹妹
名媛妹妹 2020-12-19 14:26

If so how do you do this?

(jboss/tomact embedded/jdk 1.5)

not embedded js/css but an actual file...

相关标签:
4条回答
  • 2020-12-19 14:53

    Sure you can. Only thing you need to do is to set the appropriate content type.

    <%@page contentType="text/javascript" %>
    

    or

    <%@page contentType="text/css" %>
    

    Take care with the fact that some webbrowsers might be picky on the file extension used in the actual request URL. I have never tried it as I normally would use a Servlet for those purposes, but I won't be surprised if especially MSIE won't eat that.

    0 讨论(0)
  • 2020-12-19 14:59

    On glassfish 3.1 you may need to add this:

    <servlet>
    <servlet-name>jsp</servlet-name>
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
    <init-param>
      <param-name>xpoweredBy</param-name>
      <param-value>true</param-value>
    </init-param>
    <load-on-startup>3</load-on-startup>
    

    And then

      <servlet-mapping>
        <servlet-name>jsp</servlet-name>
        <url-pattern>*.myext</url-pattern>
        <url-pattern>*.jsp</url-pattern>
    </servlet-mapping>
    

    To your web.xml, if not, you may experience "java.lang.RuntimeException: There is no web component by the name of default here. " Error

    0 讨论(0)
  • 2020-12-19 15:03

    What you want to do is assign the *.css servlet mapping to the JSPServlet.

    In most containers, you will see a mapping like this (this is from Glassfish, in it's default-web.xml):

      <servlet>
        <servlet-name>jsp</servlet-name>
        <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
        <init-param>
          <param-name>xpoweredBy</param-name>
          <param-value>true</param-value>
        </init-param>
        <load-on-startup>3</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>jsp</servlet-name>
        <url-pattern>*.jsp</url-pattern>
      </servlet-mapping>
    

    Here, it is declaring the JSP servlet, and mapping "*.jsp" to it. So, in this case, the JSP servlet reference name is, simply 'jsp'.

    So you would want to add:

    <servlet-mapping>
      <servlet-name>jsp</servlet-name>
      <url-pattern>*.css</url-pattern>
    </servlet-mapping>
    

    When you do that, "suddenly" ALL of your CSS files are, effectively, JSPs, so you can do with them whatever you want.

    The detail is I don't know if 'jsp' is the same for ALL containers, so your web.xml MAY NOT be portable.

    But that's the gist of what you want to do. If you don't want ALL CSS to be JSPs, you could put the files in their own directory, and map that to the JSP servlet. Then ANYTHING you put in there would be a JSP (css, js, etc.)

    0 讨论(0)
  • 2020-12-19 15:04

    Sure, JSP can output any necessary text you need be that (X)HTML or CSS or JavaScript code. I do this regularly for ERP customization, inject a javascript script at the end of every page and via the context in which it loads able to manipulate necessary data fields on the page without touching the underlying app.

    0 讨论(0)
提交回复
热议问题