Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在MessageSource中找不到消息_Java_Spring_Jsp_Spring Mvc - Fatal编程技术网

Java 在MessageSource中找不到消息

Java 在MessageSource中找不到消息,java,spring,jsp,spring-mvc,Java,Spring,Jsp,Spring Mvc,我正在尝试运行一个简单的spring应用程序,但出现以下异常: javax.servlet.ServletException:javax.servlet.jsp.jsptageException:否 在区域设置“en_US”的代码“label.firstname”下找到消息 My contact.jsp文件: <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> <%@taglib uri="

我正在尝试运行一个简单的spring应用程序,但出现以下异常:

javax.servlet.ServletException:javax.servlet.jsp.jsptageException:否 在区域设置“en_US”的代码“label.firstname”下找到消息

My contact.jsp文件:

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ page isELIgnored="false" %>

<html>
<head>
    <title>Spring 3 MVC Series - Contact Manager | viralpatel.net</title>
</head>
<body>

<h2>Contact Manager</h2>

<form:form method="post" action="add.html" commandName="contact">

    <table>
    <tr>
        <td><form:label path="firstname"><spring:message code="label.firstname"/></form:label></td>
        <td><form:input path="firstname" /></td>
    </tr>
    <tr>
        <td><form:label path="lastname"><spring:message code="label.lastname"/></form:label></td>
        <td><form:input path="lastname" /></td>
    </tr>
    <tr>
        <td><form:label path="email"><spring:message code="label.email"/></form:label></td>
        <td><form:input path="email" /></td>
    </tr>
    <tr>
        <td><form:label path="telephone"><spring:message code="label.telephone"/></form:label></td>
        <td><form:input path="telephone" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="submit" value="<spring:message code="label.addcontact"/>"/>
        </td>
    </tr>
</table>
</form:form>

<h3>Contacts</h3>
<c:if  test="${!empty contactList}">
<table class="data">
<tr>
    <th>Name</th>
    <th>Email</th>
    <th>Telephone</th>
    <th>&nbsp;</th>
</tr>
<c:forEach items="${contactList}" var="contact">
    <tr>
        <td>${contact.lastname}, ${contact.firstname} </td>
        <td>${contact.email}</td>
        <td>${contact.telephone}</td>
        <td><a href="delete/${contact.id}">delete</a></td>
    </tr>
</c:forEach>
</table>
</c:if>

</body>
</html>
解决问题的步骤:

  • 仅将邮件\u en.properties文件放在src/main/resource文件夹中
  • 确保文件名作为messages\u en.properties正确无误 消息属性
  • 当您使用
    mvn clean install执行maven构建时
    messages_en.properties文件将显示在 target/projectname/WEB-INF/classes/messages.properties
  • 签入springservlet.xml(名称可能不同)并使用 classpath:作为messageSource中的basename的消息
  • 它应该能解决我的问题

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>
    

    您的
    MessageSource
    bean定义有点混乱。通常,basename是要用于消息解析的文件名(减.properties)的前缀,Spring和JDK
    ResourceBundle
    类会将语言和/或国家缩写附加到该文件名

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="WEB-INF/message_en.properties/Message" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>
    
    
    
    例如,如果您希望拥有诸如
    messages.properties
    messages\u es.properties
    messages\u de.properties
    之类的资源文件,则应指定
    WEB-INF/messages
    的基本名称


    您的消息包文件的实际名称是什么?

    我也遇到了同样的问题,但我只是将其作为消息属性保留,它解决了问题

    您必须将“资源”文件夹添加到“Web部署程序集”。之后,将“classpath:messages”替换为“messages”。您需要做的最后一个更改是清理项目

    例如:

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="messages" />
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>
    
    
    

    那就足够了

    我也遇到了同样的问题,解决方案是:

  • messages.properties文件放入源文件夹src/main/resources

  • 定义Bean如下:

    <bean id="messageSource"
             class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages" />
    <property name="defaultEncoding" value="UTF-8" />
    
    
    

  • 您可以看到,我在属性中输入了与文件messages.properties相同的名称,而没有。properties


    这就是问题所在^ ^

    一个重要的考虑因素是,为了自动重新加载,属性文件不应该位于类路径中。服务器通常会缓存属性,因此如果它在classes文件夹中,它就不是真正有效的。根据Spring文档,在classpath:messages的情况下,除-1之外的cachedSeconds将不会有任何效果

    迟做总比不做好。我也有同样的问题。下面是我如何解决它的。 我的文件名是:

    消息
    fr
    .properties 对法国来说,因为这是法国之春。但是,我住在荷兰。所以我试着把它改名为

    消息属性
    它是有效的。不确定这是否是一个长期的解决办法,但我们会向其他任何将面临此问题的人了解。


    除了所有答案之外,请确保让Spring Framework处理.jsp文件。如果直接访问.jsp,则不会处理spring标记,您将看到完全相同的错误。简单地说,通过控制器公开.jsp

    关于如何解决问题的任何建议?我将其修改为WEB-INF/messages,但仍然得到相同的问题message_en.properties是我的消息包文件的实际文件名,如果文件名为
    message_en.properties
    (它在WEB-INF目录中)那么你应该有一个基本名称
    WEB-INF/message
    ,而不是
    WEB-INF/messages
    。为了排除这个问题,你确定这个属性存在于文件中吗?我也不是100%确定如何解析WEB-INF目录中的文件,我通常只是将这些文件放在类路径上(即部署到WEB-INF/classes的Maven项目中的src/main/resources)此处是否还有其他需要检查或修复的内容?1)该属性是否确实存在于文件中?2)该文件是否确实存在于WEB-INF中?3) 如果文件位于WEB-INF/classes中,并且基名称为
    message
    classpath:message
    ,您会得到不同的行为吗?等
    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="messages" />
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>
    
    <bean id="messageSource"
             class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages" />
    <property name="defaultEncoding" value="UTF-8" />