1

単純なSpringMVCコントローラーを作成しましたが、実行しようとするとエラーが発生します。

エラーメッセージ:

org.springframework.beans.factory.CannotLoadBeanCl assException:ServletContextリソース[/WEB-INF/springapp-servlet.xml]で定義された「Redirect.jsp」という名前のBeanのクラス[SpringApp.web.java.HelloController]が見つかりません。ネストされた例外はjava.lang.ClassNotFoundException:SpringApp.web.java.HelloControllerです

これが私のアプリケーション構造です。

SpringApp

----Web Pages   
    ----META-INF    
    ----WEB-INF     
        ----springapp-servlet.xml
        ----web.xml
    ----Redirect.jsp    
    ----index1.jsp  
----Source Packages         
    ----java    
        ----HelloController.java




web.xml
--------

        <?xml version="1.0" encoding="UTF-8"?>
         <!--
    To change this template, choose Tools | Templates
    and open the template in the editor.
    -->

     <web-app version="2.4"
             xmlns="http://java.sun.com/xml/ns/j2ee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
             http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >

      <servlet>
        <servlet-name>springapp</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>

      <servlet-mapping>
        <servlet-name>springapp</servlet-name>
        <url-pattern>*.htm</url-pattern>
      </servlet-mapping>


      <welcome-file-list>
        <welcome-file>
          index1.jsp
        </welcome-file>
      </welcome-file-list>

    </web-app>

springapp-servlet.xml
----------------------


    <?xml version="1.0" encoding="UTF-8"?>
    <!--

    1. This file will be used up by the DispatcherServlet and which contains the bean         definition
    2. The file will be picked up by the specification in the WEB-INF/web.xml using         <servlet>spring</servlet>
    3. hello controller is responsible for handling the request for the particular page     of     the website and known
    as the page controller.
-->

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

      <!-- the application context definition for the springapp DispatcherServlet -->

      <bean name="Redirect.jsp" class="SpringApp.web.java.HelloController"/>

    </beans>

index1.jsp
---------

        <%--
        Document   : index
        Created on : Nov 23, 2012, 11:55:53 AM
        Author     : gopc
    --%>

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
            <h1>Hello World!</h1>
        </body>
    </html>

Redirect.jsp
------------


<%--
    Document   : index
    Created on : Nov 23, 2012, 11:55:53 AM
    Author     : gopc
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Hello Controller redirect</title>
    </head>
    <body>
        <h1>This is redirect from the HelloController!</h1>
    </body>
</html>

HelloController.java
-------------------
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package java;
    /**
     *
     * @author gopc
    */



    import org.springframework.web.servlet.mvc.Controller;
    import org.springframework.web.servlet.ModelAndView;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;

    import java.io.IOException;

    public class HelloController implements Controller {

    protected final Log logger = LogFactory.getLog(getClass());

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        logger.info("Returning hello view");
        System.out.println("hi this handlerequest");

        return new ModelAndView("Redirect.jsp");
        }

    }
4

2 に答える 2

0

あなたは言う:

<bean name="Redirect.jsp" class="SpringApp.web.java.HelloController"/>

これは、Springがパッケージ内でコントローラーを見つけることを期待していることを意味しますSpringApp.web.java。あなたはそれをパッケージに入れましたjava

packageコントローラのステートメントをに変更しSpringApp.web.java、ソースファイルを適切なディレクトリに移動しますSpringApp/web/java

パッケージjavaはJavaプラットフォーム用に予約されているため、とにかく使用しないでください。

于 2012-11-23T09:45:16.377 に答える
0

。という名前のパッケージ内にクラスを配置することはできませんjava。このパッケージは、標準のJDKクラス用に予約されています。

クラスHelloControllerがパッケージfooにある場合、そのクラス名はfoo.HelloControllerです。ありませんSpringApp.web.foo.HelloController

Beanを「Redirect.jsp」と呼ぶことは、多くの混乱のレシピです。なぜJSPという名前のコントローラーBeanを呼び出すのですか?

率直に言って、クラスやパッケージのような非常に基本的なJavaの概念をマスターしていないようです。複雑な獣であるSpringで遊ぶ前に、基本から始めます。

于 2012-11-23T09:48:28.607 に答える