【问题标题】:Not able to run spring application using XML configuration无法使用 XML 配置运行 spring 应用程序
【发布时间】:2014-01-12 15:52:33
【问题描述】:

我是 Spring 新手,尝试通过 XML 配置运行 Spring 应用程序,但我在控制台中没有收到任何错误。但是应用程序没有运行,我收到 404 错误。我没有在 WEB-INF/lib 中添加 servlet-api jar。谁能帮帮我吗?提前致谢。

    package com.raistudies.action;

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

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

    public class HelloWorldAction extends AbstractController {

        @Override
        protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
                HttpServletResponse arg1) throws Exception {
            System.out.println(" helloworld... ");
            ModelAndView mav = new ModelAndView();
            mav.setViewName("hello");
            mav.addObject("helloMessage", "Hello World from My First Spring 3 mvc application with xml configuration...");
            return mav;
        }

    }

hello.jsp - WEB-INF/jsp/

<html>
    <head>
        <title>Hello World with spring 3 MVC XML configuration</title>  
    </head>
    <body>
        <h1>Welcome! Spring MVC XML configuration is working well</h1>
        ${helloMessage}
    </body>
</html>

index.jsp - WEB-INF

  <html>
    <head>
        <title>rai studies</title>
    </head>
    <body>
            Welcome...
            <a href="hello"><br>Click here to check the output :-)</a>
    </body>
    </html>

web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        id="WebApp_ID_1" version="3.0">

    <display-name>HWEWS3MVCIEA</display-name>

    <servlet>
        <servlet-name>SpringMVCDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/app-config.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>SpringMVCDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

app-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <bean name="/hello.htm" class="com.raistudies.action.HelloWorldAction" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

WEB-INF/lib 下的库

  • commons-logging-1.1.2.jar
  • jstl-1.2.jar
  • log4j-1.2.16.jar
  • spring-aspects-3.2.5.RELEASE.jar
  • spring-beans-3.2.5.RELEASE.jar
  • spring-context-3.2.5.RELEASE.jar
  • spring-core-3.2.5.RELEASE.jar
  • spring-expression-3.2.5.RELEASE.jar
  • spring-web-3.2.5.RELEASE.jar
  • spring-webmvc-3.2.5.RELEASE.jar

WEB-INF/supportingLibrary下的supportingLibrary

  • servlet-api-2.5.jar

【问题讨论】:

  • 哪个 URL 给你一个 404?
  • 感谢您的回复。只有你的评论,我才发现我犯的错误。非常感谢。

标签: spring spring-mvc


【解决方案1】:

您对 Action 的 bean 定义不正确。您必须使用一些有效名称定义一个控制器 bean,例如:

<bean name="helloWorldController" class="com.raistudies.action.HelloWorldAction" />

然后您需要将 url 映射定义添加到您的配置中,以将请求映射到定义的控制器。

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/hello.htm">helloWorldController</prop>
        </props>
    </property>
</bean> 

【讨论】:

  • 您好,感谢您的回复。我试过了,但我仍然收到相同的错误 404。我正在使用 Spring 3.2.5 并扩展 AbstractController 类。
  • 谢谢,当我将 index.jsp url href 更改为“hello.htm”时它正在工作。但是在我阅读的教程中,他们使用了我提到的上述方法。我是否需要始终按照您告诉的方式从 xml 配置中处理控制器?我不应该遵循的教程方式?
  • 我发现错误教程很好,我只是犯了错误。通过不在 url 中提供有效的 ref。现在它开始工作而没有任何改变。谢谢大家。圣诞快乐。
猜你喜欢
  • 2018-07-21
  • 1970-01-01
  • 2019-12-04
  • 1970-01-01
  • 1970-01-01
  • 2021-08-03
  • 2020-04-11
  • 2020-04-20
  • 2020-02-22
相关资源
最近更新 更多