0

我是 Spring-JSF-MVC 的新手,尝试运行示例 Spring JSF 应用程序并出现错误,

文件夹结构

在此处输入图像描述

Stockvaluefetcher.java

package net.javabeat.articles.springjsf.introduction;

import java.util.*;



public class StockValueFetcher {


private Map<String, String> stockSymbolsAndValues;

private String symbolName;
private String symbolValue;

public StockValueFetcher() {
    stockSymbolsAndValues = new HashMap<String, String>();
    stockSymbolsAndValues.put("ABC", "10");
    stockSymbolsAndValues.put("DEF", "20");
    stockSymbolsAndValues.put("GHI", "30");
    stockSymbolsAndValues.put("JKL", "40");
}

public String getSymbolName() {
    return symbolName;
}

public void setSymbolName(String symbolName) {
    this.symbolName = symbolName;
}

public String getSymbolValue() {
    return symbolValue;
}

public void setSymbolValue(String symbolValue) {
    this.symbolValue = symbolValue;
}

public String findStockValue(){
    boolean symbolFound = stockSymbolsAndValues.containsKey(symbolName);
    if (symbolFound){
        symbolValue = stockSymbolsAndValues.get(symbolName);
        return "stockOutputSuccess";
    }else{
        return "stockOutputFailure";
    }
}

}

应用程序上下文.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<bean id="stockBean"
class="net.javabeat.articles.springjsf.introduction.StockValueFetcher">
</bean>

</beans>

面孔-config.xml

<?xml version='1.0' encoding='UTF-8'?>

<faces-config version="1.2"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">

<application>
    <variable-resolver>
        org.springframework.web.jsf.DelegatingVariableResolver
    </variable-resolver>
</application>

<managed-bean>
    <managed-bean-name>stockBean</managed-bean-name>
        <managed-bean-class>
            net.javabeat.articles.springjsf.introduction.StockValueFetcher
        </managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

<navigation-rule>
    <description>Navigation from the hello page.</description>
    <from-view-id>/stockInput.jsp</from-view-id>
    <navigation-case>
        <from-outcome>stockOutputSuccess</from-outcome>
        <to-view-id>/stockOutputSuccess.jsp</to-view-id>
    </navigation-case>
    <navigation-case>
        <from-outcome>stockOutputFailure</from-outcome>
        <to-view-id>/stockOutputFailure.jsp</to-view-id>
    </navigation-case>
</navigation-rule>

</faces-config>

web.xml

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

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

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>

<session-config>
    <session-timeout>30</session-timeout>
</session-config>

<welcome-file-list>
    <welcome-file>faces/stockInput.jsp</welcome-file>
</welcome-file-list>
</web-app>

stockinput.jsp

    <%@page contentType="text/html"%>
    <%@page pageEncoding="UTF-8"%>

    <%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
    <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>

    <f:view>
    <html>
    <head>
    <title>
    Stock Input Page
    </title>
    </head>
    <body>
    <h:form id="stockForm">

    <h1>
    Please Enter the Stock Symbol and click the button
    </h1>

    <p>
    <h:inputText id="stockSymbolInput" value="#{stockBean.symbolName}"
    required="true">
    </h:inputText>
    </p>

    <h:commandButton id="stockSubmit" type="submit" value="Submit Symbol"
    action="#{stockBean.findStockValue}">
    </h:commandButton>

    </h:form>

    </body>
    </html>
    </f:view>

stockfailure.jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>

<f:view>
<h1>
The Stock symbol
<h:outputText value="#{stockBean.symbolName}"> </h:outputText>
is not found. Please check again.
</h1>
</f:view>

</body>
</html>

stockoutputsuccess.jsp <%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>

<f:view>
<h1>
The Stock Value of the symbol
<h:outputText value="#{stockBean.symbolName}"> </h:outputText>
is
<h:outputText value="#{stockBean.symbolValue}"> </h:outputText>
</h1>
</f:view>

</body>
</html>

错误:

INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/applicationContext.xml]
Aug 07, 2013 9:19:56 PM org.springframework.web.context.ContextLoader initWebApplicationContext
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:124)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:93)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:467)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:397)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4205)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4704)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
    at org.apache.catalina.core.StandardService.start(StandardService.java:525)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]
    at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:117)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
    ... 27 more
4

1 回答 1

0
 <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:/applicationContext.xml</param-value>
 </context-param>

@user1050619 请更改您的 web.xml

于 2013-08-08T04:25:30.673 回答