user2724215
user2724215

Reputation: 155

Not able to run spring application using XML configuration

I am new to spring, trying to run the Spring application through XML configuration, but i am not getting any error in console. But the application is not running and i am getting the 404 error. I didn't add the servlet-api jar in WEB-INF/lib. Can anyone please help me? Thanks in advance.

    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>

libraries under the WEB-INF/lib

supportingLibrary under WEB-INF/supportingLibrary

Upvotes: 0

Views: 292

Answers (1)

Vlad
Vlad

Reputation: 481

Your bean definition of the Action is not correct. You have to define a controller bean with some valid name like:

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

Then you need to add the url mapping definition to your configuration to map requests to the defined controller.

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

Upvotes: 0

Related Questions