如何在js文件中使用消息资源

时间:2016-02-04 22:37:42

标签: javascript java jsp struts static-content

有人可以解释一下这行做了什么。

<servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>/javascript/generic/messages.js</url-pattern>
</servlet-mapping>

可以这样做吗?

我有错误,我的jsp无法使用此行编译

如果我对此行发表评论:

<!--    <servlet-mapping> -->
<!--        <servlet-name>jsp</servlet-name> -->
<!--        <url-pattern>/javascript/generic/messages.js</url-pattern> -->
<!--    </servlet-mapping> -->

在我的messages.js中:

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

var tabMessages = new Array();

tabMessages['errors.invalid']='<bean:message key="errors.invalid" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';
tabMessages['errors.maxlength']='<bean:message key="errors.maxlength" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';
tabMessages['errors.minlength']='<bean:message key="errors.minlength" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';
tabMessages['errors.range']='<bean:message key="errors.range" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';
tabMessages['errors.required']='<bean:message key="errors.required" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';
tabMessages['errors.byte']='<bean:message key="errors.byte" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';
tabMessages['errors.date']='<bean:message key="errors.date" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';
tabMessages['errors.double']='<bean:message key="errors.double" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';
tabMessages['errors.float']='<bean:message key="errors.float" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';
tabMessages['errors.integer']='<bean:message key="errors.integer" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';
tabMessages['errors.long']='<bean:message key="errors.long" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';
tabMessages['errors.short']='<bean:message key="errors.short" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';
tabMessages['errors.creditcard']='<bean:message key="errors.creditcard" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';
tabMessages['errors.email']='<bean:message key="errors.email" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';
tabMessages['errors.numeric']='<bean:message key="errors.numeric" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';
tabMessages['errors.filetype']='<bean:message key="errors.filetype" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';
tabMessages['errors.accord.nonvalide']='<bean:message key="affichagePaa.erreur.accord.nonvalide" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';
tabMessages['errors.telephone']='<bean:message key="errors.telephone" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';
tabMessages['errors.incorrectYear']='<bean:message key="error.incorrectYear" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';


tabMessages['errors.dateCumulAnnuelDepassee']='<bean:message key="declaration.saisieCARealise.validationCumulAnnuel.possibleALaBonneDate.erreur" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';
tabMessages['errors.dateCumulAnnuelEnCours']='<bean:message key="declaration.saisieCARealise.validationCumulAnnuel.dateOK.erreur" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';
tabMessages['errors.dateCumulAnnuelNonAtteinte']='<bean:message key="declaration.saisieCARealise.validationCumulAnnuel.dateDepassee.erreur" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';
tabMessages['errors.cumulAnnuelDejaValide']='<bean:message key="declaration.saisieCARealise.validationCumulAnnuel.dej�Valider.erreur" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';

tabMessages['negociation.suppression.ristourne']='<bean:message key="negociation.suppression.ristourne" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';
tabMessages['changement.champApplication.ristourne']='<bean:message key="negociation.AjoutRistourne.changement.champApplication.ristourne" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';
tabMessages['matrice.suppression.familleaSuivre']='<bean:message key="matrice.suppression.familleaSuivre" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';

tabMessages['alerte.suppression.simple']='<bean:message key="alerte.suppression.simple" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';
tabMessages['alerte.suppression.complexe']='<bean:message key="alerte.suppression.complexe" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';

tabMessages['cumul.annuel.temporaire']='<bean:message key="cumul.annuel.temporaire" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';

tabMessages['calcul.retrocession.type.retrocession.differente']='<bean:message key="calcul.retrocession.type.retrocession.differente" arg0="{0}" arg1="{1}" arg2="{2}" arg3="{3}" arg4="{4}" />';

function getMessage(key, arg0, arg1, arg2, arg3, arg4) {
    var res = tabMessages[key];
    if (arg0 != undefined) {
        res = res.replace(/\{0\}/g, arg0);
    }
    if (arg1 != undefined) {
        res = res.replace(/\{1\}/g, arg1);
    }
    if (arg2 != undefined) {
        res = res.replace(/\{2\}/g, arg2);
    }
    if (arg3 != undefined) {
        res = res.replace(/\{3\}/g, arg3);
    }
    if (arg4 != undefined) {
        res = res.replace(/\{4\}/g, arg4);
    }
    return res;
}

