Script tags not rendered in JSP page (using Spring + Tiles + JSPX)

本小妞迷上赌 提交于 2019-11-27 07:06:57

问题


Everyone,

I am facing a strange problem including script tags in a jsp page. Of the three script I include only the first one gets endered in the final page. Here is how I have defined the layouts

<?xml version="1.0" encoding="UTF-8"?>
<!--$Id$ -->
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
       "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<tiles-definitions>
 <definition name="default" template="/WEB-INF/layouts/default.jspx">
  <put-attribute name="header" value="/WEB-INF/views/header.jspx" />
  <put-attribute name="submenu" value="/WEB-INF/views/submenu.jspx" />
  <put-attribute name="body" value="/WEB-INF/views/body.jspx" />
  <put-attribute name="footer" value="/WEB-INF/views/footer.jspx" />
 </definition>
 <definition name="registration" template="/WEB-INF/layouts/registration.jspx">
  <put-attribute name="header" value="/WEB-INF/views/header.jspx" />
  <put-attribute name="body" value="/WEB-INF/views/body.jspx" />
 </definition> 
</tiles-definitions>

the view definition is as follows :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
  "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
  "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
  <tiles-definitions>
   <definition extends="registration" name="register/default">
    <put-attribute name="body" value="/WEB-INF/views/register/register.jspx"/>
   </definition>     
  </tiles-definitions>

here's the page code (registration.jspx)

<html xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:tiles="http://tiles.apache.org/tags-tiles" xmlns:c="http://java.sun.com/jsp/jstl/core" version="2.0">
<jsp:output doctype-root-element="HTML" doctype-system="about:legacy-compat" />
<jsp:directive.page contentType="text/html;charset=UTF-8" />
<jsp:directive.page pageEncoding="UTF-8" />
<jsp:output omit-xml-declaration="true" />
<head>
<c:url var="rootUrl" value="/resources/" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<link rel="stylesheet" type="text/css" href="/resources/css/screen.css" />
<link rel="stylesheet" type="text/css" href="/resources/css/openid.css" />
<link rel="stylesheet" type="text/css" href="/resources/css/openid-shadow.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"/>
<script type="text/javascript" src="/resources/js/openid-jquery.js"/>
<script type="text/javascript" src="/resources/js/openid-en.js"/>
<script type="text/javascript">
//<![CDATA[
    $(document).ready(function() {
        openid.init('openid_identifier');
    });
//]]>   
</script>
<title>Mystery Shopping</title>
</head>
<body>
    <tiles:insertAttribute name="header" />
    <tiles:insertAttribute name="body" />
</body>
</html>

the boby attribute is then overridden in a jspx page called register.jspx which contains the form with openid-selector. The resources folder has also been mapped

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

 <!-- Scans within the base package of the application for @Components to configure as beans -->
 <context:component-scan base-package="vm.brands.controller" />

 <mvc:resources mapping="/resources/**" location="/resources/" />

 <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

</beans>

when I access the url for this registration page I see the following code (when inspecting the elements through firebug)

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=8">
<link rel="stylesheet" type="text/css" href="/resources/css/screen.css">
<link rel="stylesheet" type="text/css" href="/resources/css/openid.css">
<link rel="stylesheet" type="text/css" href="/resources/css/openid-shadow.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js">
<title>Mystery Shopping</title>
</head>

when I view the page source (unformatted) I do see the otherwise not visible script tags; however they "do not" appear formatted like other tags in the source view appear.

<html version="2.0"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/><meta content="IE=8" http-equiv="X-UA-Compatible"/><link href="/resources/css/screen.css" type="text/css" rel="stylesheet"/><link href="/resources/css/openid.css" type="text/css" rel="stylesheet"/><link href="/resources/css/openid-shadow.css" type="text/css" rel="stylesheet"/><script src="http://code.jquery.com/jquery-1.7.1.min.js" type="text/javascript"/><script src="/resources/js/openid-jquery.js" type="text/javascript"/><script src="/resources/js/openid-en.js" type="text/javascript"/><script type="text/javascript">
    $(document).ready(function() {
        openid.init('openid_identifier');
    });

And I have absolutely no ideas on the reasons of this strange behavior.

SOLVED: but a new question pops up

thanks for the help Ralph; I tried your suggestion

<spring:url value="/resources/js/openid-jquery.js" var="openid-jquery_url"/>
<spring:url value="/resources/js/openid-en.js" var="openid-en_url"/>
<script src="${openid-jquery_url}" type="text/javascript"><jsp:text> </jsp:text></script>
<script src="${openid-en_url}" type="text/javascript"><jsp:text> </jsp:text></script>

renders the html (again no ideas why; and I am little too tiered to search it ;-) )

<script src="0" type="text/javascript">
<script src="0" type="text/javascript">

but got this form to work

<c:url var="rootUrl" value="/resources/" />
<script src="${rootUrl}js/openid-jquery.js" type="text/javascript"><jsp:text> </jsp:text></script>
<script src="${rootUrl}js/openid-en.js" type="text/javascript"><jsp:text> </jsp:text></script>

this gives the expected output

<script src="/resources/js/openid-jquery.js" type="text/javascript">
<script src="/resources/js/openid-en.js" type="text/javascript">

is it a problem with the spring:url tag or I am missing something


回答1:


Use Java Script tags in this format, because some browsers have trouble if the script tag has no content:

<spring:url value="/resources/js/openid-jquery.js" var="openid-jquery_url" />
<script src="${openid-jquery_url}" type="text/javascript"><!-- required for FF3 and Opera --></script>

or more "secure"

<script src="${openid-jquery_url}" type="text/javascript"><!-- required for FF3 and Opera --><jsp:text> </jsp:text></script>

Because this will prevent the jspx parser from minimizing the tag.




回答2:


One issue with your Javascript tags is that they need to have a closing tag, you cannot shortcut with /> at the end.

Wrong: <script type="text/javascript" src="script.js"/>

Correct: <script type="text/javascript" src="script.js"></script>




回答3:


Previous comments (type="text/javascript" and jsp:text tag) are indeed needed but per the original question, this has nothing to do with spring:url in specific.

You are using a dash (-) in the var name:

var="openid-jquery_url"

Replace it with an underscore and everything will magically work...

var="openid_jquery_url"

BTW, same is true for c:url, but you simply used a different car name with no dashes when you switched to it.



来源:https://stackoverflow.com/questions/9434755/script-tags-not-rendered-in-jsp-page-using-spring-tiles-jspx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!