2012-02-24 17 views
5

tutti,tag di script non resi a pagina JSP (usando primavera + Tiles + JSPX)

Sono di fronte a un problema strano compresi i tag di script in una pagina JSP. Dei tre script che includo, solo il primo viene pubblicato nella pagina finale. Ecco come ho definito i layout

<?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> 

la definizione della vista è la seguente:

<?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> 

ecco il codice della pagina (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> 

l'attributo boby è quindi sovrascritto in una pagina jspx denominata register.jspx che contiene il modulo con openid-selector. La cartella di risorse è stato mappato

<?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> 

quando accedo L'URL per questa pagina di registrazione vedo il seguente codice (quando si ispeziona gli elementi attraverso 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> 

quando ho visualizzare il sorgente della pagina (non formattato) vedo i tag di script altrimenti non visibili; tuttavia, "non" appaiono formattati come appaiono gli altri tag nella vista sorgente.

<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'); 
    }); 

E non ho assolutamente idee sulle ragioni di questo strano comportamento.

risolto:ma una nuova domanda si apre

grazie per l'aiuto Ralph; Ho cercato il tuo suggerimento

<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> 

rende l'html (di nuovo senza idee perché; e sto po 'troppo a più livelli per esplorarlo ;-))

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

ma ho ottenuto questo modulo per lavorare

<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> 

questo dà i risultati attesi

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

è un problema con la molla: tag url o mi manca qualcosa

+0

Non penso che questa sia la causa; ma hai notato che carichi il file 'jquery-1.7.1.min.js' con un URL diverso' http: // code.jquery.com/jquery-1.7.1.min.js' invece delle risorse del tuo server? – Ralph

+0

yeap Ho sentito che è inutile salvare una copia nella mia app Web quando abbiamo un modo di recuperarlo dalla modalità host del server jQuery. Non ho usato la primavera: il formato url per jquery dal momento che veniva incluso bene in primo luogo. –

risposta

12

tag Usa Script Java in questo formato, perché alcuni browser hanno problemi se il tag script non ha un contenuto:

<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> 

o più "sicuro"

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

Perché questo impedirà il parser jspx da ridurre al minimo il tag.

+0

Ho modificato la domanda in quanto non posso rispondere alla mia domanda. ha funzionato, ma tramite c: url, spring: url stava sostituendo vars al numero 0 –

1

Un problema con i tag Javascript è che è necessario avere un tag di chiusura, non è possibile creare un collegamento con /> alla fine.

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

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

0

commenti precedenti (type="text/javascript" e jsp:text tag) sono effettivamente necessari, ma per la domanda originale, questo non ha nulla a che fare con spring:url in particolare.

Si utilizza un trattino (-) nel nome var:

var="openid-jquery_url" 

sostituirlo con un carattere di sottolineatura e tutto sarà magicamente funzionerà ...

var="openid_jquery_url" 

proposito, lo stesso vale per c:url, ma hai semplicemente utilizzato un nome diverso per l'auto senza trattini quando sei passato ad esso.

Problemi correlati