- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在尝试运行一个简单的 Spring 应用程序,但出现以下异常:
javax.servlet.ServletException: javax.servlet.jsp.JspTagException: No message found under code 'label.firstname' for locale 'en_US'.
我的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> </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>
我的 spring-servlet.xml 文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<context:annotation-config />
<context:component-scan base-package="net.viralpatel.contact" />
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="WEB-INF/messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" -->
<!-- destroy-method="close" p:driverClassName="${jdbc.driverClassName}" -->
<!-- p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" /> -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/ContactManager" p:username="root" p:password="12345" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
我正在点击网址:
localhost:8080/MavenWeb-0.0.1/index
和我的服务器堆栈跟踪:
SEVERE: No message found under code 'label.firstname' for locale 'en_US'.
javax.servlet.jsp.JspTagException: No message found under code 'label.firstname' for locale 'en_US'.
at org.springframework.web.servlet.tags.MessageTag.doStartTagInternal(MessageTag.java:184)
at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)
at org.apache.jsp.WEB_002dINF.jsp.contact_jsp._jspx_meth_spring_005fmessage_005f0(contact_jsp.java:252)
at org.apache.jsp.WEB_002dINF.jsp.contact_jsp._jspx_meth_form_005flabel_005f0(contact_jsp.java:219)
at org.apache.jsp.WEB_002dINF.jsp.contact_jsp._jspx_meth_form_005fform_005f0(contact_jsp.java:138)
at org.apache.jsp.WEB_002dINF.jsp.contact_jsp._jspService(contact_jsp.java:91)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:403)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:301)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:162)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
解决问题的步骤:
mvn clean install
进行 maven 构建时messages_en.properties 文件将出现在目标/项目名称/WEB-INF/classes/messages.properties它应该可以解决我的问题。
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
最佳答案
您的 MessageSource
bean 定义有点困惑。通常,基本名称是您要用于消息解析的文件名的前缀(减去 .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_es.properties
、messages_de.properties
这样的资源文件,您可以指定一个WEB-INF/messages
的基本名称。
您的消息包文件的实际名称是什么?
关于java - 在 MessageSource 中找不到消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6748939/
我有一个网络应用程序,我在其中定义了基本的 dispatcher-servlet web.xml 上下文并加载了 applicationContext。 我在 dispatcher-servlet 中
我有代码: messageSource.getMessage("some.key",new Object[]{30,31},Constants.LOCALE) 并在属性文件中键入: some.key=
这个问题在这里已经有了答案: Grails: How to access i18n from controller or service? (7 个回答) 6年前关闭。 我正在使用 grails 2,
我正在尝试在 Spring 中使用 MessageSource 注入(inject)文本消息。我写的程序编译得很好,但它总是打印出默认消息,而不是从 .properties 文件中获取正确的值。我一点
我已经检查过大多数类似的问题,但找不到有用的答案。 我正在使用 Maven v3.3.9 通过 JHipster 创建一个 WebApplication。除其他事项外,还发生了 PDF 生成。该网络应
我正在尝试使用发送邮件任务在成功完成数据流任务后发送消息。 执行包时,产生错误: Error: Failed to lock variable "The extraction was successf
我有以下情况:我从移动设备获取区域设置列表,我需要检查我的 MessageSource 是否与传递的列表匹配。因此,默认情况下,我可以按一种区域设置进行检查,如果 spring 找不到匹配项,它将使用
如果我将最后一个放在 Controller 之外但放在这样的服务内,我会遇到使用 MessageSource 读取属性的问题: @Service public class MessageHandler
我做了一些谷歌搜索,发现我的问题出现了很多次,但我已经尝试了所有建议的解决方案,但它仍然不适合我 我一直收到这个异常: org.springframework.context.NoSuchMessag
我想从属性文件中检索值,但 ApplicationContext 找不到该文件,我不知道为什么。查看我的代码和屏幕截图: spring_part22.xml:
org.springframework.boot spring-boot-starter-parent 1.5.9.RELEASE 在一些spring boot应用中,使用
我正在使用 Hibernate Validator 和 MessageSource 来本地化违规消息。 class FooValidator implements ConstraintValidato
我目前正在开发一个 Spring 项目,并希望将我的 messages.properties 链接到 @Bean 中以启用 UTF-8 编码。 这是我的项目结构: 这是 ServletInitiali
我无法在我的表单支持对象的 Spring 验证期间使用 messages.properties 中的消息。 应用程序配置文件: WEB-INF/classes/messages.proper
我在我的 java 配置中将消息源定义为: @Bean(name = "messageSource") public MessageSource messageSource() { Resou
我正在使用 Spring 框架为我们的基于 Web 的应用程序设计一个插件系统。插件是类路径上的 jar。这样我就可以得到jsp之类的源码了,见下文 ResourcePatternResolver r
我正在尝试运行一个简单的 Spring 应用程序,但出现以下异常: javax.servlet.ServletException: javax.servlet.jsp.JspTagException:
我们正在使用 Spring MessageSource 在我们的应用程序中构建错误消息。 我们像这样填充错误消息 dobInvalid = The DOB supplied {0} is invali
我正在使用 Spring 4 并使用 MessageSource 进行本地化。 我的 xml 文件有: 本地化文件之一 myfile_nl.properties 具有: prop1
我有一个 Controller ,它显示表单并从属性文件加载消息。我扩展了 ReloadableResourceBundleMessageSource 并将其命名为 DatabaseMessageSo
我是一名优秀的程序员,十分优秀!