user3172493
user3172493

Reputation: 13

Using Spring MVC in an existing Spring with JSP application

I tried to implement Spring MVC into my existing application, but even with hours of trying to figure out whats wrong it still won't work. The JSP with Spring application was already running without problems and is still working. Every time a request should fit the pattern from the MVC servlet it is actualy send to it. But I'm not able to catch it in the Controller.

First my web.xml where I defined the used servlets

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

    <servlet>
        <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>*.htm</url-pattern>    
    </servlet-mapping>

Then i created the servlet file dispatcher-servlet.xml, only with the following attributes:

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

<context:component-scan base-package="at.htlstp.app.mvc"/>

<context:annotation-config />

Now the given Controller is instantiated wich i was able to find out with the @PostConstruct Method. But it is not called on an Request.

@Controller
//@RequestMapping(value = "*.htm")
public class AndroidController implements Serializable {

    @PostConstruct
    private void setup() {
        System.out.println("Class created!");
    }


    @RequestMapping(value = "test.htm", method = RequestMethod.GET)
    public String findDepatment() {
        System.out.println("Method called");
        return "<H1>bar</H1>";
    }

}

Every time I'm trying to request the Application with a matching *.htm request i am getting only the following error.

WARNING: No mapping found for HTTP request with URI [/ViewMyMarks/test.htm] in DispatcherServlet with name 'dispatcher' 

I would really appreciate you answers if you had the same problem in the past. My only idea is, that the Controller is used for JSP request and MVC is not instantiating it. Then it wouldnt be able to find a matching pattern. But what can i do about it?

Upvotes: 1

Views: 582

Answers (1)

Pradeep Kr Kaushal
Pradeep Kr Kaushal

Reputation: 1546

Your dispatcher-servlet.xml does not have mvc-driven annotation information put the given line.

  <mvc:annotation-driven />  

And request mapping put the start the value from /

 @RequestMapping(value = "/test.htm", method = RequestMethod.GET)  

Read the documentation of spring-mvc. It will you help you to understand the spring mvc http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html

Upvotes: 1

Related Questions