js文件中的函数示例:

function PilotageMDDForm_required() { 

    var nbColonnesMarques = indColMarque;
    var nbLignes = indLignes;
    var indexValidation = 0;
    for(var k = 0; k< nbLignes+1; k++){

        indicetableauTotalCol = 0;

        for(var i = 1; i < nbColonnesMarques-1; i++){

            for(var j = 0; j < MonTableauMarquesFamille[i]+1; j++){

                for(var l = 0; l < MonTableauFamilleType[j]+1; l++){

                    var type = document.getElementsByName("colMarquePilotage["+i+"].colFamillePilotage["+j+"].colTypePilotage["+l+"].lignePilotage["+k+"].type")[0].value;

                    if (type == "ATTERI") {

                        eval("this.a"+indexValidation+" = new Array( \"colMarquePilotage["+i+"].colFamillePilotage["+j+"].colTypePilotage["+l+"].lignePilotage["+k+"].texte\", \"" + getMessage('errors.required','Montant') + "\", new Function (\"varName\", \"this.min='0.00'; this.max='100.00'; return this[varName];\"));");
                        indexValidation++;
                    }
                }
            }       
        }
    }   

}

我有这个错误:

Uncaught ReferenceError: getMessage is not defined

@Roman C :我喜欢这样。?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<html:base ref="site" />

<html:html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<tiles:useAttribute id="title" name="title" />
<title><bean:message key="${title}" /> - v<bean:message
        key="numero.version" /></title>
<link rel="stylesheet" type="text/css"
    href="<html:rewrite page="/css/commun.css"/>">
<link rel="stylesheet" type="text/css"
    href="<html:rewrite page="/css/tiptip/tipTip.css"/>">
<link rel="icon" type="image/png" href="images/icone.ico" />
<script type="text/javascript"
    src="<html:rewrite page="/javascript/generic/jquery-1.2.3.pack.js" />"></script>
<script type="text/javascript"
    src="<html:rewrite page="/javascript/jquery.hotkeys.js" />"></script>
<script type="text/javascript"
    src="<html:rewrite page="/javascript/commun.js" />"></script>
<%--        <script type="text/javascript" src="<html:rewrite page="/javascript/generic/messages.js" />" ></script> --%>
<jsp:include page="/javascript/generic/messages.jsp" />
<script type="text/javascript"
    src="<html:rewrite page="/javascript/tiptip/jquery.tipTip.js" />"></script>
</head>
<body>
    <tiles:insert attribute="content" flush="false" />
</body>
</html:html>

3 个答案:

答案 0 :(得分:1)

好吧,它将js文件映射到jsp。这不好,因为你需要在js文件中编写jsp标签并在服务器端渲染它们。但是js文件应该是静态的,并且由另一个servlet或静态内容提供者提供服务。如果您认为js是一个静态文件而且它不是应该在服务器端呈现的JSP,那么您将文件messages.js的扩展名更改为.jsp。然后你不需要使用这样的servlet映射,但是你需要包含messages.jsp来从服务器端渲染一些javascript内容。您可以使用jsp:include指令轻松完成。但是您需要在<script>中添加messages.jsp标记来呈现javascript。

messages.jsp:

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

<script>
var tabMessages = new Array();
...
</script>

在你加载messages.js的另一个jsp中使用

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

答案 1 :(得分:0)

这一行作为web.xml的一部分,定义了一个servlet映射,或者一个URL或URL模式之间的关系,以及将要处理它的servlet。

在这种情况下,您声明对/javascript/generic/messages.js的请求将由名为jsp的servlet处理(使用web.xml中的<servlet>...</servlet>块定义)。这是一个奇怪的servlet名称,因为servlet和JSP页面是使用Java代码呈现页面的不同方式。

答案 2 :(得分:0)

如果请求来自该URL,则Servlet映射仅用于调用Servlet程序。指定Servlet类以外的其他类型将不合适。知道Servlet程序执行业务逻辑并通过JSP文件重定向到输出。