使用Spring MVC在jsp中读取属性文件

时间:2014-01-02 05:48:37

标签: jsp spring-mvc servlet-3.0 servlet-listeners

我在/WEB-INF/properties/Messages.properties中有一个属性文件,内容如下

label.username=Enter User Name
label.password=Enter Password
....
....

我已将dispatcher-servlet.xml配置为

调度-servlet.xml中:

<context:component-scan base-package="com.st.spring.mvc" />
<mvc:annotation-driven />
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames" value="WEB-INF/properties/Messages"/>
</bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>

的web.xml:

<welcome-file-list>
    <welcome-file>/WEB-INF/jsp/hello.jsp</welcome-file>
</welcome-file-list>

<servlet><br>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<listener> 
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

最后,我试图在我的jsp页面中读取该属性文件值,如下所示

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<spring:message code="label.username"/>
<spring:message code="label.password"/>

然后,我得到了像

这样的例外 第2行

org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/jsp/hello.jsp

1: <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
2: <spring:message code="label.username"/>


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)<br>
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:465)<br>
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)<br>
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)<br>
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)<br>


root cause <br>

java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?<br>
    org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:90)<br>
    org.springframework.web.servlet.support.RequestContextUtils.getWebApplicationContext(RequestContextUtils.java:85)<br>
    org.springframework.web.servlet.support.RequestContext.initContext(RequestContext.java:207)<br>
    org.springframework.web.servlet.support.JspAwareRequestContext.initContext(JspAwareRequestContext.java:74)<br>
    org.springframework.web.servlet.support.JspAwareRequestContext.<init>(JspAwareRequestContext.java:48)<br>
    org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:76)<br>
    org.apache.jsp.WEB_002dINF.jsp.hello_jsp._jspx_meth_spring_005fmessage_005f0(hello_jsp.java:95)<br>
    org.apache.jsp.WEB_002dINF.jsp.hello_jsp._jspService(hello_jsp.java:68)<br>
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)<br>
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)<br>
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)<br>
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)<br>
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)<br>
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)<br>

有人能解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

dispatcher-servlet.xml信息放在web.xml文件中。

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>